![](/media/cache/profiles/abed4d6893be8b9687eab0234e0cac09.jpg.50x50_q85.jpg)
reading a button state
Dependencies: Hotboards_buttons mbed
Diff: main.cpp
- Revision:
- 0:538950d8a034
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Mon Feb 29 20:59:52 2016 +0000 @@ -0,0 +1,40 @@ + +/* + * The simplest program, read a single button on pin PB_4 + */ + +#include "mbed.h" +#include "Hotboards_buttons.h" + +//Creates a single button object, when the button is pressed it gives you +//a LOW(0) value because it works with pull-ups. +Hotboards_buttons btn( PB_4 ); +//If your buttons gives you a HIGH(1) value when is pressed, then we need +//to create the button object with and extra parameter: +//Hotboards_buttons btn( PB_4 , 1 ); in any case the functions will return +//a HIGH(1) value any time the button is pressed + +//To our example we will use the led on the nucleo board +DigitalOut nucleoLed( LED1 ); + +int main() +{ + while(1) + { + //The moment when the button is pressed the function will return a HIGH(1) + //value, it doesn`t matter if your button is configured with pull-ups(LOW) + //or pull-downs(HIGH) + if( btn.status() ) + { + //Button is pressed, led on the nucleo board is ON + nucleoLed = 1; + } + else + { + //Button is not pressed, led on the nucleo board is OFF + nucleoLed = 0; + } + //Just to poll not so often + wait_ms( 50 ); + } +}