![](/media/cache/profiles/abed4d6893be8b9687eab0234e0cac09.jpg.50x50_q85.jpg)
reading a button state
Dependencies: Hotboards_buttons mbed
main.cpp@0:538950d8a034, 2016-02-29 (annotated)
- Committer:
- RomanValenciaP
- Date:
- Mon Feb 29 20:59:52 2016 +0000
- Revision:
- 0:538950d8a034
first release - recquires approval
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
RomanValenciaP | 0:538950d8a034 | 1 | |
RomanValenciaP | 0:538950d8a034 | 2 | /* |
RomanValenciaP | 0:538950d8a034 | 3 | * The simplest program, read a single button on pin PB_4 |
RomanValenciaP | 0:538950d8a034 | 4 | */ |
RomanValenciaP | 0:538950d8a034 | 5 | |
RomanValenciaP | 0:538950d8a034 | 6 | #include "mbed.h" |
RomanValenciaP | 0:538950d8a034 | 7 | #include "Hotboards_buttons.h" |
RomanValenciaP | 0:538950d8a034 | 8 | |
RomanValenciaP | 0:538950d8a034 | 9 | //Creates a single button object, when the button is pressed it gives you |
RomanValenciaP | 0:538950d8a034 | 10 | //a LOW(0) value because it works with pull-ups. |
RomanValenciaP | 0:538950d8a034 | 11 | Hotboards_buttons btn( PB_4 ); |
RomanValenciaP | 0:538950d8a034 | 12 | //If your buttons gives you a HIGH(1) value when is pressed, then we need |
RomanValenciaP | 0:538950d8a034 | 13 | //to create the button object with and extra parameter: |
RomanValenciaP | 0:538950d8a034 | 14 | //Hotboards_buttons btn( PB_4 , 1 ); in any case the functions will return |
RomanValenciaP | 0:538950d8a034 | 15 | //a HIGH(1) value any time the button is pressed |
RomanValenciaP | 0:538950d8a034 | 16 | |
RomanValenciaP | 0:538950d8a034 | 17 | //To our example we will use the led on the nucleo board |
RomanValenciaP | 0:538950d8a034 | 18 | DigitalOut nucleoLed( LED1 ); |
RomanValenciaP | 0:538950d8a034 | 19 | |
RomanValenciaP | 0:538950d8a034 | 20 | int main() |
RomanValenciaP | 0:538950d8a034 | 21 | { |
RomanValenciaP | 0:538950d8a034 | 22 | while(1) |
RomanValenciaP | 0:538950d8a034 | 23 | { |
RomanValenciaP | 0:538950d8a034 | 24 | //The moment when the button is pressed the function will return a HIGH(1) |
RomanValenciaP | 0:538950d8a034 | 25 | //value, it doesn`t matter if your button is configured with pull-ups(LOW) |
RomanValenciaP | 0:538950d8a034 | 26 | //or pull-downs(HIGH) |
RomanValenciaP | 0:538950d8a034 | 27 | if( btn.status() ) |
RomanValenciaP | 0:538950d8a034 | 28 | { |
RomanValenciaP | 0:538950d8a034 | 29 | //Button is pressed, led on the nucleo board is ON |
RomanValenciaP | 0:538950d8a034 | 30 | nucleoLed = 1; |
RomanValenciaP | 0:538950d8a034 | 31 | } |
RomanValenciaP | 0:538950d8a034 | 32 | else |
RomanValenciaP | 0:538950d8a034 | 33 | { |
RomanValenciaP | 0:538950d8a034 | 34 | //Button is not pressed, led on the nucleo board is OFF |
RomanValenciaP | 0:538950d8a034 | 35 | nucleoLed = 0; |
RomanValenciaP | 0:538950d8a034 | 36 | } |
RomanValenciaP | 0:538950d8a034 | 37 | //Just to poll not so often |
RomanValenciaP | 0:538950d8a034 | 38 | wait_ms( 50 ); |
RomanValenciaP | 0:538950d8a034 | 39 | } |
RomanValenciaP | 0:538950d8a034 | 40 | } |