LEDs/Button example.
Dependencies: mbed
main.cpp@3:50a3fc3bce95, 2017-05-29 (annotated)
- Committer:
- bcostm
- Date:
- Mon May 29 13:14:37 2017 +0000
- Revision:
- 3:50a3fc3bce95
- Parent:
- 2:603a8ac68090
Correction in main.cpp
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bcostm | 0:2bd4c0051d9a | 1 | #include "mbed.h" |
bcostm | 0:2bd4c0051d9a | 2 | |
arostm | 1:d343cf676574 | 3 | DigitalOut myled1(LED1); |
arostm | 1:d343cf676574 | 4 | DigitalOut myled2(LED2); |
arostm | 1:d343cf676574 | 5 | DigitalOut myled3(LED3); |
arostm | 1:d343cf676574 | 6 | DigitalOut myled4(LED4); |
arostm | 1:d343cf676574 | 7 | |
arostm | 1:d343cf676574 | 8 | InterruptIn mybutton(USER_BUTTON); |
arostm | 1:d343cf676574 | 9 | |
bcostm | 3:50a3fc3bce95 | 10 | double tempo = 0.3; // LED blinking delay |
arostm | 1:d343cf676574 | 11 | |
bcostm | 2:603a8ac68090 | 12 | // Change LEDs blinking frequency |
bcostm | 2:603a8ac68090 | 13 | void change_blinking_frequency() { |
bcostm | 2:603a8ac68090 | 14 | if (tempo == 0.3) // If leds have low frequency |
bcostm | 2:603a8ac68090 | 15 | tempo = 0.1; // Set the fast frequency |
bcostm | 2:603a8ac68090 | 16 | else // If leds have fast frequency |
bcostm | 2:603a8ac68090 | 17 | tempo = 0.3; // Set the low frequency |
bcostm | 0:2bd4c0051d9a | 18 | } |
arostm | 1:d343cf676574 | 19 | |
arostm | 1:d343cf676574 | 20 | int main() { |
bcostm | 2:603a8ac68090 | 21 | // All LEDs are OFF |
bcostm | 2:603a8ac68090 | 22 | myled1 = 0; |
bcostm | 2:603a8ac68090 | 23 | myled2 = 0; |
bcostm | 2:603a8ac68090 | 24 | myled3 = 0; |
bcostm | 2:603a8ac68090 | 25 | myled4 = 0; |
arostm | 1:d343cf676574 | 26 | |
bcostm | 2:603a8ac68090 | 27 | // Change LEDs blinking frequency when button is pressed |
bcostm | 2:603a8ac68090 | 28 | mybutton.fall(&change_blinking_frequency); |
arostm | 1:d343cf676574 | 29 | |
arostm | 1:d343cf676574 | 30 | while(1) { |
arostm | 1:d343cf676574 | 31 | myled2 = 1; // LED2 is ON |
arostm | 1:d343cf676574 | 32 | wait(tempo); // wait tempo |
arostm | 1:d343cf676574 | 33 | myled2 = 0; // LED2 is OFF |
arostm | 1:d343cf676574 | 34 | myled1 = 1; // LED1 is ON |
arostm | 1:d343cf676574 | 35 | wait(tempo); // wait tempo |
arostm | 1:d343cf676574 | 36 | myled1 = 0; // LED1 is OFF |
arostm | 1:d343cf676574 | 37 | myled3 = 1; // LED3 is ON |
arostm | 1:d343cf676574 | 38 | wait(tempo); // wait tempo |
arostm | 1:d343cf676574 | 39 | myled3 = 0; // LED3 is OFF |
arostm | 1:d343cf676574 | 40 | myled4 = 1; // LED4 is ON |
arostm | 1:d343cf676574 | 41 | wait(tempo); // wait tempo |
arostm | 1:d343cf676574 | 42 | myled4 = 0; // LED4 is OFF |
arostm | 1:d343cf676574 | 43 | } |
arostm | 1:d343cf676574 | 44 | } |