#include "pitches.h"
#include <Keypad.h>
#include <LiquidCrystal_I2C.h>
const byte ledPins[] = {14, 15, 16, 17, 11, 12};
#define SPEAKER_PIN 13
#define MAX_GAME_LENGTH 100
const int gameTones[] = { NOTE_G3, NOTE_C4, NOTE_E4, NOTE_G5};
int speed = 300;
int gameSequence[MAX_GAME_LENGTH] = {0};
byte gameIndex = 0;
int score = 0;
int maxscore = 0;
const uint8_t ROWS = 4;
const uint8_t COLS = 4;
uint8_t colPins[COLS] = { 5, 4, 3, 2 };
uint8_t rowPins[ROWS] = { 9, 8, 7, 6 };
char keys[ROWS][COLS] = {
{ 'A', 'B', 'C', 'D' },
{ 'E', 'F', 'G', 'H' },
{ 'I', 'J', 'K', 'L' },
{ 'M', 'O', 'P', 'Q' }
};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
LiquidCrystal_I2C lcd(0x27, 20, 4);
char key = NO_KEY;
void setup() {
lcd.init();
lcd.backlight();
lcd.setCursor(1, 0);
lcd.print("GOOD LUCK!");
lcd.setCursor(1, 1);
lcd.print("HAVE FUN!");
delay(1000);
lcd.clear();
Serial.begin(9600);
for (byte i = 0; i < 7; i++) {
pinMode(ledPins[i], OUTPUT);
}
pinMode(SPEAKER_PIN, OUTPUT);
randomSeed(analogRead(A7));
}
void lightLedAndPlayTone(byte ledIndex) {
Serial.println(ledIndex);
digitalWrite(ledPins[ledIndex - 65], HIGH);
tone(SPEAKER_PIN, gameTones[ledIndex - 65]);
delay(speed);
digitalWrite(ledPins[ledIndex - 65], LOW);
noTone(SPEAKER_PIN);
}
void playSequence() {
for (int i = 0; i < gameIndex; i++) {
byte currentLed = gameSequence[i];
lightLedAndPlayTone(currentLed);
delay(100);
}
}
byte readButtons() {
while(key == NO_KEY) {
key = keypad.getKey();
if (key != NO_KEY) {
Serial.println(key);
return key;
}
delay(100);
}
delay(1000);
}
void gameOver() {
lcd.clear();
lcd.setCursor(1, 0);
lcd.print("GAME OVER :(");
delay(1000);
lcd.setCursor(1, 0);
lcd.print("Your score : ");
lcd.setCursor(14, 0);
lcd.print(score);
lcd.setCursor(1, 1);
lcd.print("High score : ");
lcd.setCursor(14, 1);
lcd.print(maxscore);
gameIndex = 0;
delay(1000);
lcd.clear();
score = 0;
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(500);
}
bool checkUserSequence() {
for (int i = 0; i < gameIndex; i++) {
byte expectedButton = gameSequence[i];
Serial.println(expectedButton);
byte actualButton = readButtons();
key = NO_KEY;
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() {
lcd.setCursor(1, 0);
lcd.print("Your score : ");
lcd.setCursor(14, 0);
lcd.print(score);
lcd.setCursor(1, 1);
lcd.print("High score : ");
lcd.setCursor(14, 1);
lcd.print(maxscore);
gameSequence[gameIndex] = random(65, 71);
gameIndex++;
if (gameIndex >= MAX_GAME_LENGTH) {
gameIndex = MAX_GAME_LENGTH - 1;
}
playSequence();
if (!checkUserSequence()) {
gameOver();
} else {
Serial.println(speed);
score = score + 1;
if (score == maxscore || score > maxscore)
maxscore = score;
if (speed >= 50)
speed = speed - 20;
}
delay(300);
if (gameIndex > 0) {
playLevelUpSound();
delay(300);
}
}