Hello, so I am working on a project and I want to check if a button is pressed while the led is on. But I also want the led to be on for a certain amount of time (s) and if i choose to put a delay it will not continue to the next if statement for the button. I was wondering if anyone had an idea of making a timer to turn the led on for the (s) time but at the same time check whether the button is pressed in that time space.
thank you in advance!
Code:
void loop() {
buttonState1 = digitalRead(b1);
buttonState2 = digitalRead(b2);
buttonState3 = digitalRead(b3);
buttonState4 = digitalRead(b4);
buttonState5 = digitalRead(b5);
// digitalWrite(led1, HIGH);
// digitalWrite(led2, LOW);
// digitalWrite(led3, LOW);
// digitalWrite(led4, LOW);
// digitalWrite(led5, LOW);
do {
ledON = random(1, 6);
}
while(ledPrevious == ledON);
ledPrevious = ledON;
if (ledON == 1) {
digitalWrite(led1, HIGH);
delay(s);
digitalWrite(led1, LOW);
}
else if (ledON == 2) {
digitalWrite(led2, HIGH);
delay(s);
digitalWrite(led2, LOW);
}
else if (ledON == 3) {
digitalWrite(led3, HIGH);
delay(s);
digitalWrite(led3, LOW);
}
else if (ledON == 4) {
digitalWrite(led4, HIGH);
delay(s);
digitalWrite(led4, LOW);
}
else if (ledON == 5) {
digitalWrite(led5, HIGH);
delay(s);
digitalWrite(led5, LOW);
}
if (buttonState1 == LOW && ledON == 1) {
//delay
score++;
Serial.print(score);
}
else if (buttonState2 == LOW && ledON == 2) {
score++;
Serial.print(score);
}
else if (buttonState3 == LOW && ledON == 3) {
score++;
Serial.print(score);
}
else if (buttonState4 == LOW && ledON == 4) {
score++;
Serial.print(score);
}
else if (buttonState5 == LOW && ledON == 5) {
score++;
Serial.print(score);
}
}