Lights up to 4 LEDs based on ambient light levels. Uses Grove Seeed Light Sensor
Fork of Seeed_Grove_Shield_Light_Sensor by
main.cpp@3:15c1934a8316, 2016-03-16 (annotated)
- Committer:
- clively6
- Date:
- Wed Mar 16 17:38:22 2016 +0000
- Revision:
- 3:15c1934a8316
- Parent:
- 2:76004e9cf0cc
test
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
screamer | 0:5ff78d2c85d1 | 1 | |
clively6 | 3:15c1934a8316 | 2 | /*Graduated Night light with Seed Grove Ambient Light sensor |
clively6 | 3:15c1934a8316 | 3 | Casey Lively |
clively6 | 3:15c1934a8316 | 4 | March 2016*/ |
clively6 | 3:15c1934a8316 | 5 | |
clively6 | 3:15c1934a8316 | 6 | //Read in ambient light sensor with mbed LPC1768 |
screamer | 0:5ff78d2c85d1 | 7 | #include "mbed.h" |
screamer | 0:5ff78d2c85d1 | 8 | |
clively6 | 3:15c1934a8316 | 9 | AnalogIn sensor(p20);// |
clively6 | 3:15c1934a8316 | 10 | DigitalOut led1(LED1); |
clively6 | 3:15c1934a8316 | 11 | DigitalOut led2(LED2); |
clively6 | 3:15c1934a8316 | 12 | DigitalOut led3(LED3); |
clively6 | 3:15c1934a8316 | 13 | DigitalOut led4(LED4); |
screamer | 0:5ff78d2c85d1 | 14 | |
screamer | 0:5ff78d2c85d1 | 15 | int main() { |
screamer | 1:d10e08a99894 | 16 | float val; |
clively6 | 3:15c1934a8316 | 17 | float threshold = .03; //User specified for night-light mode. Set to .03 for light measurement mode |
screamer | 0:5ff78d2c85d1 | 18 | while(1) { |
screamer | 1:d10e08a99894 | 19 | val = sensor.read(); |
sam_grove | 2:76004e9cf0cc | 20 | printf("Sensor reading: %2.2f - %2.2f\r\n", val, (float)(1023-val)*10/val); |
clively6 | 3:15c1934a8316 | 21 | |
clively6 | 3:15c1934a8316 | 22 | if(val <= threshold) |
clively6 | 3:15c1934a8316 | 23 | led4 = 1; |
clively6 | 3:15c1934a8316 | 24 | else |
clively6 | 3:15c1934a8316 | 25 | led4 = 0; |
clively6 | 3:15c1934a8316 | 26 | |
clively6 | 3:15c1934a8316 | 27 | if(val <= 0.1) |
clively6 | 3:15c1934a8316 | 28 | led3 = 1; |
clively6 | 3:15c1934a8316 | 29 | else |
clively6 | 3:15c1934a8316 | 30 | led3 = 0; |
clively6 | 3:15c1934a8316 | 31 | |
clively6 | 3:15c1934a8316 | 32 | if(val <= 0.35) |
clively6 | 3:15c1934a8316 | 33 | led2 = 1; |
clively6 | 3:15c1934a8316 | 34 | else |
clively6 | 3:15c1934a8316 | 35 | led2 = 0; |
clively6 | 3:15c1934a8316 | 36 | |
clively6 | 3:15c1934a8316 | 37 | if(val <= 0.55) |
clively6 | 3:15c1934a8316 | 38 | led1 = 1; |
clively6 | 3:15c1934a8316 | 39 | else |
clively6 | 3:15c1934a8316 | 40 | led1 = 0; |
clively6 | 3:15c1934a8316 | 41 | |
clively6 | 3:15c1934a8316 | 42 | |
clively6 | 3:15c1934a8316 | 43 | wait(0.1); |
clively6 | 3:15c1934a8316 | 44 | }//end while(1) |
clively6 | 3:15c1934a8316 | 45 | }//end Main |