asking if the button is pressed

Dependencies:   Hotboards_buttons mbed

Committer:
RomanValenciaP
Date:
Mon Feb 29 20:57:45 2016 +0000
Revision:
0:c0ab15e9fafd
first release - recquires approval

Who changed what in which revision?

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