Albertas Galin
/
Reaction_timer
Human(oid) reaction time measurement Basic buttons,leds,timer,wait usage examples
main.cpp@0:436ae35cdd94, 2015-04-30 (annotated)
- Committer:
- albertas
- Date:
- Thu Apr 30 20:02:06 2015 +0000
- Revision:
- 0:436ae35cdd94
Rev1.0. by AG
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
albertas | 0:436ae35cdd94 | 1 | /* |
albertas | 0:436ae35cdd94 | 2 | Reaction time measurement |
albertas | 0:436ae35cdd94 | 3 | Result output to terminal |
albertas | 0:436ae35cdd94 | 4 | your result should be about 0.220+-0.060s |
albertas | 0:436ae35cdd94 | 5 | best result https://www.youtube.com/watch?v=XsU5AMxvlKg (youtube) |
albertas | 0:436ae35cdd94 | 6 | */ |
albertas | 0:436ae35cdd94 | 7 | #include "mbed.h" |
albertas | 0:436ae35cdd94 | 8 | |
albertas | 0:436ae35cdd94 | 9 | DigitalOut myled(LED1); //define LED |
albertas | 0:436ae35cdd94 | 10 | DigitalIn mybutton(USER_BUTTON); //define BUTTON |
albertas | 0:436ae35cdd94 | 11 | Timer t; //define timer |
albertas | 0:436ae35cdd94 | 12 | int x; //x for random number storage |
albertas | 0:436ae35cdd94 | 13 | |
albertas | 0:436ae35cdd94 | 14 | int main() |
albertas | 0:436ae35cdd94 | 15 | { |
albertas | 0:436ae35cdd94 | 16 | printf("Reaction test!\n"); |
albertas | 0:436ae35cdd94 | 17 | printf("Push the button when the LED is on!\n"); |
albertas | 0:436ae35cdd94 | 18 | |
albertas | 0:436ae35cdd94 | 19 | while(1) { |
albertas | 0:436ae35cdd94 | 20 | |
albertas | 0:436ae35cdd94 | 21 | while(1) { |
albertas | 0:436ae35cdd94 | 22 | |
albertas | 0:436ae35cdd94 | 23 | x=rand()%2000; //generate random number 0-2000 |
albertas | 0:436ae35cdd94 | 24 | wait_ms(1000+x); //wait random time between 1 and 3 seconds |
albertas | 0:436ae35cdd94 | 25 | |
albertas | 0:436ae35cdd94 | 26 | t.start(); //start the timer |
albertas | 0:436ae35cdd94 | 27 | |
albertas | 0:436ae35cdd94 | 28 | if(mybutton == 0)break; //to early for push or cheating |
albertas | 0:436ae35cdd94 | 29 | |
albertas | 0:436ae35cdd94 | 30 | myled=1; //LED on |
albertas | 0:436ae35cdd94 | 31 | |
albertas | 0:436ae35cdd94 | 32 | while(1) { //wait for push |
albertas | 0:436ae35cdd94 | 33 | if (mybutton == 0)break; |
albertas | 0:436ae35cdd94 | 34 | } |
albertas | 0:436ae35cdd94 | 35 | |
albertas | 0:436ae35cdd94 | 36 | t.stop(); //stop the timer |
albertas | 0:436ae35cdd94 | 37 | |
albertas | 0:436ae35cdd94 | 38 | myled=0; //LED off |
albertas | 0:436ae35cdd94 | 39 | |
albertas | 0:436ae35cdd94 | 40 | printf("The time taken was %f seconds\n", t.read()); |
albertas | 0:436ae35cdd94 | 41 | |
albertas | 0:436ae35cdd94 | 42 | t.reset(); //reset the timer |
albertas | 0:436ae35cdd94 | 43 | |
albertas | 0:436ae35cdd94 | 44 | wait(0.5); //debounce the BUTTON |
albertas | 0:436ae35cdd94 | 45 | |
albertas | 0:436ae35cdd94 | 46 | } |
albertas | 0:436ae35cdd94 | 47 | printf("Cheeting!!!\n"); |
albertas | 0:436ae35cdd94 | 48 | wait(0.5);//debounce the BUTTON |
albertas | 0:436ae35cdd94 | 49 | } |
albertas | 0:436ae35cdd94 | 50 | } |