Page 194 - ARM Based Microcontroller Projects Using MBED
P. 194

180                          8. INTERMEDIATE LEVEL PROJECTS

                      /*************************************************************************

                                                      REACTION TIMING
                                                      ===============
                      This program measures the user reaction time. The User LED is lit randomly
                      and the program waits until the User button is pressed. The elapsed time
                      between the LED going ON and the button pressed is displayed on the PC
                      screen in milliseconds. This process is repeated after 3 seconds.
                      Author: Dogan Ibrahim
                      Date  : August 2018
                      File  : Reaction
                      **************************************************************************/
                      #include "mbed.h"
                      Serial MyPC(USBTX, USBRX);
                      Timer tim;
                      DigitalOut MyLED(LED1);                                 // LED1 is output
                      DigitalIn button(BUTTON1);                              // BUTTON1 is input
                      int main()
                      {
                         int r;
                         float Duration;


                         MyPC.printf("\n\rReaction Timing");                  // Display heading
                         MyPC.printf("\n\rPress the button as soon as you see the light...");

                         while(1)
                         {
                             tim.reset();                                     // Reset Timer to 0
                             r = rand() % 10 + 1;                             // Between 1 and 10
                             wait(r);                                         // Random wait
                             tim.start();                                     // Start Timer
                             MyLED = 1;                                       // LED ON
                             wait(0.05);                                      // 50ms delay
                             MyLED = 0;                                       // LED OFF
                             while(button == 1);                              // Wait for button
                             tim.stop();                                      // Stop Timer
                             Duration = tim.read_ms() - 50.0;                 // Read Duration
                             MyPC.printf("\n\rReaction Time (ms) = %f", Duration);
                             wait(3.0);
                          }
                      }
                 FIG. 8.41  Program listing.










                 FIG. 8.42  Typical output from the program.
   189   190   191   192   193   194   195   196   197   198   199