Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp@0:2e007d983da0, 2013-05-24 (annotated)
- Committer:
- robt
- Date:
- Fri May 24 21:49:03 2013 +0000
- Revision:
- 0:2e007d983da0
by Rob Toulson and Tim Wilmshurst from textbook "Fast and Effective Embedded Systems Design: Applying the ARM mbed"
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| robt | 0:2e007d983da0 | 1 | /*Program Example 9.8: Tests reaction time, and demos use of Timer and Timeout functions |
| robt | 0:2e007d983da0 | 2 | */ |
| robt | 0:2e007d983da0 | 3 | #include "mbed.h" |
| robt | 0:2e007d983da0 | 4 | #include <stdio.h> |
| robt | 0:2e007d983da0 | 5 | #include <stdlib.h> //contains rand() function |
| robt | 0:2e007d983da0 | 6 | void measure (); |
| robt | 0:2e007d983da0 | 7 | Serial pc(USBTX, USBRX); |
| robt | 0:2e007d983da0 | 8 | DigitalOut led1(LED1); |
| robt | 0:2e007d983da0 | 9 | DigitalOut led4(LED4); |
| robt | 0:2e007d983da0 | 10 | DigitalIn responseinput(p5); //the player hits the switch connected here to respond |
| robt | 0:2e007d983da0 | 11 | Timer t; //used to measure the response time |
| robt | 0:2e007d983da0 | 12 | Timeout action; //the Timeout used to initiate the response speed test |
| robt | 0:2e007d983da0 | 13 | |
| robt | 0:2e007d983da0 | 14 | int main (){ |
| robt | 0:2e007d983da0 | 15 | pc.printf("Reaction Time Test\n\r"); |
| robt | 0:2e007d983da0 | 16 | pc.printf("------------------\n\r"); |
| robt | 0:2e007d983da0 | 17 | while (1) { |
| robt | 0:2e007d983da0 | 18 | int r_delay; //this will be the “random” delay before the led is blinked |
| robt | 0:2e007d983da0 | 19 | pc.printf("New Test\n\r"); |
| robt | 0:2e007d983da0 | 20 | led4=1; //warn that test will start |
| robt | 0:2e007d983da0 | 21 | wait(0.2); |
| robt | 0:2e007d983da0 | 22 | led4=0; |
| robt | 0:2e007d983da0 | 23 | r_delay = rand() % 10 + 1; // generates a pseudorandom number range 1-10 |
| robt | 0:2e007d983da0 | 24 | pc.printf("random number is %i\n\r", r_delay); // allows test randomness; |
| robt | 0:2e007d983da0 | 25 | //removed for normal play |
| robt | 0:2e007d983da0 | 26 | action.attach(&measure,r_delay); // set up Timeout to call measure() |
| robt | 0:2e007d983da0 | 27 | // after random time |
| robt | 0:2e007d983da0 | 28 | wait(10); //test will start within this time, and we then return to it |
| robt | 0:2e007d983da0 | 29 | } |
| robt | 0:2e007d983da0 | 30 | } |
| robt | 0:2e007d983da0 | 31 | |
| robt | 0:2e007d983da0 | 32 | void measure (){ // called when the led blinks, and measures response time |
| robt | 0:2e007d983da0 | 33 | if (responseinput ==1){ //detect cheating! |
| robt | 0:2e007d983da0 | 34 | pc.printf("Don't hold button down!"); |
| robt | 0:2e007d983da0 | 35 | } |
| robt | 0:2e007d983da0 | 36 | else{ |
| robt | 0:2e007d983da0 | 37 | t.start(); //start the timer |
| robt | 0:2e007d983da0 | 38 | led1=1; //blink the led |
| robt | 0:2e007d983da0 | 39 | wait(0.05); |
| robt | 0:2e007d983da0 | 40 | led1=0; |
| robt | 0:2e007d983da0 | 41 | while (responseinput==0) { |
| robt | 0:2e007d983da0 | 42 | //wait here for response |
| robt | 0:2e007d983da0 | 43 | } |
| robt | 0:2e007d983da0 | 44 | t.stop(); //stop the timer once response detected |
| robt | 0:2e007d983da0 | 45 | pc.printf("Your reaction time was %f seconds\n\r", t.read()); |
| robt | 0:2e007d983da0 | 46 | t.reset(); |
| robt | 0:2e007d983da0 | 47 | } |
| robt | 0:2e007d983da0 | 48 | } |
| robt | 0:2e007d983da0 | 49 |