arduino-toy-projects/fastest_presser/src/main.cpp

183 lines
4.1 KiB
C++
Raw Normal View History

2021-04-28 21:59:11 +02:00
#include <Arduino.h>
2021-04-28 23:27:30 +02:00
#include <LiquidCrystal.h>
#include <main.h>
2021-04-29 07:23:14 +02:00
#define LCD_COLS 16
#define LCD_ROWS 2
2022-03-07 17:28:34 +01:00
#define PLAYER_GREEN 2
#define PLAYER_RED 3
#define TIE 1
2021-04-29 07:23:14 +02:00
2021-04-29 13:31:09 +02:00
LiquidCrystal lcd(9, 8, 4, 5, 6, 7);
2022-03-07 17:28:34 +01:00
int scoreGreen = 0;
int scoreRed = 0;
2021-04-28 23:27:30 +02:00
2021-04-28 21:59:11 +02:00
void setup() {
2021-04-28 23:27:30 +02:00
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, LOW);
2022-03-07 17:28:34 +01:00
pinMode(PLAYER_GREEN, INPUT_PULLUP);
pinMode(PLAYER_RED, INPUT_PULLUP);
2021-04-28 23:27:30 +02:00
randomSeed(analogRead(0));
2021-04-29 07:23:14 +02:00
lcd.begin(LCD_COLS, LCD_ROWS);
2021-04-28 21:59:11 +02:00
}
void loop() {
2021-04-29 07:23:14 +02:00
waitForReady();
2021-04-28 23:27:30 +02:00
displayCountdown();
delay(1000);
byte randomWait = random(6, 32);
2022-03-07 17:28:34 +01:00
byte cheating = 0;
2021-04-28 23:27:30 +02:00
lcd.clear();
for (byte i = 0; i < randomWait; i++) {
2021-04-29 07:23:14 +02:00
if (i == LCD_COLS) {
lcdSecondLine();
2021-04-28 23:27:30 +02:00
}
lcd.print(".");
2021-04-29 07:23:14 +02:00
2022-03-07 17:28:34 +01:00
cheating = isCheating();
if (cheating) {
break;
}
2021-04-29 07:23:14 +02:00
}
2022-03-07 17:28:34 +01:00
lcd.clear();
if (cheating) {
lcd.print("Pas de triche !");
lcdSecondLine();
if (cheating == PLAYER_GREEN) {
lcd.print("Joueur vert: -1");
scoreGreen -= 1;
}
else if (cheating == PLAYER_RED) {
lcd.print("Joueur rouge: -1");
scoreRed -= 1;
}
else {
lcd.print("Joueurs : -1");
scoreGreen -= 1;
scoreRed -= 1;
}
2021-04-29 07:23:14 +02:00
}
else {
2022-03-07 17:28:34 +01:00
lcd.print("Maintenant !");
const byte winner = computeWinner();
lcd.clear();
if (winner == PLAYER_GREEN) {
lcd.print("Bravo joueur");
lcdSecondLine();
lcd.print("vert !");
scoreGreen += 1;
}
else if (winner == PLAYER_RED) {
lcd.print("Bravo joueur");
lcd.print("rouge !");
scoreRed += 1;
}
else {
lcd.print("Egalite");
}
2021-04-29 07:23:14 +02:00
}
delay(4000);
printScore();
2021-04-28 23:27:30 +02:00
}
void displayCountdown() {
2021-04-29 07:23:14 +02:00
lcd.clear();
2021-04-28 23:27:30 +02:00
lcd.print("Prets ?");
2021-04-29 07:23:14 +02:00
lcdSecondLine();
2021-04-28 23:27:30 +02:00
displayDigit("3");
delay(600);
displayDigit("2");
delay(600);
displayDigit("1");
}
void displayDigit(const char *digit) {
lcd.print(digit);
for (byte i = 0; i < 3; i++) {
delay(200);
lcd.print(".");
}
lcd.print(" ");
}
2021-04-29 07:23:14 +02:00
byte computeWinner() {
2022-03-07 17:28:34 +01:00
int playerGreen = digitalRead(PLAYER_GREEN);
int playerRed = digitalRead(PLAYER_RED);
while (playerGreen == HIGH && playerRed == HIGH) {
2021-04-29 07:23:14 +02:00
// Wait for a button press
2022-03-07 17:28:34 +01:00
playerGreen = digitalRead(PLAYER_GREEN);
playerRed = digitalRead(PLAYER_RED);
2021-04-29 07:23:14 +02:00
}
2022-03-07 17:28:34 +01:00
if (playerGreen == LOW && playerRed == HIGH) {
return PLAYER_GREEN;
2021-04-29 07:23:14 +02:00
}
2022-03-07 17:28:34 +01:00
if (playerGreen == HIGH && playerRed == LOW) {
return PLAYER_RED;
2021-04-29 07:23:14 +02:00
}
else {
return TIE;
}
}
2022-03-07 17:28:34 +01:00
byte isCheating() {
int playerGreen = digitalRead(PLAYER_GREEN);
int playerRed = digitalRead(PLAYER_RED);
int timer = 0;
int maxTimer = 500;
while (timer < maxTimer && playerGreen == HIGH && playerRed == HIGH) {
// Wait for a button press
playerGreen = digitalRead(PLAYER_GREEN);
playerRed = digitalRead(PLAYER_RED);
timer += 10;
delay(10);
}
if (playerGreen == LOW && playerRed == HIGH) {
return PLAYER_GREEN;
}
else if (playerGreen == HIGH && playerRed == LOW) {
return PLAYER_RED;
}
else if (playerGreen == LOW && playerRed == LOW) {
return TIE;
}
else {
return 0;
}
}
2021-04-29 07:23:14 +02:00
void lcdSecondLine() {
lcd.setCursor(0, 1);
}
void printScore() {
lcd.clear();
2022-03-07 17:28:34 +01:00
lcd.print("Vert: ");
lcd.print(scoreGreen);
2021-04-29 07:23:14 +02:00
lcdSecondLine();
2022-03-07 17:28:34 +01:00
lcd.print("Rouge: ");
lcd.print(scoreRed);
2021-04-29 07:23:14 +02:00
delay(2000);
}
void waitForReady() {
lcd.clear();
2022-03-07 17:28:34 +01:00
lcd.print("Vert: Commencer");
2021-04-29 07:23:14 +02:00
lcdSecondLine();
2022-03-07 17:28:34 +01:00
lcd.print("Rouge: Scores");
2021-04-29 07:23:14 +02:00
2022-03-07 17:28:34 +01:00
int playerGreen = digitalRead(PLAYER_GREEN);
int playerRed = digitalRead(PLAYER_RED);
while (playerGreen == HIGH && playerRed == HIGH) {
2021-04-29 07:23:14 +02:00
// Wait for a button press
2022-03-07 17:28:34 +01:00
playerGreen = digitalRead(PLAYER_GREEN);
playerRed = digitalRead(PLAYER_RED);
2021-04-29 07:23:14 +02:00
}
2022-03-07 17:28:34 +01:00
if (playerRed == LOW) {
2021-04-29 07:23:14 +02:00
printScore();
waitForReady();
}
}