Common anode LED version of my Smart Nightlight program. For use at the Adrift faculty development activity.
Fork of Nightlight2 by
This is my smart nightlight demo code. This version is for common anode LEDs (uses negative logic).
Revision 5:f723b267eae3, committed 2017-04-06
- Comitter:
- CSTritt
- Date:
- Thu Apr 06 18:09:47 2017 +0000
- Parent:
- 4:aa100356f053
- Commit message:
- Smart Nightlight program modified for common anode LED.
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/main.cpp Thu Mar 30 23:59:22 2017 +0000 +++ b/main.cpp Thu Apr 06 18:09:47 2017 +0000 @@ -1,8 +1,8 @@ /* - Project: Nightlight2 + Project: Nightlight3 File: main.cpp - See Word document. + See Word document. Common anode version (makes equations odd). Written by: Dr. C. S. Tritt Created: 3/26/17 (v. 1.0) @@ -13,8 +13,8 @@ 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.070; // Read from serial stream and enter. -const float br_max = 0.430; // Read from serial stream and enter. +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; @@ -22,7 +22,7 @@ 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(A1); // Create object for photocell. +AnalogIn photocell(A0); // Create object for photocell. PwmOut red(D9), grn(D10), blu(D11); // Create objects for LED connected pins. int main() { @@ -34,24 +34,24 @@ 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 = LOW; - grn = LOW; - blu = LOW; + red = HIGH; + grn = HIGH; + blu = HIGH; } else if (brightness > blu_grn) { // Blue to green fade. - red = LOW; - grn = (all_off - brightness)/(all_off - blu_grn); + 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 = (blu_grn - brightness)/(blu_grn - grn_red); + red = 1.0f - (blu_grn - brightness)/(blu_grn - grn_red); grn = 1.0f - red; - blu = LOW; + blu = HIGH; } else { // Red on full intensity. - red = HIGH; - grn = LOW; - blu = LOW; + red = LOW; + grn = HIGH; + blu = HIGH; } wait(0.1); // Delay 100 ms }