homayoun mh / Mbed 2 deprecated Arduino_Debounce

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 
00003 InterruptIn button(USER_BUTTON);
00004 DigitalOut led(LED1);
00005 Timer debounceTimer;
00006 long debounceDelay = 50;
00007 bool debounced = false;
00008 
00009 void buttonPressed()
00010 {
00011     if (debounceTimer.read_ms() > debounceDelay) {
00012         debounceTimer.stop();
00013         debounced = false;
00014     }
00015     if (debounced == false) {
00016 
00017         led = 1;
00018 
00019         debounceTimer.start();
00020         debounced = true;
00021     }
00022 }
00023 
00024 void buttonReleased()
00025 {
00026     if (debounceTimer.read_ms() > debounceDelay) {
00027         debounceTimer.stop();
00028         debounced = false;
00029     }
00030     if (debounced == false) {
00031 
00032         led = 0;
00033 
00034         debounceTimer.start();
00035         debounced = true;
00036     }
00037 }
00038 
00039 void setup()
00040 {
00041     // button.mode(PullUp);
00042     button.rise(&buttonReleased);  // attach the address of the buttonReleased function to the rising edge
00043     button.fall(&buttonPressed);  // attach the address of the buttonPressed function to the falling edge
00044 }
00045 
00046 void loop()
00047 {
00048     // put your main code here, to run repeatedly:
00049 
00050 }
00051 
00052 int main()
00053 {
00054     setup();
00055     while(1) loop();
00056 }