Toggle led ON pin PA_5 (LED1) when the button on pin PB_4 is pressed

Dependencies:   Hotboards_buttons mbed

Committer:
Hotboards
Date:
Thu Mar 03 20:25:07 2016 +0000
Revision:
0:da4086004592
first release

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Hotboards 0:da4086004592 1
Hotboards 0:da4086004592 2 /*
Hotboards 0:da4086004592 3 * Toggle led ON pin PA_5 (LED1) when the button on pin PB_4 is pressed
Hotboards 0:da4086004592 4 */
Hotboards 0:da4086004592 5
Hotboards 0:da4086004592 6 #include "mbed.h"
Hotboards 0:da4086004592 7 #include "Hotboards_buttons.h"
Hotboards 0:da4086004592 8
Hotboards 0:da4086004592 9 //Creates a single button object, when the button is pressed it gives you
Hotboards 0:da4086004592 10 //a LOW(0) value because it works with pull-ups.
Hotboards 0:da4086004592 11 Hotboards_buttons btn( PB_4 );
Hotboards 0:da4086004592 12 //If your buttons gives you a HIGH(1) value when is pressed, then we need
Hotboards 0:da4086004592 13 //to create the button object with and extra parameter:
Hotboards 0:da4086004592 14 //Hotboards_buttons btn( PB_4 , 1 ); in any case the functions will return
Hotboards 0:da4086004592 15 //a HIGH(1) value any time the button is pressed
Hotboards 0:da4086004592 16
Hotboards 0:da4086004592 17 //To our example we will use the led on the nucleo board
Hotboards 0:da4086004592 18 DigitalOut nucleoLed( LED1 );
Hotboards 0:da4086004592 19
Hotboards 0:da4086004592 20 int main()
Hotboards 0:da4086004592 21 {
Hotboards 0:da4086004592 22 while(1)
Hotboards 0:da4086004592 23 {
Hotboards 0:da4086004592 24 //The moment when the button is pressed the function will return a HIGH(1)
Hotboards 0:da4086004592 25 //value, it doesn`t matter if your button is configured with pull-ups(LOW)
Hotboards 0:da4086004592 26 //or pull-downs(HIGH)
Hotboards 0:da4086004592 27 if( btn.isPressed( ) )
Hotboards 0:da4086004592 28 {
Hotboards 0:da4086004592 29 //Toggle led on the nucleo board
Hotboards 0:da4086004592 30 nucleoLed = !nucleoLed;
Hotboards 0:da4086004592 31 }
Hotboards 0:da4086004592 32 }
Hotboards 0:da4086004592 33 }