Charles Tritt / Mbed OS 21_ForShift2_v5
Committer:
CSTritt
Date:
Mon Oct 11 01:42:06 2021 +0000
Revision:
111:459fa5199128
Parent:
110:b148a36c9833
Child:
112:a34a2c4cc2cc
Initial version.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
CSTritt 107:61b9c99a4e27 1 /*
CSTritt 111:459fa5199128 2 Project: 21_ShiftFor2_v5
CSTritt 110:b148a36c9833 3 File: main.cpp
CSTritt 111:459fa5199128 4
CSTritt 110:b148a36c9833 5 Demonstrates the use of a for() loop. Lights indivdiual LEDs of a bargraph
CSTritt 111:459fa5199128 6 display in sequence, then in reverse order. This version demonstrates the use
CSTritt 111:459fa5199128 7 of the combination assignment operators. In general it is probably better to
CSTritt 111:459fa5199128 8 use the shift operators. This is the "Nightrider" effect.
CSTritt 111:459fa5199128 9
CSTritt 110:b148a36c9833 10 The circuit:
CSTritt 111:459fa5199128 11
CSTritt 110:b148a36c9833 12 Bargraph LEDs from pins D3 through D12 to ground via 330 Ohm resistors.
CSTritt 111:459fa5199128 13
CSTritt 110:b148a36c9833 14 Created 2006 by David A. Mellis
CSTritt 110:b148a36c9833 15 Modified 2011 by Tom Igoe
CSTritt 110:b148a36c9833 16 Modified for Nucleo / mbed 12 Aug 2017 by Sheila Ross
CSTritt 111:459fa5199128 17 Modified by C. S. Tritt 9/21/17 and 10/10/21 (v. 1.0, untested)
CSTritt 111:459fa5199128 18
CSTritt 110:b148a36c9833 19 This example code is in the public domain.
CSTritt 107:61b9c99a4e27 20 */
CSTritt 111:459fa5199128 21
Jonathan Austin 0:2757d7abb7d9 22 #include "mbed.h"
CSTritt 111:459fa5199128 23
CSTritt 110:b148a36c9833 24 const int DELAY = 100;; // mS wait time.
CSTritt 111:459fa5199128 25
CSTritt 110:b148a36c9833 26 BusOut bar_graph(D3,D4,D5,D6,D7,D8,D9,D10,D11,D12);
CSTritt 111:459fa5199128 27
CSTritt 111:459fa5199128 28 int main()
CSTritt 111:459fa5199128 29 {
CSTritt 111:459fa5199128 30
CSTritt 110:b148a36c9833 31 while(true) { // Keep the lights going back and forth forever.
CSTritt 111:459fa5199128 32
CSTritt 111:459fa5199128 33 int barLoc = 1; // Set inital bar location (value).
CSTritt 110:b148a36c9833 34 // Lighted bar will shift from 0 to 9 places.
CSTritt 110:b148a36c9833 35 for(int shift = 0; shift < 10; shift++) {
CSTritt 111:459fa5199128 36 barLoc *= 2; // Shift one bar left.
CSTritt 111:459fa5199128 37 bar_graph = barLoc; // Update display.
CSTritt 110:b148a36c9833 38 ThisThread::sleep_for(DELAY); // Pause
CSTritt 110:b148a36c9833 39 }
CSTritt 111:459fa5199128 40
CSTritt 110:b148a36c9833 41 // Now shift down from 9 to 0 places.
CSTritt 110:b148a36c9833 42 for(int shift = 9; shift >= 0; shift--) {
CSTritt 111:459fa5199128 43 barLoc /= 2; // Shift one bar rightt.
CSTritt 111:459fa5199128 44 bar_graph = barLoc; // Update display.
CSTritt 110:b148a36c9833 45 ThisThread::sleep_for(DELAY); // Pause
CSTritt 108:eee3167b25b4 46 }
CSTritt 108:eee3167b25b4 47 }
CSTritt 108:eee3167b25b4 48 }