homayoun mh
/
Arduino_Debounce
Arduino_Debounce sample code ported.
Diff: main.cpp
- Revision:
- 0:4e066da5d43d
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Wed Sep 03 11:03:46 2014 +0000 @@ -0,0 +1,56 @@ +#include "mbed.h" + +InterruptIn button(USER_BUTTON); +DigitalOut led(LED1); +Timer debounceTimer; +long debounceDelay = 50; +bool debounced = false; + +void buttonPressed() +{ + if (debounceTimer.read_ms() > debounceDelay) { + debounceTimer.stop(); + debounced = false; + } + if (debounced == false) { + + led = 1; + + debounceTimer.start(); + debounced = true; + } +} + +void buttonReleased() +{ + if (debounceTimer.read_ms() > debounceDelay) { + debounceTimer.stop(); + debounced = false; + } + if (debounced == false) { + + led = 0; + + debounceTimer.start(); + debounced = true; + } +} + +void setup() +{ + // button.mode(PullUp); + button.rise(&buttonReleased); // attach the address of the buttonReleased function to the rising edge + button.fall(&buttonPressed); // attach the address of the buttonPressed function to the falling edge +} + +void loop() +{ + // put your main code here, to run repeatedly: + +} + +int main() +{ + setup(); + while(1) loop(); +}