We could just use the delay() function to wait the random amount of time determined for the countdown. Something like this:
// 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().
// 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.
Page last edited December 09, 2016
Text editor powered by tinymce.