Check button bounce by counting the interrupts between press and release of BUTTON1.

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 DigitalIn mybutton(BUTTON1,PullUp); // Pushbutton input (PC_13)
00004 InterruptIn button(BUTTON1);        // Pusbutton interrupt
00005 Serial pc(USBTX,USBRX);             // UART0 via OpenSDA
00006 volatile uint16_t counts;           // counter variable
00007 
00008 void button_pressed() {
00009     counts++;                       // counts button presses
00010 }
00011 
00012 int main() {
00013     pc.baud(115200);                // set baudrate for UART
00014     button.mode(PullUp);            // Enable internal pullup
00015     button.fall(&button_pressed);   // Attach function to falling edge
00016     while (true) {
00017         counts = 0;                 // Clear counter
00018         pc.printf("Press & release switch... \r\n");
00019         while (mybutton);           // Wait for button press
00020         wait_ms(20);                // Debounce delay
00021         while (!mybutton);          // Wait for button release
00022         wait_ms(20);                // Debounce delay
00023         pc.printf("Button pressed %d times\r\n",counts);
00024     }
00025 }