I have a simple reaction time game on a protoboard where it does a countdown on a display that goes down somewhat randomly, then when it reaches 0 you have to press the button and an LED will light up relating to how well you did. That all works fine but Im wondering how to make it so you cant just hold down the button/spam it to get the best reaction time possible. I also want it to check during the delays as thats basically all of the countdown. This is my code.
int good=2;
int ok=3;
int bad=4;
int button=6;
int b=12;
int c=11;
int d=9;
int a=8;
unsigned long changeTime;
void setup()
{
Serial.begin(9600);
pinMode(good,OUTPUT);
pinMode(ok,OUTPUT);
pinMode(bad,OUTPUT);
pinMode(button,INPUT);
pinMode(a,OUTPUT);
pinMode(b,OUTPUT);
pinMode(c,OUTPUT);
pinMode(d,OUTPUT);
digitalWrite(a,HIGH);
digitalWrite(d,HIGH);
Serial.begin(9600);
}
void loop()
{
delay(1000);
digitalWrite(good,LOW);
digitalWrite(ok,LOW);
digitalWrite(bad,LOW);
delay(random(250, 1000));
digitalWrite(a,LOW);
delay(random(250, 1000));
digitalWrite(a,HIGH);
digitalWrite(d,LOW);
digitalWrite(b,HIGH);
digitalWrite(c,HIGH);
delay(random(250, 1000));
digitalWrite(a,LOW);
delay(random(250, 1000));
digitalWrite(a,HIGH);
digitalWrite(b,LOW);
delay(random(250, 1000));
digitalWrite(a,LOW);
delay(random(250, 1000));
digitalWrite(a,HIGH);
digitalWrite(b,HIGH);
digitalWrite(c,LOW);
delay(random(250, 1000));
digitalWrite(a,LOW);
delay(random(250, 1000));
digitalWrite(b,LOW);
digitalWrite(a,HIGH);
delay(random(250, 1000));
digitalWrite(a,LOW);
digitalWrite(b,LOW);
digitalWrite(c,LOW);
digitalWrite(d,LOW);
changeTime=millis();
while(true){
int state=digitalRead(button);
Serial.println(state);
while(state==HIGH){
if((millis()-changeTime)<=100){
digitalWrite(good,HIGH);
digitalWrite(a,HIGH);
digitalWrite(d,HIGH);
state=LOW;
return;
}
else if((millis()-changeTime)<=300 && (millis()-changeTime)>100){
digitalWrite(ok,HIGH);
digitalWrite(a,HIGH);
digitalWrite(d,HIGH);
state=LOW;
return;
}
else if((millis()-changeTime)>400){
digitalWrite(bad,HIGH);
digitalWrite(a,HIGH);
digitalWrite(d,HIGH);
state=LOW;
return;
}
}
}
}