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@4:aa100356f053, 2017-03-30 (annotated)
- Committer:
- CSTritt
- Date:
- Thu Mar 30 23:59:22 2017 +0000
- Revision:
- 4:aa100356f053
- Parent:
- 2:5682a72277ed
No functional changes. Commited and publish to correct repository problems.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
CSTritt | 0:8ed2f4a2a2fe | 1 | /* |
CSTritt | 2:5682a72277ed | 2 | Project: Nightlight2 |
CSTritt | 0:8ed2f4a2a2fe | 3 | File: main.cpp |
CSTritt | 0:8ed2f4a2a2fe | 4 | |
CSTritt | 2:5682a72277ed | 5 | See Word document. |
CSTritt | 0:8ed2f4a2a2fe | 6 | |
CSTritt | 0:8ed2f4a2a2fe | 7 | Written by: Dr. C. S. Tritt |
CSTritt | 0:8ed2f4a2a2fe | 8 | Created: 3/26/17 (v. 1.0) |
CSTritt | 0:8ed2f4a2a2fe | 9 | |
CSTritt | 0:8ed2f4a2a2fe | 10 | */ |
CSTritt | 0:8ed2f4a2a2fe | 11 | #include "mbed.h" |
CSTritt | 0:8ed2f4a2a2fe | 12 | |
CSTritt | 2:5682a72277ed | 13 | const int HIGH = 1; // Inclusion is optional, but makes code more readable. |
CSTritt | 2:5682a72277ed | 14 | const int LOW = 0; // Inclusion is optional, but makes code more readable. |
CSTritt | 2:5682a72277ed | 15 | |
CSTritt | 2:5682a72277ed | 16 | const float br_min = 0.070; // Read from serial stream and enter. |
CSTritt | 2:5682a72277ed | 17 | const float br_max = 0.430; // Read from serial stream and enter. |
CSTritt | 2:5682a72277ed | 18 | const float k_1 = 0.7; // These values work well... |
CSTritt | 2:5682a72277ed | 19 | const float k_2 = 0.5; |
CSTritt | 2:5682a72277ed | 20 | const float k_3 = 0.3; |
CSTritt | 2:5682a72277ed | 21 | const float all_off = br_min + k_1*(br_max - br_min); // Thresholds... All off. |
CSTritt | 2:5682a72277ed | 22 | const float blu_grn = br_min + k_2*(br_max - br_min); // Blue-green fade. |
CSTritt | 2:5682a72277ed | 23 | const float grn_red = br_min + k_3*(br_max - br_min); // Green-red fade. |
CSTritt | 0:8ed2f4a2a2fe | 24 | |
CSTritt | 2:5682a72277ed | 25 | AnalogIn photocell(A1); // Create object for photocell. |
CSTritt | 2:5682a72277ed | 26 | PwmOut red(D9), grn(D10), blu(D11); // Create objects for LED connected pins. |
CSTritt | 0:8ed2f4a2a2fe | 27 | |
CSTritt | 0:8ed2f4a2a2fe | 28 | int main() { |
CSTritt | 2:5682a72277ed | 29 | float brightness; // 0 to 1 max. range. Larger indicates brighter light. |
CSTritt | 0:8ed2f4a2a2fe | 30 | |
CSTritt | 2:5682a72277ed | 31 | printf("\nSmart nightlight example\n"); // ID software. |
CSTritt | 0:8ed2f4a2a2fe | 32 | |
CSTritt | 0:8ed2f4a2a2fe | 33 | while(true) { |
CSTritt | 2:5682a72277ed | 34 | brightness = photocell; // Read light level (0 to 1). |
CSTritt | 2:5682a72277ed | 35 | printf("Value = %f\n", brightness); // Send as text via serial port. |
CSTritt | 2:5682a72277ed | 36 | if (brightness > all_off) { // Bright light. All LEDs off. |
CSTritt | 2:5682a72277ed | 37 | red = LOW; |
CSTritt | 2:5682a72277ed | 38 | grn = LOW; |
CSTritt | 2:5682a72277ed | 39 | blu = LOW; |
CSTritt | 2:5682a72277ed | 40 | } |
CSTritt | 2:5682a72277ed | 41 | else if (brightness > blu_grn) { // Blue to green fade. |
CSTritt | 2:5682a72277ed | 42 | red = LOW; |
CSTritt | 2:5682a72277ed | 43 | grn = (all_off - brightness)/(all_off - blu_grn); |
CSTritt | 2:5682a72277ed | 44 | blu = 1.0f - grn; |
CSTritt | 0:8ed2f4a2a2fe | 45 | } |
CSTritt | 2:5682a72277ed | 46 | else if (brightness > grn_red) { // Green to red fade. |
CSTritt | 2:5682a72277ed | 47 | red = (blu_grn - brightness)/(blu_grn - grn_red); |
CSTritt | 2:5682a72277ed | 48 | grn = 1.0f - red; |
CSTritt | 2:5682a72277ed | 49 | blu = LOW; |
CSTritt | 0:8ed2f4a2a2fe | 50 | } |
CSTritt | 2:5682a72277ed | 51 | else { // Red on full intensity. |
CSTritt | 2:5682a72277ed | 52 | red = HIGH; |
CSTritt | 2:5682a72277ed | 53 | grn = LOW; |
CSTritt | 2:5682a72277ed | 54 | blu = LOW; |
CSTritt | 2:5682a72277ed | 55 | } |
CSTritt | 2:5682a72277ed | 56 | wait(0.1); // Delay 100 ms |
CSTritt | 0:8ed2f4a2a2fe | 57 | } |
CSTritt | 4:aa100356f053 | 58 | } |