Guest User

weeeeeeeeeee

a guest
Mar 29th, 2016
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.95 KB | None | 0 0
  1. // C4_Reaction_Time_Tester
  2. // Reports over the serial port how many milli seconds it
  3. // takes you to remove your finger from the pushbutton after
  4. // the LED turns on.
  5.  
  6. // Constants used to set pin numbers
  7. const int buttonPin = 12; // the number of the pushbutton pin
  8.  
  9. // variables
  10. int buttonState = 0; // pushbutton status variable
  11. const int ledPin = 11; // the number of the LED pin
  12. int reactionTime = 0; // reaction time variable
  13.  
  14. unsigned long startTime = 0;
  15. unsigned long endTime = 0;
  16.  
  17. long randNumber;
  18.  
  19. void setup() {
  20. // initialize Serial communications
  21. Serial.begin(57600);
  22. Serial.println("Test your reaction time.");
  23. Serial.println("Hold down button and release when LED comes on.");
  24.  
  25. // set the buttonPin mode to INPUT
  26. pinMode(buttonPin, INPUT);
  27. // set the ledPin mode to OUTPUT
  28. pinMode(ledPin, OUTPUT);
  29. }
  30.  
  31. void loop(){
  32.  
  33. // get the state of the pushbutton
  34. buttonState = digitalRead(buttonPin);
  35.  
  36. // is the button pressed?
  37. // if it is, the buttonState is HIGH:
  38. if (buttonState == HIGH) {
  39.  
  40. // select a random number of milliseconds from 1000 to 5000
  41. randNumber = random(1000,5000);
  42.  
  43. // turn LED on:
  44. digitalWrite(ledPin, HIGH);
  45.  
  46. // leave the LED on for the randome milliseconds
  47. delay(randNumber);
  48.  
  49. // turn LED off:
  50. digitalWrite(ledPin, LOW);
  51.  
  52. // get the start time as soon as the LED goes off
  53. startTime = millis();
  54.  
  55. // wait until the subjects gets his finger off the button
  56. // read the button state until it is equal to HIGH
  57. do{
  58. // get the state of the pushbutton
  59. buttonState = digitalRead(buttonPin);
  60. }while(buttonState == HIGH);
  61.  
  62. // get the end time as soon as the finger is off the switch
  63. endTime = millis();
  64.  
  65. // Tell the world your reaction time
  66. Serial.print("You took: ");
  67. Serial.print(endTime-startTime,DEC);
  68. Serial.println(" milliseconds.");
  69. }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment