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.
Dependencies: mbed
main.cpp@0:c3ed03e22b93, 2016-10-31 (annotated)
- Committer:
- MDevolution
- Date:
- Mon Oct 31 11:08:54 2016 +0000
- Revision:
- 0:c3ed03e22b93
Game Reaction
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| MDevolution | 0:c3ed03e22b93 | 1 | /*Program Example 7: Tests reaction time, and demos use of Timer and Timeout functions */ |
| MDevolution | 0:c3ed03e22b93 | 2 | #include "mbed.h" |
| MDevolution | 0:c3ed03e22b93 | 3 | #include <stdio.h> |
| MDevolution | 0:c3ed03e22b93 | 4 | #include <stdlib.h> //contains rand() function |
| MDevolution | 0:c3ed03e22b93 | 5 | void measure (); |
| MDevolution | 0:c3ed03e22b93 | 6 | Serial pc(USBTX, USBRX); |
| MDevolution | 0:c3ed03e22b93 | 7 | DigitalOut led1(D5); |
| MDevolution | 0:c3ed03e22b93 | 8 | DigitalOut led4(D6); |
| MDevolution | 0:c3ed03e22b93 | 9 | DigitalIn responseinput(PC_13); //the player hits the switch connected here to respond |
| MDevolution | 0:c3ed03e22b93 | 10 | Timer t; //used to measure the response time |
| MDevolution | 0:c3ed03e22b93 | 11 | Timeout action; //the Timeout used to initiate the response speed test |
| MDevolution | 0:c3ed03e22b93 | 12 | |
| MDevolution | 0:c3ed03e22b93 | 13 | int main (){ |
| MDevolution | 0:c3ed03e22b93 | 14 | pc.printf("Reaction Time Test\n\r"); |
| MDevolution | 0:c3ed03e22b93 | 15 | pc.printf("------------------\n\r"); |
| MDevolution | 0:c3ed03e22b93 | 16 | while (1) { |
| MDevolution | 0:c3ed03e22b93 | 17 | int r_delay; //this will be the “random†delay before the led is blinked |
| MDevolution | 0:c3ed03e22b93 | 18 | pc.printf("New Test\n\r"); |
| MDevolution | 0:c3ed03e22b93 | 19 | led4=1; //warn that test will start |
| MDevolution | 0:c3ed03e22b93 | 20 | wait(0.2); |
| MDevolution | 0:c3ed03e22b93 | 21 | led4=0; |
| MDevolution | 0:c3ed03e22b93 | 22 | r_delay = rand() % 10 + 1; // generates a pseudorandom number range 1-10 |
| MDevolution | 0:c3ed03e22b93 | 23 | pc.printf("random number is %i\n\r", r_delay); // allows test randomness; |
| MDevolution | 0:c3ed03e22b93 | 24 | //removed for normal play |
| MDevolution | 0:c3ed03e22b93 | 25 | action.attach(&measure,r_delay); // set up Timeout to call measure() |
| MDevolution | 0:c3ed03e22b93 | 26 | // after random time |
| MDevolution | 0:c3ed03e22b93 | 27 | wait(10); //test will start within this time, and we then return to it |
| MDevolution | 0:c3ed03e22b93 | 28 | } |
| MDevolution | 0:c3ed03e22b93 | 29 | } |
| MDevolution | 0:c3ed03e22b93 | 30 | |
| MDevolution | 0:c3ed03e22b93 | 31 | |
| MDevolution | 0:c3ed03e22b93 | 32 | void measure () // called when the led blinks, and measures response time |
| MDevolution | 0:c3ed03e22b93 | 33 | { |
| MDevolution | 0:c3ed03e22b93 | 34 | if (responseinput == 1) { //detect cheating! |
| MDevolution | 0:c3ed03e22b93 | 35 | pc.printf("Do not hold button down!\n"); |
| MDevolution | 0:c3ed03e22b93 | 36 | } else { |
| MDevolution | 0:c3ed03e22b93 | 37 | t.start(); //start the timer |
| MDevolution | 0:c3ed03e22b93 | 38 | led1=1; //blink the led |
| MDevolution | 0:c3ed03e22b93 | 39 | wait(0.05); |
| MDevolution | 0:c3ed03e22b93 | 40 | led1=0; |
| MDevolution | 0:c3ed03e22b93 | 41 | while (responseinput==0) { |
| MDevolution | 0:c3ed03e22b93 | 42 | //wait here for response |
| MDevolution | 0:c3ed03e22b93 | 43 | } |
| MDevolution | 0:c3ed03e22b93 | 44 | t.stop(); //stop the timer once response detected |
| MDevolution | 0:c3ed03e22b93 | 45 | pc.printf("Your reaction time was %f seconds\n\r", t.read()); |
| MDevolution | 0:c3ed03e22b93 | 46 | t.reset(); |
| MDevolution | 0:c3ed03e22b93 | 47 | } |
| MDevolution | 0:c3ed03e22b93 | 48 | } |