// Reaction time experiment of one LED.
// LED will flash 3 times before the start of the experiment.
int LED = 2;
int LEDState = LOW;
int Button = 7;
int ButtonState = LOW;
// Time Variables
long randDelay;
long newT;
long oldT;
long deltaT;
void setup()
{
pinMode(LED, OUTPUT);
pinMode(Button, INPUT);
Serial.begin(9600);
// Three initial blinks
Serial.println("Prepare. Reaction time measurements will begin shortly!");
for (int i = 0; i <= 2; i++)
{
digitalWrite(LED, HIGH);
delay(250);
digitalWrite(LED, LOW);
delay(250);
}
randomSeed(analogRead(A0));
randDelay = random(3000, 6000);
delay(randDelay);
}
void loop()
{
ButtonState = digitalRead(Button);//See if button is being pressed.
// Critera for trial start.
if (LEDState == LOW && ButtonState == LOW)
{
digitalWrite(LED, HIGH);
oldT = millis();
LEDState = HIGH;
}
// Criteria for trial end.
if (LEDState == HIGH && ButtonState == HIGH)
{
// Record time when button was pressed and turn off LED
newT = millis();
digitalWrite(LED, LOW);
// Print to console the time differnce between LED turn off to button pressed.
deltaT = newT - oldT;
Serial.println(deltaT);
// Reset trial and wait a random amount of time for next trial.
LEDState = LOW;
randDelay = random(1000, 3000);
delay(randDelay);
}
}