void loop() {
-
handle_guess_button();
-
handle_start_button();
30
}
-
-
void handle_guess_button() {
-
if (guess_button.update()) {
-
if (guess_button.read() == HIGH) {
35
guess = (guess % 6) + 1;
-
output_result(guess);
-
Serial.print("Guess: ");
-
Serial.println(guess);
-
}
40
}
-
}
-
-
void handle_start_button() {
-
if (start_button.update()) {
45
if (start_button.read() == HIGH) {
-
const int result = random(1, 7);
-
output_result(result);
-
Serial.print("Result: ");
-
Serial.println(result);
50
if (guess > 0) {
-
if (result == guess) {
-
Serial.println("You win!");
-
hooray();
-
} else {
55
Serial.println("You lose!");
-
}
-
}
-
delay(2000);
-
guess = 0;
60
}
-
}
-
}
-
void output_result(const long result) {
-
digitalWrite(LED_BIT0, result & B001);
65
digitalWrite(LED_BIT1, result & B010);
-
digitalWrite(LED_BIT2, result & B100);
-
}
-
-
void hooray() {
70
for (unsigned int i = 0; i < 3; i++) {
-
output_result(7);
-
delay(500);
-
output_result(0);
-
delay(500);
75
}
-
}
-
report erratum • discuss
Building a Dice Game • 57
www.it-ebooks.info