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).
main.cpp
- Committer:
- CSTritt
- Date:
- 2017-04-06
- Revision:
- 5:f723b267eae3
- Parent:
- 4:aa100356f053
File content as of revision 5:f723b267eae3:
/* Project: Nightlight3 File: main.cpp See Word document. Common anode version (makes equations odd). Written by: Dr. C. S. Tritt Created: 3/26/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. int main() { float brightness; // 0 to 1 max. range. Larger indicates brighter light. printf("\nSmart nightlight example\n"); // ID software. 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; } 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 } }