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