![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Leds get brighter as it gets darker outside
main.cpp@0:00bceb1fa958, 2015-09-16 (annotated)
- Committer:
- destradafilm
- Date:
- Wed Sep 16 16:24:20 2015 +0000
- Revision:
- 0:00bceb1fa958
HW 4.1 q3
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
destradafilm | 0:00bceb1fa958 | 1 | #include "mbed.h" |
destradafilm | 0:00bceb1fa958 | 2 | |
destradafilm | 0:00bceb1fa958 | 3 | #define PROGNAME "DEstra_HW 4_1 QUESTION 3" |
destradafilm | 0:00bceb1fa958 | 4 | #define DATATIME 0.5 |
destradafilm | 0:00bceb1fa958 | 5 | |
destradafilm | 0:00bceb1fa958 | 6 | #define PRINTDEBUG |
destradafilm | 0:00bceb1fa958 | 7 | |
destradafilm | 0:00bceb1fa958 | 8 | AnalogIn LightSensor(PTE22); // define light sensor |
destradafilm | 0:00bceb1fa958 | 9 | PwmOut redLed(LED_RED); |
destradafilm | 0:00bceb1fa958 | 10 | PwmOut greenLed(LED_GREEN); |
destradafilm | 0:00bceb1fa958 | 11 | |
destradafilm | 0:00bceb1fa958 | 12 | Serial pc(USBTX, USBRX); |
destradafilm | 0:00bceb1fa958 | 13 | |
destradafilm | 0:00bceb1fa958 | 14 | int main() |
destradafilm | 0:00bceb1fa958 | 15 | { |
destradafilm | 0:00bceb1fa958 | 16 | float lightVal; |
destradafilm | 0:00bceb1fa958 | 17 | unsigned short lightWord; |
destradafilm | 0:00bceb1fa958 | 18 | |
destradafilm | 0:00bceb1fa958 | 19 | pc.printf(PROGNAME); |
destradafilm | 0:00bceb1fa958 | 20 | |
destradafilm | 0:00bceb1fa958 | 21 | while(true){ |
destradafilm | 0:00bceb1fa958 | 22 | lightVal = LightSensor.read(); |
destradafilm | 0:00bceb1fa958 | 23 | lightWord = LightSensor.read_u16(); |
destradafilm | 0:00bceb1fa958 | 24 | redLed = 1.0 - lightVal; // redLed shines brighter as it gets darker outside |
destradafilm | 0:00bceb1fa958 | 25 | greenLed = 1.0 - lightVal; |
destradafilm | 0:00bceb1fa958 | 26 | |
destradafilm | 0:00bceb1fa958 | 27 | #ifdef PRINTDEBUG |
destradafilm | 0:00bceb1fa958 | 28 | pc.printf("LS => %1.3f %5 \r\n", (1.0 - lightVal), lightWord); |
destradafilm | 0:00bceb1fa958 | 29 | pc.printf("other => %\r\n", lightWord); |
destradafilm | 0:00bceb1fa958 | 30 | #endif |
destradafilm | 0:00bceb1fa958 | 31 | wait(DATATIME); |
destradafilm | 0:00bceb1fa958 | 32 | } |
destradafilm | 0:00bceb1fa958 | 33 | } |