Demo of LED lighting effects using PWM and wait for time delays. Pins are setup for LPC1768 platform’s LEDs. For complete information, see http://developer.mbed.org/users/4180_1/notebook/led-lighting-effects-for-modelers/
main.cpp@0:a903b3cf5317, 2014-11-28 (annotated)
- Committer:
- 4180_1
- Date:
- Fri Nov 28 17:59:16 2014 +0000
- Revision:
- 0:a903b3cf5317
ver 1.0
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
4180_1 | 0:a903b3cf5317 | 1 | #include "mbed.h" |
4180_1 | 0:a903b3cf5317 | 2 | //LED Railroad Crossing Lighting Effect |
4180_1 | 0:a903b3cf5317 | 3 | PwmOut myled(LED1); |
4180_1 | 0:a903b3cf5317 | 4 | PwmOut myled2(LED4); |
4180_1 | 0:a903b3cf5317 | 5 | //Use a PWM output to enable dimming |
4180_1 | 0:a903b3cf5317 | 6 | //1.0 is full on, 0.0 off, 0.5 50% on |
4180_1 | 0:a903b3cf5317 | 7 | |
4180_1 | 0:a903b3cf5317 | 8 | //function to simulate incandescent bulb warm up |
4180_1 | 0:a903b3cf5317 | 9 | //and cool down cycles |
4180_1 | 0:a903b3cf5317 | 10 | void incandescent_bulb(PwmOut led, float on_time = 0.1, |
4180_1 | 0:a903b3cf5317 | 11 | float warm_time = 0.2) |
4180_1 | 0:a903b3cf5317 | 12 | { |
4180_1 | 0:a903b3cf5317 | 13 | //LED warm up effect using PWM |
4180_1 | 0:a903b3cf5317 | 14 | for(int i=0; i<10; i++) { |
4180_1 | 0:a903b3cf5317 | 15 | led = i/10.0; |
4180_1 | 0:a903b3cf5317 | 16 | wait(warm_time/10.0); |
4180_1 | 0:a903b3cf5317 | 17 | } |
4180_1 | 0:a903b3cf5317 | 18 | //LED at full brightness level |
4180_1 | 0:a903b3cf5317 | 19 | led = 1.0; |
4180_1 | 0:a903b3cf5317 | 20 | wait(on_time); |
4180_1 | 0:a903b3cf5317 | 21 | //LED cool down effect using PWM |
4180_1 | 0:a903b3cf5317 | 22 | for(int i=9; i>0; i--) { |
4180_1 | 0:a903b3cf5317 | 23 | led = i/10.0; |
4180_1 | 0:a903b3cf5317 | 24 | wait(warm_time/10.0); |
4180_1 | 0:a903b3cf5317 | 25 | } |
4180_1 | 0:a903b3cf5317 | 26 | //LED off |
4180_1 | 0:a903b3cf5317 | 27 | led = 0.0; |
4180_1 | 0:a903b3cf5317 | 28 | } |
4180_1 | 0:a903b3cf5317 | 29 | |
4180_1 | 0:a903b3cf5317 | 30 | int main() |
4180_1 | 0:a903b3cf5317 | 31 | { |
4180_1 | 0:a903b3cf5317 | 32 | while(1) { |
4180_1 | 0:a903b3cf5317 | 33 | incandescent_bulb(myled); |
4180_1 | 0:a903b3cf5317 | 34 | incandescent_bulb(myled2); |
4180_1 | 0:a903b3cf5317 | 35 | } |
4180_1 | 0:a903b3cf5317 | 36 | } |