We could just use the delay() function to wait the random amount of time determined for the countdown. Something like this:

Download: file
  // Wait a random period of time
  unsigned long countTime = random(SHORTEST_DELAY, LONGEST_DELAY);
  delay(countTime);

However, we need to monitor the buttons during the countdown to make sure one of the players did not cheat (misdraw). We can do this by using a while() loop instead of delay() for the countdown and polling the buttons inside the loop. That would look something like the following, note that there is now no use of delay().

Download: file
  // Wait a random period of time
  unsigned long countTime = random(SHORTEST_DELAY, LONGEST_DELAY);
  unsigned long startTime = millis();
  while (millis() - startTime < countTime) {
    // Check if player draws too soon.
    if (CircuitPlayground.leftButton()) showOutcome(1, false);
    if (CircuitPlayground.rightButton()) showOutcome(2, false); 
  } 

But what's the showOutcome() function? Well, we'll talk about that next.

This guide was first published on Dec 29, 2016. It was last updated on Dec 29, 2016.
This page (The Countdown) was last updated on Jun 03, 2020.