Emery Premeaux / Mbed 2 deprecated Examples

Dependencies:   mbed SDFileSystem

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Debounce.cpp Source File

Debounce.cpp

00001 #ifdef COMPILE_Debounce
00002 
00003 DigitalOut led1(LED1);
00004 
00005 InterruptIn button1(USER_BUTTON);
00006 volatile bool button1_pressed = false; // Used in the main loop
00007 volatile bool button1_enabled = true; // Used for debouncing
00008 Timeout button1_timeout; // Used for debouncing
00009 
00010 // Enables button when bouncing is over
00011 void button1_enabled_cb(void)
00012 {
00013     button1_enabled = true;
00014 }
00015 
00016 // ISR handling button pressed event
00017 void button1_onpressed_cb(void)
00018 {
00019     if (button1_enabled) { // Disabled while the button is bouncing
00020         button1_enabled = false;
00021         button1_pressed = true; // To be read by the main loop
00022         button1_timeout.attach(callback(button1_enabled_cb), 0.3); // Debounce time 300 ms
00023     }
00024 }
00025 
00026 int main()
00027 {
00028     //button1.mode(PullUp); // Activate pull-up
00029     button1.fall(callback(button1_onpressed_cb)); // Attach ISR to handle button press event
00030 
00031     int idx = 0; // Just for printf below
00032 
00033     while(1) {
00034         if (button1_pressed) { // Set when button is pressed
00035             button1_pressed = false;
00036             printf("Button pressed %d\n", idx++);
00037             led1 = !led1;
00038         }
00039     }
00040 }
00041 
00042 #endif