diff --git a/fastest_presser/src/main.cpp b/fastest_presser/src/main.cpp index 8387b00..acd384c 100644 --- a/fastest_presser/src/main.cpp +++ b/fastest_presser/src/main.cpp @@ -2,38 +2,63 @@ #include #include -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() { pinMode(LED_BUILTIN, OUTPUT); digitalWrite(LED_BUILTIN, LOW); + pinMode(PLAYER_A, INPUT_PULLUP); + pinMode(PLAYER_B, INPUT_PULLUP); randomSeed(analogRead(0)); - lcd.begin(16, 2); + lcd.begin(LCD_COLS, LCD_ROWS); } void loop() { + waitForReady(); displayCountdown(); delay(1000); byte randomWait = random(6, 32); lcd.clear(); for (byte i = 0; i < randomWait; i++) { - if (i == 16) { - lcd.setCursor(0, 1); + if (i == LCD_COLS) { + lcdSecondLine(); } lcd.print("."); delay(500); } lcd.clear(); lcd.print("Maintenant !"); - // TODO add code to determine winner, maybe use interrupts? - delay(2000); + + const byte winner = computeWinner(); 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() { - lcd.setCursor(0, 0); + lcd.clear(); lcd.print("Prets ?"); - lcd.setCursor(0, 1); + lcdSecondLine(); displayDigit("3"); delay(600); displayDigit("2"); @@ -49,3 +74,58 @@ void displayDigit(const char *digit) { } 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(); + } +} diff --git a/fastest_presser/src/main.h b/fastest_presser/src/main.h index be4186a..3d9277f 100644 --- a/fastest_presser/src/main.h +++ b/fastest_presser/src/main.h @@ -7,5 +7,9 @@ void displayCountdown(); void displayDigit(const char *digit); +byte computeWinner(); +void lcdSecondLine(); +void printScore(); +void waitForReady(); #endif //FASTEST_PRESSER_MAIN_H