Charles Tritt / Mbed OS 21_ForShift2_v5
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*
00002   Project: 21_ShiftFor2_v5
00003   File: main.cpp
00004 
00005  Demonstrates the use of a for() loop. Lights indivdiual LEDs of a bargraph
00006  display in sequence, then in reverse order. This version demonstrates the use
00007  of the combination assignment operators. In general it is probably better to 
00008  use the shift operators. This is the "Nightrider" effect.
00009  
00010  This version has a minor glitch (blink) when bar is at left end of bar.
00011 
00012  The circuit:
00013 
00014     Bargraph LEDs from pins D3 through D12 to ground via 330 Ohm resistors.
00015 
00016  Created 2006  by David A. Mellis
00017  Modified 2011  by Tom Igoe
00018  Modified for Nucleo / mbed 12 Aug 2017  by Sheila Ross
00019  Modified by C. S. Tritt 9/21/17 and 10/10/21 (v. 1.1)
00020 
00021 This example code is in the public domain.
00022 */
00023 
00024 #include "mbed.h"
00025 
00026 const int DELAY = 100;; // mS wait time.
00027 
00028 BusOut bar_graph(D2,D3,D4,D5,D6,D7,D8,D9,D10,D11);
00029 
00030 int main()
00031 {
00032 
00033     while(true) { // Keep the lights going back and forth forever.
00034 
00035         int barLoc = 1; // Set inital bar location (value).
00036         // Lighted bar will shift from 0 to 9 places.
00037         for(int shift = 0; shift < 10; shift++) {
00038             barLoc *= 2; // Shift one bar left.
00039             bar_graph = barLoc; // Update display.
00040             ThisThread::sleep_for(DELAY); // Pause
00041         }
00042 
00043         // Now shift down from 9 to 0 places.
00044         for(int shift = 9; shift >= 0; shift--) {
00045             barLoc /= 2; // Shift one bar rightt.
00046             bar_graph = barLoc; // Update display.
00047             ThisThread::sleep_for(DELAY); // Pause
00048         }
00049     }
00050 }