Steffen Wehner
/
Button_input_ext_interrupt
a program with changes the between two states with the press of a button
main.cpp@0:26da192b707b, 2015-11-03 (annotated)
- Committer:
- wehner334
- Date:
- Tue Nov 03 12:25:44 2015 +0000
- Revision:
- 0:26da192b707b
- Child:
- 1:a070f8a0bf04
intial build not jet tested
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
wehner334 | 0:26da192b707b | 1 | #include "mbed.h" |
wehner334 | 0:26da192b707b | 2 | |
wehner334 | 0:26da192b707b | 3 | InterruptIn mybutton(USER_BUTTON);//generates an object named mybutton out of the class InterruptIn |
wehner334 | 0:26da192b707b | 4 | DigitalOut myled(LED1); // |
wehner334 | 0:26da192b707b | 5 | |
wehner334 | 0:26da192b707b | 6 | volatile float delay = 1.0; // 1 sec of delay in the delay |
wehner334 | 0:26da192b707b | 7 | volatile bool Pressedornot=false; // global variable with is updated by the interrupt function |
wehner334 | 0:26da192b707b | 8 | |
wehner334 | 0:26da192b707b | 9 | /*function pressed() is called than a interrupt is generated by the external interrupt when the userbutton is pressed |
wehner334 | 0:26da192b707b | 10 | it alters the Pressedornot variable from true to false |
wehner334 | 0:26da192b707b | 11 | */ |
wehner334 | 0:26da192b707b | 12 | void pressed() |
wehner334 | 0:26da192b707b | 13 | { |
wehner334 | 0:26da192b707b | 14 | if (Pressedornot==false) |
wehner334 | 0:26da192b707b | 15 | Pressedornot=true; |
wehner334 | 0:26da192b707b | 16 | else |
wehner334 | 0:26da192b707b | 17 | Pressedornot=false; |
wehner334 | 0:26da192b707b | 18 | } |
wehner334 | 0:26da192b707b | 19 | |
wehner334 | 0:26da192b707b | 20 | int main() |
wehner334 | 0:26da192b707b | 21 | { |
wehner334 | 0:26da192b707b | 22 | mybutton.fall(&pressed);//activates the interrupt then a falling edge is detected on the pin |
wehner334 | 0:26da192b707b | 23 | while (1) { |
wehner334 | 0:26da192b707b | 24 | if(Pressedornot==false) |
wehner334 | 0:26da192b707b | 25 | {myled = !myled; |
wehner334 | 0:26da192b707b | 26 | wait(delay); |
wehner334 | 0:26da192b707b | 27 | } |
wehner334 | 0:26da192b707b | 28 | else |
wehner334 | 0:26da192b707b | 29 | {myled=true; |
wehner334 | 0:26da192b707b | 30 | } |
wehner334 | 0:26da192b707b | 31 | |
wehner334 | 0:26da192b707b | 32 | } |
wehner334 | 0:26da192b707b | 33 | } |