reading two buttons when are pressed

Dependencies:   Hotboards_buttons mbed

Committer:
RomanValenciaP
Date:
Mon Feb 29 20:55:31 2016 +0000
Revision:
0:6889ada60471
first release - recquires approval

Who changed what in which revision?

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