Move a lighted bar back and forth on an LED bargraph display. Demonstrations for loops.

Dependencies:   mbed

Fork of ForLoopIteration by Charles Tritt

Files at this revision

API Documentation at this revision

Comitter:
CSTritt
Date:
Thu Sep 21 20:43:08 2017 +0000
Parent:
0:9475544275a6
Commit message:
My initial version. Alternative to the approach used in ForLoopIteration.

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r 9475544275a6 -r 4d725d785ea6 main.cpp
--- a/main.cpp	Thu Sep 21 20:11:08 2017 +0000
+++ b/main.cpp	Thu Sep 21 20:43:08 2017 +0000
@@ -1,9 +1,9 @@
 /*
-  Project: ForLoopIteration
+  Project: ForLoopIteration2
   File: main.cpp
  
  Demonstrates the use of a for() loop. Lights multiple LEDs in sequence, then
- in reverse.
+ in reverse. Uses alternative approach to that used in ForLoopIteration.
  
  The circuit:
  
@@ -30,15 +30,15 @@
   
     while(true) { // Keep the lights going back and forth forever.
  
-        // Lighted bar will shift from 0 to 9 places.
-        for(int shift = 0; shift < 10; shift++) {
-            bar_graph = (1<<shift); // A 1 shifted over "shift" places.
+        // 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 shift = 9; shift >= 0; shift--) {
-            bar_graph = (1 << shift); // A 1 shifted over "shift" places.
+        for(int lights = (1<<9); lights >= 1; lights = (lights >> 1)) {
+            bar_graph = lights; // Shifting rather than incrementing in for.
             wait(delay); // Pause
         }
     }