Charles Tritt / Mbed OS 21_ForShift_v5
Revision:
110:b148a36c9833
Parent:
108:eee3167b25b4
Child:
111:d1994a385ab2
--- a/main.cpp	Wed Sep 22 13:19:31 2021 +0000
+++ b/main.cpp	Mon Oct 11 01:27:18 2021 +0000
@@ -1,31 +1,42 @@
 /*
-    Project: BinaryCount
-    File: main.cpp
-    
-    Displays 8 bit binary count on bar graph display.
-    
-    Written by: Dr. C. S. Tritt
-    Created: 9/20/21 (v. 1.2)
+  Project: ForLoopIteration
+  File: main.cpp
+ 
+ Demonstrates the use of a for() loop. Lights indivdiual LEDs of a bargraph
+ display in sequence, then in reverse order.
+ 
+ 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)
+ 
+This example code is in the public domain.
 */
+ 
 #include "mbed.h"
-
-BusOut barGraph(D2, D3, D4, D5, D6, D7, D8, D9);  // Create BusOut object.
-
+ 
+const int DELAY = 100;; // mS wait time.
+ 
+BusOut bar_graph(D3,D4,D5,D6,D7,D8,D9,D10,D11,D12);
+ 
 int main() {
-    // Test the wiring.
-    barGraph = 0;  // All bars off (base 10).
-    ThisThread::sleep_for(400); // For 0.4 seconds.
-    barGraph = 0b01010101;  // Odd bars on (binary).
-    ThisThread::sleep_for(400); // Test even bars for 0.4 seconds. 
-    barGraph = 0b10101010;  // Even bars on (binary).   
-    ThisThread::sleep_for(400); // Test even bars for 0.4 seconds.
-    barGraph = 0xFF;  // All bars on. Hex.
-    ThisThread::sleep_for(400); // For 0.4 seconds.
-    // Enter main loop.   
-    while(true) {
-        for (int i = 0; i < 256; i++) { // Add one to count.
-            barGraph = i;  // Copy the count to the bargraph.
-            ThisThread::sleep_for(100);  // Display the value for 0.1 seconds.
+  
+    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.
+            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.
+            ThisThread::sleep_for(DELAY); // Pause
         }
     }
 }
\ No newline at end of file