Steffen Wehner
/
Button_input_ext_interrupt
a program with changes the between two states with the press of a button
main.cpp@1:a070f8a0bf04, 2015-11-03 (annotated)
- Committer:
- wehner334
- Date:
- Tue Nov 03 15:32:39 2015 +0000
- Revision:
- 1:a070f8a0bf04
- Parent:
- 0:26da192b707b
tested and working with changes;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
wehner334 | 0:26da192b707b | 1 | #include "mbed.h" |
wehner334 | 1:a070f8a0bf04 | 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 | 1:a070f8a0bf04 | 5 | |
wehner334 | 1:a070f8a0bf04 | 6 | volatile float delay = 4.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 | 1:a070f8a0bf04 | 15 | Pressedornot=true; |
wehner334 | 0:26da192b707b | 16 | else |
wehner334 | 1:a070f8a0bf04 | 17 | Pressedornot=false; |
wehner334 | 0:26da192b707b | 18 | } |
wehner334 | 1:a070f8a0bf04 | 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 | 1:a070f8a0bf04 | 24 | while(Pressedornot==false) { |
wehner334 | 1:a070f8a0bf04 | 25 | myled = !myled; |
wehner334 | 1:a070f8a0bf04 | 26 | for(int i=0;i<1000000;i++)// |
wehner334 | 1:a070f8a0bf04 | 27 | {wait_us(1); |
wehner334 | 1:a070f8a0bf04 | 28 | if(Pressedornot==true) |
wehner334 | 1:a070f8a0bf04 | 29 | {break;} |
wehner334 | 1:a070f8a0bf04 | 30 | } |
wehner334 | 1:a070f8a0bf04 | 31 | } while( Pressedornot==true ) |
wehner334 | 1:a070f8a0bf04 | 32 | { |
wehner334 | 1:a070f8a0bf04 | 33 | myled=true; |
wehner334 | 0:26da192b707b | 34 | } |
wehner334 | 1:a070f8a0bf04 | 35 | |
wehner334 | 0:26da192b707b | 36 | } |
wehner334 | 0:26da192b707b | 37 | } |