/**
Simon Game for Arduino with Score display and LCD I2C Time Display
Copyright (C) 2022, Uri Shaked
Released under the MIT License.
*/
#include "pitches.h"
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Constants - define pin numbers for LEDs, buttons, speaker, and game tones:
const uint8_t ledPins[] = {9, 10, 11, 12};
const uint8_t buttonPins[] = {2, 3, 4, 5};
#define SPEAKER_PIN 8
// Pins connected to the 74HC595 shift register (for game score display):
const int LATCH_PIN = A1;
const int DATA_PIN = A0;
const int CLOCK_PIN = A2;
#define MAX_GAME_LENGTH 100
const int gameTones[] = { NOTE_G3, NOTE_C4, NOTE_E4, NOTE_G5};
// LCD I2C setup (address 0x27, 16 columns, 2 rows):
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Global variables to store the game state and time tracking
uint8_t gameSequence[MAX_GAME_LENGTH] = {0};
uint8_t gameIndex = 0;
unsigned long startTime = 0;
unsigned long elapsedTime = 0;
unsigned long lastUpdateTime = 0;
unsigned long gameDelay = 300; // Time between actions
// Digit table for the 7-segment display
const uint8_t digitTable[] = {
0b11000000,
0b11111001,
0b10100100,
0b10110000,
0b10011001,
0b10010010,
0b10000010,
0b11111000,
0b10000000,
0b10010000,
};
const uint8_t DASH = 0b10111111;
void sendScore(uint8_t high, uint8_t low) {
digitalWrite(LATCH_PIN, LOW);
shiftOut(DATA_PIN, CLOCK_PIN, MSBFIRST, low);
shiftOut(DATA_PIN, CLOCK_PIN, MSBFIRST, high);
digitalWrite(LATCH_PIN, HIGH);
}
void displayScore() {
int high = gameIndex % 100 / 10;
int low = gameIndex % 10;
sendScore(high ? digitTable[high] : 0xff, digitTable[low]);
}
void playStartMelody() {
tone(SPEAKER_PIN, NOTE_C4);
delay(200);
tone(SPEAKER_PIN, NOTE_E4);
delay(200);
tone(SPEAKER_PIN, NOTE_G4);
delay(200);
noTone(SPEAKER_PIN);
}
void startCountdown() {
// Afficher le compte à rebours de 3 secondes sur l'écran LCD
for (int i = 3; i > 0; i--) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Starting in: ");
lcd.setCursor(0, 1);
lcd.print(i);
playStartMelody(); // Jouer la petite mélodie
delay(1000); // Attendre 1 seconde entre chaque numéro du compte à rebours
}
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Go!"); // Afficher "Go!" lorsque la partie commence
playStartMelody(); // Jouer la mélodie au début du jeu
delay(500);
lcd.clear();
}
void setup() {
Serial.begin(9600);
lcd.begin(16, 2); // Initialiser l'écran LCD avec 16 colonnes et 2 lignes
lcd.backlight(); // Allumer le rétroéclairage
for (byte i = 0; i < 4; i++) {
pinMode(ledPins[i], OUTPUT);
pinMode(buttonPins[i], INPUT_PULLUP);
}
pinMode(SPEAKER_PIN, OUTPUT);
pinMode(LATCH_PIN, OUTPUT);
pinMode(CLOCK_PIN, OUTPUT);
pinMode(DATA_PIN, OUTPUT);
randomSeed(analogRead(A3));
// Initialiser l'affichage du temps sur l'écran LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Time: 0 s");
startTime = millis();
startCountdown(); // Démarrer le compte à rebours
}
void lightLedAndPlayTone(byte ledIndex) {
digitalWrite(ledPins[ledIndex], HIGH);
tone(SPEAKER_PIN, gameTones[ledIndex]);
delay(300); // Cette ligne sera modifiée
digitalWrite(ledPins[ledIndex], LOW);
noTone(SPEAKER_PIN);
}
void playSequence() {
for (int i = 0; i < gameIndex; i++) {
byte currentLed = gameSequence[i];
lightLedAndPlayTone(currentLed);
delay(50); // Cette ligne sera modifiée
}
}
byte readButtons() {
while (true) {
elapsedTime = (millis() - startTime) / 1000;
lcd.setCursor(0, 0);
lcd.print("Time: ");
lcd.print(elapsedTime);
lcd.print(" s ");
for (byte i = 0; i < 4; i++) {
byte buttonPin = buttonPins[i];
if (digitalRead(buttonPin) == LOW) {
return i;
}
}
delay(1); // Petite pause pour éviter une surcharge du processeur
}
}
void gameOver() {
Serial.print("Game over! your score: ");
Serial.println(gameIndex - 1);
gameIndex = 0;
delay(200);
// Display "Game Over" on the LCD with time
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Game Over!");
lcd.setCursor(0, 1);
lcd.print("Time: ");
lcd.print(elapsedTime);
lcd.print(" s");
// Play game over sound
tone(SPEAKER_PIN, NOTE_DS5);
delay(300); // Cette ligne sera modifiée
tone(SPEAKER_PIN, NOTE_D5);
delay(300); // Cette ligne sera modifiée
tone(SPEAKER_PIN, NOTE_CS5);
delay(300); // Cette ligne sera modifiée
for (byte i = 0; i < 10; i++) {
for (int pitch = -10; pitch <= 10; pitch++) {
tone(SPEAKER_PIN, NOTE_C5 + pitch);
delay(5); // Cette ligne sera modifiée
}
}
noTone(SPEAKER_PIN);
sendScore(DASH, DASH);
delay(500); // Cette ligne sera modifiée
// Reset the time to 0 for the next game
startTime = millis(); // Reset start time
startCountdown(); // Relancer le compte à rebours après le game over
}
bool checkUserSequence() {
for (int i = 0; i < gameIndex; i++) {
byte expectedButton = gameSequence[i];
byte actualButton = readButtons();
lightLedAndPlayTone(actualButton);
if (expectedButton != actualButton) {
return false;
}
}
return true;
}
void playLevelUpSound() {
tone(SPEAKER_PIN, NOTE_E4);
delay(150); // Cette ligne sera modifiée
tone(SPEAKER_PIN, NOTE_G4);
delay(150); // Cette ligne sera modifiée
tone(SPEAKER_PIN, NOTE_E5);
delay(150); // Cette ligne sera modifiée
tone(SPEAKER_PIN, NOTE_C5);
delay(150); // Cette ligne sera modifiée
tone(SPEAKER_PIN, NOTE_D5);
delay(150); // Cette ligne sera modifiée
tone(SPEAKER_PIN, NOTE_G5);
delay(150); // Cette ligne sera modifiée
noTone(SPEAKER_PIN);
}
void loop() {
unsigned long currentTime = millis();
// Mise à jour de l'affichage du temps
if (currentTime - lastUpdateTime >= 1000) {
elapsedTime = (millis() - startTime) / 1000;
lcd.setCursor(0, 0);
lcd.print("Time: ");
lcd.print(elapsedTime);
lcd.print(" s ");
lastUpdateTime = currentTime;
}
displayScore();
// Add a random color to the sequence
gameSequence[gameIndex] = random(0, 4);
gameIndex++;
if (gameIndex >= MAX_GAME_LENGTH) {
gameIndex = MAX_GAME_LENGTH - 1;
}
playSequence();
if (!checkUserSequence()) {
gameOver();
}
if (gameIndex > 0) {
playLevelUpSound();
}
}