void setup() {
Serial.begin(9600);
randomSeed(analogRead(0));
pinMode(9, INPUT_PULLUP);
pinMode(7, INPUT_PULLUP);
Serial.println("Welcome to reaction test! You need to press green button when the LED is on");
Serial.println("Press red button to start the reaction test");
}
boolean button_7 = 0, system_on = 0, button_9 = 0;
unsigned long last_press = 0, reaction = 0, last_rand = 0;
void loop() {
button_7 = !digitalRead(7);
if (button_7 && millis() - last_press > 800){
system_on = 1;
last_press = millis();
Serial.println("Reaction test started! Good luck!");
}
if (system_on){
if (random(100) < 20 && millis() - last_rand > 4000){
digitalWrite(8, HIGH);
reaction = millis();
last_rand = millis();
}
button_9 = !digitalRead(9);
if (button_9 && millis() - last_press > 800){
if (digitalRead(8)){
reaction = millis() - reaction;
Serial.print("Your reaction:");
Serial.println(reaction);
digitalWrite(8, LOW);
} else {
Serial.println("You are pressed button too early");
}
last_press = millis();
}
}
}