Button with interrupt example.

Dependencies:   mbed

Committer:
bcostm
Date:
Fri Jul 07 06:42:11 2017 +0000
Revision:
1:cb693ddbc9b2
Parent:
0:7d38be0bc9c1
mbed lib rev 146

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bcostm 0:7d38be0bc9c1 1 #include "mbed.h"
bcostm 0:7d38be0bc9c1 2
bcostm 0:7d38be0bc9c1 3 InterruptIn button(USER_BUTTON);
bcostm 0:7d38be0bc9c1 4
bcostm 0:7d38be0bc9c1 5 DigitalOut led(LED1);
bcostm 0:7d38be0bc9c1 6
bcostm 0:7d38be0bc9c1 7 double delay = 0.5; // 500 ms
bcostm 0:7d38be0bc9c1 8
bcostm 0:7d38be0bc9c1 9 void pressed()
bcostm 0:7d38be0bc9c1 10 {
bcostm 0:7d38be0bc9c1 11 delay = 0.1; // 100 ms
bcostm 0:7d38be0bc9c1 12 }
bcostm 0:7d38be0bc9c1 13
bcostm 0:7d38be0bc9c1 14 void released()
bcostm 0:7d38be0bc9c1 15 {
bcostm 0:7d38be0bc9c1 16 delay = 0.5; // 500 ms
bcostm 0:7d38be0bc9c1 17 }
bcostm 0:7d38be0bc9c1 18
bcostm 0:7d38be0bc9c1 19 int main()
bcostm 0:7d38be0bc9c1 20 {
bcostm 0:7d38be0bc9c1 21 // Assign functions to button
bcostm 0:7d38be0bc9c1 22 button.fall(&pressed);
bcostm 0:7d38be0bc9c1 23 button.rise(&released);
bcostm 0:7d38be0bc9c1 24
bcostm 0:7d38be0bc9c1 25 while (1) {
bcostm 0:7d38be0bc9c1 26 led = !led;
bcostm 0:7d38be0bc9c1 27 wait(delay);
bcostm 0:7d38be0bc9c1 28 }
bcostm 0:7d38be0bc9c1 29 }