#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include "pitches.h"
LiquidCrystal_I2C lcd(0x27, 16, 2);
const uint8_t ledPins[] = {9, 10, 11, 12};
const uint8_t buttonPins[] = {2, 3, 4, 5};
#define START_BUTTON_PIN 1
#define SPEAKER_PIN 8
#define MAX_GAME_LENGTH 100
const int gameTones[] = { NOTE_G3, NOTE_C4, NOTE_E4, NOTE_G5 };
uint8_t gameSequence[MAX_GAME_LENGTH] = {0};
uint8_t gameIndex = 0;
bool gameStarted = false;
int highestScore = 0;
void playImperialMarch() {
int melody[] = {
NOTE_A3, NOTE_A3, NOTE_F3, NOTE_C4,
NOTE_A3, NOTE_F3, NOTE_C4, NOTE_A3,
NOTE_E4, NOTE_E4, NOTE_E4,
NOTE_F4, NOTE_C4, NOTE_GS3, NOTE_A3,
NOTE_F3, NOTE_C4, NOTE_A3
};
int durations[] = {
500, 500, 350, 150,
500, 350, 150, 1000,
250, 250, 250,
500, 350, 150, 1000,
350, 150, 1000
};
for (unsigned int i = 0; i < sizeof(melody)/sizeof(int); i++) {
tone(SPEAKER_PIN, melody[i]);
delay(durations[i]);
noTone(SPEAKER_PIN);
delay(50);
}
}
void setup() {
Serial.begin(9600);
for (byte i = 0; i < 4; i++) {
pinMode(ledPins[i], OUTPUT);
pinMode(buttonPins[i], INPUT_PULLUP);
}
pinMode(START_BUTTON_PIN, INPUT_PULLUP);
pinMode(SPEAKER_PIN, OUTPUT);
lcd.init();
lcd.backlight();
randomSeed(analogRead(A3));
lcd.setCursor(0, 0);
lcd.print(" Bem-vindo ao");
lcd.setCursor(0, 1);
lcd.print("Memory Game JEDI");
delay(2000);
lcd.setCursor(0, 0);
lcd.print("___By KOLLER___");
lcd.setCursor(0, 1);
lcd.print("****************");
delay(2000);
playImperialMarch();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Pressione Start");
lcd.setCursor(0, 1);
lcd.print("para jogar!");
while (digitalRead(START_BUTTON_PIN) == HIGH) {
delay(100);
}
lcd.clear();
gameStarted = true;
}
void displayScore() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Score: ");
lcd.print(gameIndex);
lcd.setCursor(0, 1);
lcd.print("High Score: ");
lcd.print(highestScore);
}
void lightLedAndPlayTone(byte ledIndex) {
digitalWrite(ledPins[ledIndex], HIGH);
tone(SPEAKER_PIN, gameTones[ledIndex]);
delay(300);
digitalWrite(ledPins[ledIndex], LOW);
noTone(SPEAKER_PIN);
}
void playSequence() {
for (int i = 0; i < gameIndex; i++) {
byte currentLed = gameSequence[i];
lightLedAndPlayTone(currentLed);
delay(50);
}
}
byte readButtons() {
while (true) {
for (byte i = 0; i < 4; i++) {
if (digitalRead(buttonPins[i]) == LOW) {
return i;
}
}
delay(1);
}
}
void gameOver() {
Serial.print("Game over! your score: ");
Serial.println(gameIndex - 1);
if (gameIndex - 1 > highestScore) {
highestScore = gameIndex - 1;
}
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Game Over!");
lcd.setCursor(0, 1);
lcd.print("Score: ");
lcd.print(gameIndex - 1);
gameIndex = 0;
delay(200);
tone(SPEAKER_PIN, NOTE_DS5); delay(300);
tone(SPEAKER_PIN, NOTE_D5); delay(300);
tone(SPEAKER_PIN, NOTE_CS5); delay(300);
for (byte i = 0; i < 10; i++) {
for (int pitch = -10; pitch <= 10; pitch++) {
tone(SPEAKER_PIN, NOTE_C5 + pitch);
delay(5);
}
}
noTone(SPEAKER_PIN);
delay(1000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Pressione Start");
lcd.setCursor(0, 1);
lcd.print("para jogar!");
while (digitalRead(START_BUTTON_PIN) == HIGH) {
delay(100);
}
lcd.clear();
gameStarted = true;
gameIndex = 0;
}
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);
tone(SPEAKER_PIN, NOTE_G4); delay(150);
tone(SPEAKER_PIN, NOTE_E5); delay(150);
tone(SPEAKER_PIN, NOTE_C5); delay(150);
tone(SPEAKER_PIN, NOTE_D5); delay(150);
tone(SPEAKER_PIN, NOTE_G5); delay(150);
noTone(SPEAKER_PIN);
}
void loop() {
if (gameStarted) {
displayScore();
gameSequence[gameIndex] = random(0, 4);
gameIndex++;
if (gameIndex >= MAX_GAME_LENGTH) {
gameIndex = MAX_GAME_LENGTH - 1;
}
playSequence();
if (!checkUserSequence()) {
gameOver();
}
delay(300);
if (gameIndex > 0) {
playLevelUpSound();
delay(300);
}
}
}
Start
Reset