Smart of night demonstration project. Color of light fades from blue to green to red as it gets darker.
Fork of Nightlight2 by
main.cpp
00001 /* 00002 Project: Nightlight2 00003 File: main.cpp 00004 00005 See Word document. 00006 00007 Written by: Dr. C. S. Tritt 00008 Created: 3/26/17 (v. 1.0) 00009 00010 */ 00011 #include "mbed.h" 00012 00013 const int HIGH = 1; // Inclusion is optional, but makes code more readable. 00014 const int LOW = 0; // Inclusion is optional, but makes code more readable. 00015 00016 const float br_min = 0.070; // Read from serial stream and enter. 00017 const float br_max = 0.430; // Read from serial stream and enter. 00018 const float k_1 = 0.7; // These values work well... 00019 const float k_2 = 0.5; 00020 const float k_3 = 0.3; 00021 const float all_off = br_min + k_1*(br_max - br_min); // Thresholds... All off. 00022 const float blu_grn = br_min + k_2*(br_max - br_min); // Blue-green fade. 00023 const float grn_red = br_min + k_3*(br_max - br_min); // Green-red fade. 00024 00025 AnalogIn photocell(A1); // Create object for photocell. 00026 PwmOut red(D9), grn(D10), blu(D11); // Create objects for LED connected pins. 00027 00028 int main() { 00029 float brightness; // 0 to 1 max. range. Larger indicates brighter light. 00030 00031 printf("\nSmart nightlight example\n"); // ID software. 00032 00033 while(true) { 00034 brightness = photocell; // Read light level (0 to 1). 00035 printf("Value = %f\n", brightness); // Send as text via serial port. 00036 if (brightness > all_off) { // Bright light. All LEDs off. 00037 red = LOW; 00038 grn = LOW; 00039 blu = LOW; 00040 } 00041 else if (brightness > blu_grn) { // Blue to green fade. 00042 red = LOW; 00043 grn = (all_off - brightness)/(all_off - blu_grn); 00044 blu = 1.0f - grn; 00045 } 00046 else if (brightness > grn_red) { // Green to red fade. 00047 red = (blu_grn - brightness)/(blu_grn - grn_red); 00048 grn = 1.0f - red; 00049 blu = LOW; 00050 } 00051 else { // Red on full intensity. 00052 red = HIGH; 00053 grn = LOW; 00054 blu = LOW; 00055 } 00056 wait(0.1); // Delay 100 ms 00057 } 00058 }
Generated on Mon Aug 1 2022 11:29:01 by 1.7.2