First version. Demo of PWM setup and speaker operation. Fine grain control of the PWM parameters is an advantage Mbed has over the Arduino environment.
Diff: main.cpp
- Revision:
- 109:86a37f0a397f
- Parent:
- 108:eee3167b25b4
- Child:
- 110:e80444a6fe3a
--- a/main.cpp Tue Sep 21 02:00:55 2021 +0000 +++ b/main.cpp Tue Oct 05 05:00:06 2021 +0000 @@ -1,31 +1,45 @@ /* - Project: BinaryCount + Project: Binary Counter Improved (while loop) Version File: main.cpp - Displays 8 bit binary count on bar graph display. + Displays 8-bit binary count on bar graph display. This version uses a while + loop which provides a much better starting point for an Up/Down counter. + + Error in pow function use corrected in v. 1.0. + Note: I tried to use count for the loop counter but this resulted in an + ambiguous name error. + Written by: Dr. C. S. Tritt - Created: 9/20/21 (v. 1.2) + Created: 9/27/21 (v. 1.0) */ #include "mbed.h" -BusOut barGraph(D2, D3, D4, D5, D6, D7, D8, D9); // Create BusOut object. +DigitalIn uB(USER_BUTTON); // Button is active low (up = 1, down = 0). +const int bits = 4; // Number of bits to use. +const int c_max = pow(2, bits) - 1; // Maximum count. pow is type double. +const int sleepTime = 100; // Sleep time in milliseconds. +int myCount = 0; // Count to be displayed. Loop counter. +BusOut barGraph(D2, D3, D4, D5, D6, D7, D8, D9); // The display. 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. + ThisThread::sleep_for(400); // For 0.4 seconds. + + barGraph = myCount; // Initialize the display to myCount. + // 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 (myCount >= 0 && myCount <= c_max) { + barGraph = myCount; // Set display to myCount. + ThisThread::sleep_for(sleepTime); // Display value for 0.1 seconds. + myCount++; } + myCount = 0; } } \ No newline at end of file