Charles Tritt / Mbed OS 21_ForShift2_v5
Revision:
111:459fa5199128
Parent:
110:b148a36c9833
Child:
112:a34a2c4cc2cc
--- a/main.cpp	Mon Oct 11 01:27:18 2021 +0000
+++ b/main.cpp	Mon Oct 11 01:42:06 2021 +0000
@@ -1,41 +1,47 @@
 /*
-  Project: ForLoopIteration
+  Project: 21_ShiftFor2_v5
   File: main.cpp
- 
+
  Demonstrates the use of a for() loop. Lights indivdiual LEDs of a bargraph
- display in sequence, then in reverse order.
- 
+ display in sequence, then in reverse order. This version demonstrates the use
+ of the combination assignment operators. In general it is probably better to 
+ use the shift operators. This is the "Nightrider" effect.
+
  The circuit:
- 
+
     Bargraph LEDs from pins D3 through D12 to ground via 330 Ohm resistors.
- 
+
  Created 2006  by David A. Mellis
  Modified 2011  by Tom Igoe
  Modified for Nucleo / mbed 12 Aug 2017  by Sheila Ross
- Modified by C. S. Tritt 9/21/17 and 10/10/21 (v. 1.1, untested)
- 
+ Modified by C. S. Tritt 9/21/17 and 10/10/21 (v. 1.0, untested)
+
 This example code is in the public domain.
 */
- 
+
 #include "mbed.h"
- 
+
 const int DELAY = 100;; // mS wait time.
- 
+
 BusOut bar_graph(D3,D4,D5,D6,D7,D8,D9,D10,D11,D12);
- 
-int main() {
-  
+
+int main()
+{
+
     while(true) { // Keep the lights going back and forth forever.
- 
+
+        int barLoc = 1; // Set inital bar location (value).
         // 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.
+            barLoc *= 2; // Shift one bar left.
+            bar_graph = barLoc; // Update display.
             ThisThread::sleep_for(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.
+            barLoc /= 2; // Shift one bar rightt.
+            bar_graph = barLoc; // Update display.
             ThisThread::sleep_for(DELAY); // Pause
         }
     }