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