LEDs and user button example.

Dependencies:   mbed

Committer:
bcostm
Date:
Tue Sep 26 12:55:12 2017 +0000
Revision:
1:8932d9db4aaf
Parent:
0:b385f231a65a
Add mbed library v151

Who changed what in which revision?

UserRevisionLine numberNew contents of line
arostm 0:b385f231a65a 1 #include "mbed.h"
bcostm 1:8932d9db4aaf 2
arostm 0:b385f231a65a 3 DigitalOut myled1(LED1);
arostm 0:b385f231a65a 4 DigitalOut myled2(LED2);
arostm 0:b385f231a65a 5 DigitalOut myled3(LED3);
bcostm 1:8932d9db4aaf 6
arostm 0:b385f231a65a 7 InterruptIn mybutton(USER_BUTTON);
bcostm 1:8932d9db4aaf 8
arostm 0:b385f231a65a 9 double tempo = 0.2; //time to wait
bcostm 1:8932d9db4aaf 10
bcostm 1:8932d9db4aaf 11 void changetempo()
bcostm 1:8932d9db4aaf 12 {
arostm 0:b385f231a65a 13 if(tempo == 0.2) // If leds have low frequency
arostm 0:b385f231a65a 14 tempo = 0.1; // Set the fast frequency
arostm 0:b385f231a65a 15 else // If les have fast frequency
arostm 0:b385f231a65a 16 tempo = 0.2; // Set the low frequency
arostm 0:b385f231a65a 17 }
bcostm 1:8932d9db4aaf 18
bcostm 1:8932d9db4aaf 19 int main()
bcostm 1:8932d9db4aaf 20 {
arostm 0:b385f231a65a 21 myled1 = 0; //LED1 is OFF
arostm 0:b385f231a65a 22 myled2 = 0; //LED2 is OFF
arostm 0:b385f231a65a 23 myled3 = 0; //LED3 is OFF
bcostm 1:8932d9db4aaf 24
arostm 0:b385f231a65a 25 mybutton.fall(&changetempo); //Interrupt to change tempo
bcostm 1:8932d9db4aaf 26
arostm 0:b385f231a65a 27 while(1) {
arostm 0:b385f231a65a 28 myled1 = 1; // LED2 is ON
arostm 0:b385f231a65a 29 wait(tempo); // wait tempo
arostm 0:b385f231a65a 30 myled1 = 0; // LED2 is OFF
arostm 0:b385f231a65a 31 myled2 = 1; // LED1 is ON
arostm 0:b385f231a65a 32 wait(tempo); // wait tempo
arostm 0:b385f231a65a 33 myled2 = 0; // LED1 is OFF
arostm 0:b385f231a65a 34 myled3 = 1; // LED3 is ON
arostm 0:b385f231a65a 35 wait(tempo); // wait tempo
arostm 0:b385f231a65a 36 myled3 = 0; // LED3 is OFF
arostm 0:b385f231a65a 37 }
arostm 0:b385f231a65a 38 }