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

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*
00002     Project: 21_BC_Improved_v5 (Improved Binary Counter (while loop) Version)
00003     File: main.cpp
00004     
00005     Displays 8-bit binary count on bar graph display. This version uses a while 
00006     loop which provides a much better starting point for an Up/Down counter.
00007     
00008     Error in pow function use corrected in v. 1.0.
00009     
00010     Note: I tried to use count for the loop counter but this resulted in an 
00011     ambiguous name error.
00012         
00013     Written by: Dr. C. S. Tritt
00014     Created: 10/5/21 (v. 1.1)
00015 */
00016 #include "mbed.h"
00017 
00018 DigitalIn uB(USER_BUTTON); // Button is active low (up = 1, down = 0).
00019 const int bits = 8; // Number of bits to use.
00020 const int c_max = pow(2, bits) - 1; // Maximum count. pow is type double.
00021 const int sleepTime = 100; // Sleep time in milliseconds.
00022 int myCount = 0; // Count to be displayed. Loop counter.
00023 BusOut barGraph(D2, D3, D4, D5, D6, D7, D8, D9);  // The display.
00024 
00025 int main() {
00026     // Test the wiring.
00027     ThisThread::sleep_for(400); // For 0.4 seconds.
00028     barGraph = 0b01010101;  // Odd bars on (binary).
00029     ThisThread::sleep_for(400); // Test even bars for 0.4 seconds. 
00030     barGraph = 0b10101010;  // Even bars on (binary).   
00031     ThisThread::sleep_for(400); // Test even bars for 0.4 seconds.
00032     barGraph = 0xFF;  // All bars on. Hex.
00033     ThisThread::sleep_for(400); // For 0.4 seconds.  
00034 
00035     barGraph = myCount; // Initialize the display to myCount.
00036     // Enter main loop. 
00037     while(true) {
00038         while (myCount >= 0 && myCount <= c_max) {
00039             barGraph = myCount; // Set display to myCount.
00040             ThisThread::sleep_for(sleepTime);  // Display value for 0.1 seconds.
00041             myCount++;
00042         }
00043         myCount = 0;
00044     }
00045 }