Arduino_Debounce sample code ported.

Dependencies:   mbed

Committer:
homayoun
Date:
Wed Sep 03 11:03:46 2014 +0000
Revision:
0:4e066da5d43d
Arduino_Debounce

Who changed what in which revision?

UserRevisionLine numberNew contents of line
homayoun 0:4e066da5d43d 1 #include "mbed.h"
homayoun 0:4e066da5d43d 2
homayoun 0:4e066da5d43d 3 InterruptIn button(USER_BUTTON);
homayoun 0:4e066da5d43d 4 DigitalOut led(LED1);
homayoun 0:4e066da5d43d 5 Timer debounceTimer;
homayoun 0:4e066da5d43d 6 long debounceDelay = 50;
homayoun 0:4e066da5d43d 7 bool debounced = false;
homayoun 0:4e066da5d43d 8
homayoun 0:4e066da5d43d 9 void buttonPressed()
homayoun 0:4e066da5d43d 10 {
homayoun 0:4e066da5d43d 11 if (debounceTimer.read_ms() > debounceDelay) {
homayoun 0:4e066da5d43d 12 debounceTimer.stop();
homayoun 0:4e066da5d43d 13 debounced = false;
homayoun 0:4e066da5d43d 14 }
homayoun 0:4e066da5d43d 15 if (debounced == false) {
homayoun 0:4e066da5d43d 16
homayoun 0:4e066da5d43d 17 led = 1;
homayoun 0:4e066da5d43d 18
homayoun 0:4e066da5d43d 19 debounceTimer.start();
homayoun 0:4e066da5d43d 20 debounced = true;
homayoun 0:4e066da5d43d 21 }
homayoun 0:4e066da5d43d 22 }
homayoun 0:4e066da5d43d 23
homayoun 0:4e066da5d43d 24 void buttonReleased()
homayoun 0:4e066da5d43d 25 {
homayoun 0:4e066da5d43d 26 if (debounceTimer.read_ms() > debounceDelay) {
homayoun 0:4e066da5d43d 27 debounceTimer.stop();
homayoun 0:4e066da5d43d 28 debounced = false;
homayoun 0:4e066da5d43d 29 }
homayoun 0:4e066da5d43d 30 if (debounced == false) {
homayoun 0:4e066da5d43d 31
homayoun 0:4e066da5d43d 32 led = 0;
homayoun 0:4e066da5d43d 33
homayoun 0:4e066da5d43d 34 debounceTimer.start();
homayoun 0:4e066da5d43d 35 debounced = true;
homayoun 0:4e066da5d43d 36 }
homayoun 0:4e066da5d43d 37 }
homayoun 0:4e066da5d43d 38
homayoun 0:4e066da5d43d 39 void setup()
homayoun 0:4e066da5d43d 40 {
homayoun 0:4e066da5d43d 41 // button.mode(PullUp);
homayoun 0:4e066da5d43d 42 button.rise(&buttonReleased); // attach the address of the buttonReleased function to the rising edge
homayoun 0:4e066da5d43d 43 button.fall(&buttonPressed); // attach the address of the buttonPressed function to the falling edge
homayoun 0:4e066da5d43d 44 }
homayoun 0:4e066da5d43d 45
homayoun 0:4e066da5d43d 46 void loop()
homayoun 0:4e066da5d43d 47 {
homayoun 0:4e066da5d43d 48 // put your main code here, to run repeatedly:
homayoun 0:4e066da5d43d 49
homayoun 0:4e066da5d43d 50 }
homayoun 0:4e066da5d43d 51
homayoun 0:4e066da5d43d 52 int main()
homayoun 0:4e066da5d43d 53 {
homayoun 0:4e066da5d43d 54 setup();
homayoun 0:4e066da5d43d 55 while(1) loop();
homayoun 0:4e066da5d43d 56 }