Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // C4_Reaction_Time_Tester
- // Reports over the serial port how many milli seconds it
- // takes you to remove your finger from the pushbutton after
- // the LED turns on.
- // Constants used to set pin numbers
- const int buttonPin = 12; // the number of the pushbutton pin
- // variables
- int buttonState = 0; // pushbutton status variable
- const int ledPin = 11; // the number of the LED pin
- int reactionTime = 0; // reaction time variable
- unsigned long startTime = 0;
- unsigned long endTime = 0;
- long randNumber;
- void setup() {
- // initialize Serial communications
- Serial.begin(57600);
- Serial.println("Test your reaction time.");
- Serial.println("Hold down button and release when LED comes on.");
- // set the buttonPin mode to INPUT
- pinMode(buttonPin, INPUT);
- // set the ledPin mode to OUTPUT
- pinMode(ledPin, OUTPUT);
- }
- void loop(){
- // get the state of the pushbutton
- buttonState = digitalRead(buttonPin);
- // is the button pressed?
- // if it is, the buttonState is HIGH:
- if (buttonState == HIGH) {
- // select a random number of milliseconds from 1000 to 5000
- randNumber = random(1000,5000);
- // turn LED on:
- digitalWrite(ledPin, HIGH);
- // leave the LED on for the randome milliseconds
- delay(randNumber);
- // turn LED off:
- digitalWrite(ledPin, LOW);
- // get the start time as soon as the LED goes off
- startTime = millis();
- // wait until the subjects gets his finger off the button
- // read the button state until it is equal to HIGH
- do{
- // get the state of the pushbutton
- buttonState = digitalRead(buttonPin);
- }while(buttonState == HIGH);
- // get the end time as soon as the finger is off the switch
- endTime = millis();
- // Tell the world your reaction time
- Serial.print("You took: ");
- Serial.print(endTime-startTime,DEC);
- Serial.println(" milliseconds.");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment