Add menus and winner computation

This commit is contained in:
Gabriel Augendre 2021-04-29 07:23:14 +02:00
parent fac1fbdd1e
commit c297c3d09b
2 changed files with 92 additions and 8 deletions

View file

@ -2,38 +2,63 @@
#include <LiquidCrystal.h> #include <LiquidCrystal.h>
#include <main.h> #include <main.h>
LiquidCrystal lcd(12, 11, 2, 3, 4, 5); #define LCD_COLS 16
#define LCD_ROWS 2
#define PLAYER_A 2
#define PLAYER_B 3
#define TIE 0
LiquidCrystal lcd(12, 11, 4, 5, 6, 7);
unsigned int scoreA = 0;
unsigned int scoreB = 0;
void setup() { void setup() {
pinMode(LED_BUILTIN, OUTPUT); pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, LOW); digitalWrite(LED_BUILTIN, LOW);
pinMode(PLAYER_A, INPUT_PULLUP);
pinMode(PLAYER_B, INPUT_PULLUP);
randomSeed(analogRead(0)); randomSeed(analogRead(0));
lcd.begin(16, 2); lcd.begin(LCD_COLS, LCD_ROWS);
} }
void loop() { void loop() {
waitForReady();
displayCountdown(); displayCountdown();
delay(1000); delay(1000);
byte randomWait = random(6, 32); byte randomWait = random(6, 32);
lcd.clear(); lcd.clear();
for (byte i = 0; i < randomWait; i++) { for (byte i = 0; i < randomWait; i++) {
if (i == 16) { if (i == LCD_COLS) {
lcd.setCursor(0, 1); lcdSecondLine();
} }
lcd.print("."); lcd.print(".");
delay(500); delay(500);
} }
lcd.clear(); lcd.clear();
lcd.print("Maintenant !"); lcd.print("Maintenant !");
// TODO add code to determine winner, maybe use interrupts?
delay(2000); const byte winner = computeWinner();
lcd.clear(); lcd.clear();
if (winner == PLAYER_A) {
lcd.print("Bravo joueur A !");
scoreA += 1;
}
else if (winner == PLAYER_B) {
lcd.print("Bravo joueur B !");
scoreB += 1;
}
else {
lcd.print("Egalite");
}
delay(4000);
printScore();
} }
void displayCountdown() { void displayCountdown() {
lcd.setCursor(0, 0); lcd.clear();
lcd.print("Prets ?"); lcd.print("Prets ?");
lcd.setCursor(0, 1); lcdSecondLine();
displayDigit("3"); displayDigit("3");
delay(600); delay(600);
displayDigit("2"); displayDigit("2");
@ -49,3 +74,58 @@ void displayDigit(const char *digit) {
} }
lcd.print(" "); lcd.print(" ");
} }
byte computeWinner() {
int playerA = digitalRead(PLAYER_A);
int playerB = digitalRead(PLAYER_B);
while (playerA == HIGH && playerB == HIGH) {
// Wait for a button press
playerA = digitalRead(PLAYER_A);
playerB = digitalRead(PLAYER_B);
delay(1);
}
if (playerA == LOW && playerB == HIGH) {
return PLAYER_A;
}
if (playerA == HIGH && playerB == LOW) {
return PLAYER_B;
}
else {
return TIE;
}
}
void lcdSecondLine() {
lcd.setCursor(0, 1);
}
void printScore() {
lcd.clear();
lcd.print("A: ");
lcd.print(scoreA);
lcdSecondLine();
lcd.print("B: ");
lcd.print(scoreB);
delay(2000);
}
void waitForReady() {
lcd.clear();
lcd.print("A: Commencer");
lcdSecondLine();
lcd.print("B: Scores");
int playerA = digitalRead(PLAYER_A);
int playerB = digitalRead(PLAYER_B);
while (playerA == HIGH && playerB == HIGH) {
// Wait for a button press
playerA = digitalRead(PLAYER_A);
playerB = digitalRead(PLAYER_B);
delay(1);
}
if (playerB == LOW) {
printScore();
waitForReady();
}
}

View file

@ -7,5 +7,9 @@
void displayCountdown(); void displayCountdown();
void displayDigit(const char *digit); void displayDigit(const char *digit);
byte computeWinner();
void lcdSecondLine();
void printScore();
void waitForReady();
#endif //FASTEST_PRESSER_MAIN_H #endif //FASTEST_PRESSER_MAIN_H