Measure reaction time of the user. One must press the builtin user button when the builtin LED lights up.

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 Serial pc(USBTX,USBRX); 
00003 DigitalOut myled(LED1);             //define LED
00004 DigitalIn mybutton(BUTTON1,PullUp); //define pushbutton input with pull-up
00005 Timer t;                            //define timer
00006 int x;                              //x for random number storage
00007 
00008 int main() {
00009     pc.baud(115200);
00010     myled = 0;                      //LED is initially off 
00011     pc.printf("\r\nReaction test!\r\n");
00012     pc.printf("Push the button when the LED is on!\r\n");
00013     while(1) {
00014         t.reset();                  //reset the timer        
00015         while(!mybutton);           //wait for button release
00016         x=rand()%2000;              //generate random number 0-2000
00017         wait_ms(1000+x);            //wait random time between 1 and 3 seconds
00018         t.start();                  //start the timer
00019         myled=1;                    //LED on
00020         while(mybutton);            //wait for press
00021         t.stop();                   //stop the timer
00022         myled=0;                    //LED off
00023         wait_ms(20);                //debounce delay
00024         printf("The time taken was %f seconds\r\n", t.read());
00025     }
00026 }