reaction timer

Dependencies:   mbed

Revision:
0:3f43a0c4607d
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Thu Dec 07 13:29:32 2017 +0000
@@ -0,0 +1,65 @@
+/* Reaction timer
+Simple reaction timer, wait for the green light
+press the button on the board. Results are 
+sent to the PC, view using a COM port reader (Terminal / XCTU) 
+*/
+
+#include "mbed.h"
+
+  
+// pin setup
+//LED's
+DigitalOut redLED(D11);
+DigitalOut amberLED(D12); 
+DigitalOut greenLED(D13); 
+
+// button
+DigitalIn button(USER_BUTTON); 
+
+//timer
+Timer t; 
+
+// communications
+Serial pc (SERIAL_TX, SERIAL_RX); 
+
+// global variables
+float startTime = 0.0; 
+float stopTime = 0.0;
+float duration = 0.0;  
+
+
+
+
+int main() {
+    t.start(); // start the timer
+    while(1) {
+        flash();
+        startTime = t.read(); // read and store the time 
+        while (stopTime == 0.0)
+        {
+            if (button !=1) // button pressed
+            {                
+                stopTime = t.read(); // read and store the time
+                // calculate the time elapsed
+                duration = stopTime - startTime; 
+                pc.printf("Reaction Time = %d Seconds", duration);                 
+                wait(5); // wait 5 seconds
+                // reset the timers 
+                duration = 0.0;                          
+                } 
+         }   
+         stopTime = 0.0; // reset the stop time 
+         wait (5.0); // wait for a bit before starting again    
+    }
+}
+
+void flash(){
+        // flash sequence 
+        redLED = 1; // Red is ON
+        wait(2); // 2 seconds
+        redLED = 0; // Red is OFF
+        amberLED= 1; // Amber is ON 
+        wait(1); // 1 second
+        amberLED = 0; // Amber is OFF
+        greenLED = 1; // Green in ON             
+}