Timer

Dependencies:   mbed

Revision:
0:d50ef34ef9ae
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Wed Sep 09 07:45:57 2015 +0000
@@ -0,0 +1,52 @@
+#include "mbed.h"
+
+DigitalIn button(PTA4);
+DigitalOut blue(LED3);
+Timer timer;
+int nBlinky = 1;
+int buttonState = 1;
+
+void blinkRed(int nblinks) {
+    for (int i=0; i < nblinks; i++) {
+        blue.write(0);
+        wait(0.2);
+        blue.write(1);
+        wait(0.2);
+    }
+}
+
+bool buttonPressed(){
+
+    if (button.read() == 0 && buttonState == 1){
+        buttonState = 0;
+        return true;
+    }
+    if (button.read() == 1 && buttonState == 0){
+        buttonState = 1;
+    }
+    return false;
+}
+
+void count() {
+    if (buttonPressed()) {
+        nBlinky++;
+    }
+}
+
+int main()
+{
+    blue = 1;
+
+    while (true) {
+        if (buttonPressed()) {
+            timer.reset();
+            timer.start();
+            nBlinky = 1;
+            while (timer.read_ms() <= 2000) {
+                count();
+            }
+            blinkRed(nBlinky);
+        }
+
+    }
+}
\ No newline at end of file