Move a lighted bar back and forth on an LED bargraph display. Demonstrations for loops.
Fork of ForLoopIteration by
main.cpp
- Committer:
- CSTritt
- Date:
- 2017-09-21
- Revision:
- 1:4d725d785ea6
- Parent:
- 0:9475544275a6
File content as of revision 1:4d725d785ea6:
/* Project: ForLoopIteration2 File: main.cpp Demonstrates the use of a for() loop. Lights multiple LEDs in sequence, then in reverse. Uses alternative approach to that used in ForLoopIteration. The circuit: Bargraph LEDs from pins 2 through 11 to ground via 330 Ohm resistors. Created 2006 by David A. Mellis Modified 30 Aug 2011 by Tom Igoe Modified for Nucleo / mbed 12 Aug 2017 by Sheila Ross Modified by C. S. Tritt 9/21/17 (v. 1.0) This example code is in the public domain. */ #include "mbed.h" float delay = 0.1; // Seconds, the higher the number, the slower the rate. BusOut bar_graph(D2,D3,D4,D5,D6,D7,D8,D9,D10,D11); int main() { while(true) { // Keep the lights going back and forth forever. // Shift bar from 0 to 9 places. 1 << 9 is 2 to the 10th power. for(int lights = 1; lights <= (1<<9); lights = (lights << 1)) { bar_graph = lights; // Shifting rather than incrementing in for. wait(delay); // Pause } // Now shift down from 9 to 0 places. for(int lights = (1<<9); lights >= 1; lights = (lights >> 1)) { bar_graph = lights; // Shifting rather than incrementing in for. wait(delay); // Pause } } }