#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
// Define screen dimensions
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
// Create display object
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
// Define button pins
const int buttonPins[5] = {2, 3, 4, 5, 6};
// Define the corresponding notes for each button
const int noteFrequencies[5] = {262, 294, 330, 349, 392}; // C4, D4, E4, F4, G4
// Define the speaker pin
const int speakerPin = 8;
// Define mode pins for the slider switch
const int pinMode1 = 7;
const int pinMode2 = 8;
// Game variables
int sequence[10]; // Array to hold the sequence of notes
int sequenceLength = 5; // Initial length of the sequence
int currentStep = 0; // Current step in the sequence
bool userTurn = false; // Flag to indicate if it's the user's turn
void setup() {
Serial.begin(9600); // Initialize serial communication for debugging
// Initialize buttons
for (int i = 0; i < 5; i++) {
pinMode(buttonPins[i], INPUT);
}
// Initialize the speaker pin
pinMode(speakerPin, OUTPUT);
// Initialize mode pins
pinMode(pinMode1, INPUT);
pinMode(pinMode2, INPUT);
// Initialize the display
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
display.display();
delay(2000);
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
// Generate a random sequence
randomSeed(analogRead(0));
for (int i = 0; i < 10; i++) {
sequence[i] = random(0, 5);
}
// Print the generated sequence for debugging
Serial.print("Generated sequence: ");
for (int i = 0; i < sequenceLength; i++) {
Serial.print(sequence[i]);
Serial.print(" ");
}
Serial.println();
}
void loop() {
// Read the mode from the slider switch
bool mode1 = digitalRead(pinMode1);
bool mode2 = digitalRead(pinMode2);
if (!mode1 && !mode2) {
offState();
} else if (mode1 && !mode2) {
freePlayMode();
} else if (mode1 && mode2) {
sequenceMode();
}
}
void offState() {
display.clearDisplay();
display.setCursor(0, 0);
display.println("Off state");
display.display();
noTone(speakerPin); // Ensure no sound is playing
}
void freePlayMode() {
display.clearDisplay();
display.setCursor(0, 0);
display.println("Free-play mode");
display.display();
for (int i = 0; i < 5; i++) {
if (digitalRead(buttonPins[i]) == HIGH) {
playNoteAndDisplay(i);
while (digitalRead(buttonPins[i]) == HIGH); // Wait for button release
noTone(speakerPin);
}
}
}
void sequenceMode() {
if (userTurn) {
checkUserInput();
} else {
displaySequence();
}
}
void displaySequence() {
display.clearDisplay();
display.setCursor(0, 0);
display.println("Watch the sequence:");
display.display();
delay(1000); // Wait for a moment before displaying the sequence
for (int i = 0; i < sequenceLength; i++) {
displayColor(sequence[i]);
playNoteAndDisplay(sequence[i]);
delay(500);
noTone(speakerPin);
delay(500);
}
display.clearDisplay();
display.setCursor(0, 0);
display.println("Your turn!");
display.display();
userTurn = true;
currentStep = 0;
}
void displayColor(int colorIndex) {
display.clearDisplay();
// Draw the color representation as a filled rectangle
int rectWidth = SCREEN_WIDTH / 5;
display.fillRect(colorIndex * rectWidth, 20, rectWidth, SCREEN_HEIGHT - 20, SSD1306_WHITE);
display.display();
delay(500); // Show the color for 500 ms
}
void checkUserInput() {
for (int i = 0; i < 5; i++) {
if (digitalRead(buttonPins[i]) == HIGH) {
// Print the button pressed for debugging
Serial.print("Button pressed: ");
Serial.println(i);
playNoteAndDisplay(i);
delay(200); // Debounce delay
noTone(speakerPin);
if (sequence[currentStep] == i) {
currentStep++;
if (currentStep == sequenceLength) {
sequenceLength++;
userTurn = false;
delay(1000);
}
} else {
display.clearDisplay();
display.setCursor(0, 0);
display.println("Wrong! Game over.");
display.display();
Serial.println("Wrong input! Game over."); // Debugging message
while (true); // Halt the game
}
while (digitalRead(buttonPins[i]) == HIGH); // Wait for button release
}
}
}
void playNoteAndDisplay(int buttonIndex) {
tone(speakerPin, noteFrequencies[buttonIndex]);
display.clearDisplay();
display.setCursor(0, 0);
display.print("Note: ");
display.println(noteFrequencies[buttonIndex]);
display.display();
}