Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of BinaryCount by
Diff: main.cpp
- Revision:
- 6:0c1ab2c11252
- Parent:
- 5:f723b267eae3
- Child:
- 7:031078522195
diff -r f723b267eae3 -r 0c1ab2c11252 main.cpp --- a/main.cpp Thu Apr 06 18:09:47 2017 +0000 +++ b/main.cpp Wed Sep 13 10:32:11 2017 +0000 @@ -1,58 +1,29 @@ /* - Project: Nightlight3 + Project: BinaryCount File: main.cpp - See Word document. Common anode version (makes equations odd). + Displays 4 bit binary count on bar graph display. Written by: Dr. C. S. Tritt - Created: 3/26/17 (v. 1.0) - + Created: 9/13/17 (v. 1.0) */ #include "mbed.h" -const int HIGH = 1; // Inclusion is optional, but makes code more readable. -const int LOW = 0; // Inclusion is optional, but makes code more readable. - -const float br_min = 0.60; // Read from serial stream and enter. -const float br_max = 0.89; // Read from serial stream and enter. -const float k_1 = 0.7; // These values work well... -const float k_2 = 0.5; -const float k_3 = 0.3; -const float all_off = br_min + k_1*(br_max - br_min); // Thresholds... All off. -const float blu_grn = br_min + k_2*(br_max - br_min); // Blue-green fade. -const float grn_red = br_min + k_3*(br_max - br_min); // Green-red fade. - -AnalogIn photocell(A0); // Create object for photocell. -PwmOut red(D9), grn(D10), blu(D11); // Create objects for LED connected pins. +BusOut barGraph(D5, D4, D3, D2); // Create barGraph BusOut object. +DigitalOut pinD7(D7); // Needed to force this pin off. int main() { - float brightness; // 0 to 1 max. range. Larger indicates brighter light. + + printf("Binary count example\n"); // ID software. - printf("\nSmart nightlight example\n"); // ID software. + pinD7 = 0; // Force pin D7 off. This shouldn't be necessary. + barGraph = 0; // Light top bar. while(true) { - brightness = photocell; // Read light level (0 to 1). - printf("Value = %f\n", brightness); // Send as text via serial port. - if (brightness > all_off) { // Bright light. All LEDs off. - red = HIGH; - grn = HIGH; - blu = HIGH; + barGraph = barGraph + 1; // Add one to count. + wait(1.0); + if (barGraph == 15) { + barGraph = 0; } - else if (brightness > blu_grn) { // Blue to green fade. - red = HIGH; - grn = 1.0f - (all_off - brightness)/(all_off - blu_grn); - blu = 1.0f - grn; - } - else if (brightness > grn_red) { // Green to red fade. - red = 1.0f - (blu_grn - brightness)/(blu_grn - grn_red); - grn = 1.0f - red; - blu = HIGH; - } - else { // Red on full intensity. - red = LOW; - grn = HIGH; - blu = HIGH; - } - wait(0.1); // Delay 100 ms } } \ No newline at end of file