Basic example showing how to read a button with interrupt.
Dependencies: mbed
main.cpp@1:a6ed173ce041, 2017-06-07 (annotated)
- Committer:
- arostm
- Date:
- Wed Jun 07 11:58:21 2017 +0000
- Revision:
- 1:a6ed173ce041
- Parent:
- 0:2faf62d5a08c
Adding new mbed library
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bcostm | 0:2faf62d5a08c | 1 | #include "mbed.h" |
bcostm | 0:2faf62d5a08c | 2 | |
bcostm | 0:2faf62d5a08c | 3 | DigitalOut green_led(LED1); |
bcostm | 0:2faf62d5a08c | 4 | DigitalOut red_led(LED2); |
bcostm | 0:2faf62d5a08c | 5 | |
bcostm | 0:2faf62d5a08c | 6 | InterruptIn user_button(USER_BUTTON); |
bcostm | 0:2faf62d5a08c | 7 | |
bcostm | 0:2faf62d5a08c | 8 | void button_pressed() |
bcostm | 0:2faf62d5a08c | 9 | { |
bcostm | 0:2faf62d5a08c | 10 | green_led = 1; |
bcostm | 0:2faf62d5a08c | 11 | } |
bcostm | 0:2faf62d5a08c | 12 | |
bcostm | 0:2faf62d5a08c | 13 | void button_released() |
bcostm | 0:2faf62d5a08c | 14 | { |
bcostm | 0:2faf62d5a08c | 15 | green_led = 0; |
bcostm | 0:2faf62d5a08c | 16 | } |
bcostm | 0:2faf62d5a08c | 17 | |
bcostm | 0:2faf62d5a08c | 18 | int main() { |
bcostm | 0:2faf62d5a08c | 19 | |
bcostm | 0:2faf62d5a08c | 20 | user_button.rise(&button_pressed); |
bcostm | 0:2faf62d5a08c | 21 | user_button.fall(&button_released); |
bcostm | 0:2faf62d5a08c | 22 | |
bcostm | 0:2faf62d5a08c | 23 | while(1) { |
bcostm | 0:2faf62d5a08c | 24 | red_led = !red_led; |
bcostm | 0:2faf62d5a08c | 25 | wait(0.2); |
bcostm | 0:2faf62d5a08c | 26 | } |
bcostm | 0:2faf62d5a08c | 27 | |
bcostm | 0:2faf62d5a08c | 28 | } |