Debounce and latch a button input

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
ethanharstad
Date:
Mon Jun 02 06:47:09 2014 +0000
Commit message:
Initial commit

Changed in this revision

Debounce.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r 59aa41d04f51 Debounce.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Debounce.cpp	Mon Jun 02 06:47:09 2014 +0000
@@ -0,0 +1,38 @@
+#include "mbed.h"
+
+Serial pc(USBTX, USBRX);
+DigitalIn btn(USER_BUTTON);
+DigitalOut led(LED1);
+Timer timer;
+Timer debounce;
+
+bool state = false;
+bool prevState = false;
+bool counting = false;
+
+int main() {
+    while(true) {
+        state = btn;
+        if(state != prevState) {
+            debounce.start();
+        }
+        if(debounce.read_ms() > 20) {
+            debounce.stop();
+            debounce.reset();
+            if(!state) {
+                if(!counting) {
+                    counting = true;
+                    led = true;
+                    timer.start();
+                } else {
+                    counting = false;
+                    led = false;
+                    timer.stop();
+                    pc.printf("%i ms\n", timer.read_ms());
+                    timer.reset();
+                }
+            }
+        }
+        prevState = state;
+    }
+}
\ No newline at end of file
diff -r 000000000000 -r 59aa41d04f51 mbed.bld
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Mon Jun 02 06:47:09 2014 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/0b3ab51c8877
\ No newline at end of file