/*
Simon Says Game - 3 LED Version
*/
#define led7 7 // white button led
#define led6 6 // green button led
#define led5 5 // red button led
#define button7 led7 + 4 // white button
#define button6 led6 + 4 // green button
#define button5 led5 + 4 // red button
#define levelsInGame 50
#define buzzer 3
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 32 // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET 4
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
int simonSaid[levelsInGame]; // initialize the array
void setup() {
Serial.begin(9600);
// Initialize digital pins as outputs for LEDs in the buttons
pinMode(led7, OUTPUT);
pinMode(led6, OUTPUT);
pinMode(led5, OUTPUT);
digitalWrite(led7, LOW);
digitalWrite(led6, LOW);
digitalWrite(led5, LOW);
pinMode(button7, INPUT_PULLUP); // Set the button pins as inputs
pinMode(button6, INPUT_PULLUP);
pinMode(button5, INPUT_PULLUP);
randomSeed(analogRead(0)); // make our random numbers more random
// Populate the array with random 'colors'
for (int i = 0; i <= levelsInGame; i++) {
simonSaid[i] = random(5, 8); // Random numbers between 5 and 7 (inclusive)
}
for (int j = 0; j < levelsInGame; j++) { // Print the array to the serial monitor
Serial.println(simonSaid[j]);
}
// Initialize the OLED display
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;) ;
}
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(1, 0);
display.println("Simon Says");
display.setTextSize(1);
display.println("by Lsyahaba");
display.display();
delay(2000);
}
void loop() {
bool gameOver = 0;
while (!gameOver) {
for (int i = 1; i < (levelsInGame + 1); i++) { // For each level
Serial.println();
Serial.print("Level ");
Serial.println(i);
display.clearDisplay();
display.setTextSize(i < 10 ? 3 : 2);
display.setTextColor(WHITE);
display.setCursor(1, 0);
display.print("Level ");
display.println(i);
display.display();
delay(400);
// Play the color sequence for the current level
for (int g = 0; g < i; g++) {
Serial.print("Colour number ");
Serial.print(g + 1);
Serial.print(" is the button on D");
Serial.println(simonSaid[g]);
digitalWrite(simonSaid[g], HIGH);
playBuzzer(simonSaid[g]);
delay(200);
digitalWrite(simonSaid[g], LOW);
delay(50);
}
// Check if the player presses the correct buttons
for (int g = 0; g < i; g++) {
int sensorButton7 = 1;
int sensorButton6 = 1;
int sensorButton5 = 1;
int buttonPressed = 0;
// Wait for any button press
while (sensorButton5 == HIGH && sensorButton6 == HIGH && sensorButton7 == HIGH) {
sensorButton7 = digitalRead(button7);
sensorButton6 = digitalRead(button6);
sensorButton5 = digitalRead(button5);
}
delay(200); // Debounce delay
// Determine which button was pressed
if (sensorButton7 == LOW) {
buttonPressed = 7;
} else if (sensorButton6 == LOW) {
buttonPressed = 6;
} else if (sensorButton5 == LOW) {
buttonPressed = 5;
}
Serial.print("Button pressed is = ");
Serial.println(buttonPressed);
digitalWrite(buttonPressed, HIGH); // Turn on the button light
playBuzzer(buttonPressed); // Play sound for the button pressed
digitalWrite(buttonPressed, LOW); // Turn off the button light
// Check if the pressed button matches the correct one
if (buttonPressed == simonSaid[g]) {
Serial.println("Correct button - well done");
} else {
Serial.println("Wrong button pressed. Game over.");
gameOver = 1;
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(1, 0);
display.println("GAME OVER");
display.print("at lvl ");
display.print(i);
display.display();
tone(buzzer, 350); // Play game over tone
digitalWrite(led7, HIGH);
digitalWrite(led6, HIGH);
digitalWrite(led5, HIGH);
delay(2000);
noTone(buzzer);
digitalWrite(led7, LOW);
digitalWrite(led6, LOW);
digitalWrite(led5, LOW);
delay(2000);
softReset();
}
}
}
}
}
void softReset() {
asm volatile (" jmp 0");
}
void playBuzzer(int button) {
if (button == 5) {
tone(buzzer, 950);
delay(300);
noTone(buzzer);
} else if (button == 6) {
tone(buzzer, 850);
delay(300);
noTone(buzzer);
} else if (button == 7) {
tone(buzzer, 750);
delay(300);
noTone(buzzer);
}
}