I am making a game where you need to push the button when the LED lights up but it is not registering the push at the right time. I need to push the button when LED is about to go off.

My code:

randNumber = random(2);

if(randNumber == 0){
  digitalWrite(9, HIGH);
  digitalWrite(9, LOW);

if (btnAddFirst.pressed() && randNumber == 0){
  Serial.println("You did it");
else {
  Serial.println("u failed");

1 Answer 1


Don't use delay(), since nothing can happen (not even reading a button) while that delay is running.

Instead use millis() as in the BlinkWithoutDelay example sketch.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.