reaction timer

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* Reaction timer
00002 Simple reaction timer, wait for the green light
00003 press the button on the board. Results are 
00004 sent to the PC, view using a COM port reader (Terminal / XCTU) 
00005 */
00006 
00007 #include "mbed.h"
00008 
00009   
00010 // pin setup
00011 //LED's
00012 DigitalOut redLED(D11);
00013 DigitalOut amberLED(D12); 
00014 DigitalOut greenLED(D13); 
00015 
00016 // button
00017 DigitalIn button(USER_BUTTON); 
00018 
00019 //timer
00020 Timer t; 
00021 
00022 // communications
00023 Serial pc (SERIAL_TX, SERIAL_RX); 
00024 
00025 // global variables
00026 float startTime = 0.0; 
00027 float stopTime = 0.0;
00028 float duration = 0.0;  
00029 
00030 
00031 
00032 
00033 int main() {
00034     t.start(); // start the timer
00035     while(1) {
00036         flash();
00037         startTime = t.read(); // read and store the time 
00038         while (stopTime == 0.0)
00039         {
00040             if (button !=1) // button pressed
00041             {                
00042                 stopTime = t.read(); // read and store the time
00043                 // calculate the time elapsed
00044                 duration = stopTime - startTime; 
00045                 pc.printf("Reaction Time = %d Seconds", duration);                 
00046                 wait(5); // wait 5 seconds
00047                 // reset the timers 
00048                 duration = 0.0;                          
00049                 } 
00050          }   
00051          stopTime = 0.0; // reset the stop time 
00052          wait (5.0); // wait for a bit before starting again    
00053     }
00054 }
00055 
00056 void flash(){
00057         // flash sequence 
00058         redLED = 1; // Red is ON
00059         wait(2); // 2 seconds
00060         redLED = 0; // Red is OFF
00061         amberLED= 1; // Amber is ON 
00062         wait(1); // 1 second
00063         amberLED = 0; // Amber is OFF
00064         greenLED = 1; // Green in ON             
00065 }