Counts button bounces in a button press-release cycle by using InterruptIn class library. Results are printed out through UART0 (via OpenSDA).

Dependencies:   mbed

07_button_bounce

Counts button bounces in a button press-release cycle by using InterruptIn class library. Results are printed out through UART0 (via OpenSDA).

Hardware requirements:

  • FRDM-KL25Z board
  • Pushbutton connected between D3 and GND

Wiring scheme:

/media/uploads/icserny/led_button.png

Revision:
0:b4a0a2e03ca2
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Thu Dec 10 12:53:11 2015 +0000
@@ -0,0 +1,34 @@
+/** 07_button_bounce
+ *
+ * Counts button bounces in a button press-release cycle
+ * by using InterruptIn class library. Results are printed
+ * out through UART0 (via OpenSDA).
+ *
+ * Hardware requirements:
+ *  - FRDM-KL25Z board
+ *  - Pusbutton (tied between D3 and GND)
+ */
+
+#include "mbed.h"
+DigitalIn mybutton(D3,PullUp);      // Pushbutton input
+InterruptIn button(D3);             // Pusbutton interrupt
+Serial pc(USBTX,USBRX);             // UART0 via OpenSDA
+volatile uint16_t counts;           // counter variable
+
+void button_pressed() {
+    counts++;                       // counts button presses
+}
+
+int main() {
+    button.mode(PullUp);            // Enable internal pullup
+    button.fall(&button_pressed);   // Attach function to falling edge
+    while (true) {
+        counts = 0;                 // Clear counter
+        pc.printf("Press & release switch... \r\n");
+        while (mybutton);           // Wait for button press
+        wait_ms(20);                // Debounce delay
+        while (!mybutton);          // Wait for button release
+        wait_ms(20);                // Debounce delay
+        pc.printf("Button pressed %d times\r\n",counts);
+    }
+}
\ No newline at end of file