Move a lighted bar back and forth on an LED bargraph display. Demonstrations for loops.
Fork of ForLoopIteration by
main.cpp@1:4d725d785ea6, 2017-09-21 (annotated)
- Committer:
- CSTritt
- Date:
- Thu Sep 21 20:43:08 2017 +0000
- Revision:
- 1:4d725d785ea6
- Parent:
- 0:9475544275a6
My initial version. Alternative to the approach used in ForLoopIteration.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
CSTritt | 0:9475544275a6 | 1 | /* |
CSTritt | 1:4d725d785ea6 | 2 | Project: ForLoopIteration2 |
CSTritt | 0:9475544275a6 | 3 | File: main.cpp |
CSTritt | 0:9475544275a6 | 4 | |
CSTritt | 0:9475544275a6 | 5 | Demonstrates the use of a for() loop. Lights multiple LEDs in sequence, then |
CSTritt | 1:4d725d785ea6 | 6 | in reverse. Uses alternative approach to that used in ForLoopIteration. |
CSTritt | 0:9475544275a6 | 7 | |
CSTritt | 0:9475544275a6 | 8 | The circuit: |
CSTritt | 0:9475544275a6 | 9 | |
CSTritt | 0:9475544275a6 | 10 | Bargraph LEDs from pins 2 through 11 to ground via 330 Ohm resistors. |
CSTritt | 0:9475544275a6 | 11 | |
CSTritt | 0:9475544275a6 | 12 | Created 2006 |
CSTritt | 0:9475544275a6 | 13 | by David A. Mellis |
CSTritt | 0:9475544275a6 | 14 | Modified 30 Aug 2011 |
CSTritt | 0:9475544275a6 | 15 | by Tom Igoe |
CSTritt | 0:9475544275a6 | 16 | Modified for Nucleo / mbed 12 Aug 2017 |
CSTritt | 0:9475544275a6 | 17 | by Sheila Ross |
CSTritt | 0:9475544275a6 | 18 | Modified by C. S. Tritt 9/21/17 (v. 1.0) |
CSTritt | 0:9475544275a6 | 19 | |
CSTritt | 0:9475544275a6 | 20 | This example code is in the public domain. |
CSTritt | 0:9475544275a6 | 21 | */ |
CSTritt | 0:9475544275a6 | 22 | |
CSTritt | 0:9475544275a6 | 23 | #include "mbed.h" |
CSTritt | 0:9475544275a6 | 24 | |
CSTritt | 0:9475544275a6 | 25 | float delay = 0.1; // Seconds, the higher the number, the slower the rate. |
CSTritt | 0:9475544275a6 | 26 | |
CSTritt | 0:9475544275a6 | 27 | BusOut bar_graph(D2,D3,D4,D5,D6,D7,D8,D9,D10,D11); |
CSTritt | 0:9475544275a6 | 28 | |
CSTritt | 0:9475544275a6 | 29 | int main() { |
CSTritt | 0:9475544275a6 | 30 | |
CSTritt | 0:9475544275a6 | 31 | while(true) { // Keep the lights going back and forth forever. |
CSTritt | 0:9475544275a6 | 32 | |
CSTritt | 1:4d725d785ea6 | 33 | // Shift bar from 0 to 9 places. 1 << 9 is 2 to the 10th power. |
CSTritt | 1:4d725d785ea6 | 34 | for(int lights = 1; lights <= (1<<9); lights = (lights << 1)) { |
CSTritt | 1:4d725d785ea6 | 35 | bar_graph = lights; // Shifting rather than incrementing in for. |
CSTritt | 0:9475544275a6 | 36 | wait(delay); // Pause |
CSTritt | 0:9475544275a6 | 37 | } |
CSTritt | 0:9475544275a6 | 38 | |
CSTritt | 0:9475544275a6 | 39 | // Now shift down from 9 to 0 places. |
CSTritt | 1:4d725d785ea6 | 40 | for(int lights = (1<<9); lights >= 1; lights = (lights >> 1)) { |
CSTritt | 1:4d725d785ea6 | 41 | bar_graph = lights; // Shifting rather than incrementing in for. |
CSTritt | 0:9475544275a6 | 42 | wait(delay); // Pause |
CSTritt | 0:9475544275a6 | 43 | } |
CSTritt | 0:9475544275a6 | 44 | } |
CSTritt | 0:9475544275a6 | 45 | } |