#include <Tone.h>
Tone speakerpin;
int starttune[] = {NOTE_C4, NOTE_F4, NOTE_C4, NOTE_F4, NOTE_C4, NOTE_F4, NOTE_C4, NOTE_F4, NOTE_G4, NOTE_F4, NOTE_E4, NOTE_F4, NOTE_G4};
int duration2[] = {100, 200, 100, 200, 100, 400, 100, 100, 100, 100, 200, 100, 500};
int note[] = {NOTE_C4, NOTE_C4, NOTE_G4, NOTE_C5, NOTE_G4, NOTE_C5};
int duration[] = {100, 100, 100, 300, 100, 300};
int button[] = {8, 9, 10, 11}; // The four button input pins
int ledpin[] = {2, 3, 4, 5}; // LED pins
int turn = 0; // turn counter
int buttonstate = 0; // button state checker
int randomArray[100]; // Intentionally long to store up to 100 inputs (doubtful anyone will get this far)
int inputArray[100];
void startup(){
randomSeed(analogRead(0));
for (int thisNote = 0; thisNote < 13; thisNote++) {
// Play the next note:
speakerpin.play(starttune[thisNote]);
// Hold the note:
for (int i = 0; i < 4; i++) {
if (thisNote == i || thisNote == i + 8) {
digitalWrite(ledpin[i], HIGH);
}
}
delay(duration2[thisNote]);
// Stop for the next note:
speakerpin.stop();
for (int i = 0; i < 4; i++) {
digitalWrite(ledpin[i], LOW);
}
delay(25);
}
}
void setup() {
Serial.begin(9600);
speakerpin.begin(12); // speaker is on pin 12
for (int x = 0; x < 4; x++) // LED pins are outputs
{
pinMode(ledpin[x], OUTPUT);
}
for (int x = 0; x < 4; x++)
{
pinMode(button[x], INPUT_PULLUP); // button pins are inputs with internal pull-up resistors enabled
}
// Added to generate "more randomness" with the randomArray for the output function
// for (int thisNote = 0; thisNote < 13; thisNote++) {
// // Play the next note:
// speakerpin.play(starttune[thisNote]);
// // Hold the note:
// for (int i = 0; i < 4; i++) {
// if (thisNote == i || thisNote == i + 8) {
// digitalWrite(ledpin[i], HIGH);
// }
// }
// delay(duration2[thisNote]);
// // Stop for the next note:
// speakerpin.stop();
// for (int i = 0; i < 4; i++) {
// digitalWrite(ledpin[i], LOW);
// }
// delay(25);
// }
startup();
delay(1000);
}
void loop()
{
delay(500);
for (int y = 0; y <= 99; y++)
{
// Function for generating the array to be matched by the player
for (int i = 0; i < 4; i++) {
digitalWrite(ledpin[i], HIGH);
}
for (int thisNote = 0; thisNote < 6; thisNote++) {
// Play the next note:
speakerpin.play(note[thisNote]);
// Hold the note:
delay(duration[thisNote]);
// Stop for the next note:
speakerpin.stop();
delay(25);
}
for (int i = 0; i < 4; i++) {
digitalWrite(ledpin[i], LOW);
}
delay(1000);
for (int y = turn; y <= turn; y++)
{ // Limited by the turn variable
Serial.println(""); // Some serial output to follow along
Serial.print("Turn: ");
Serial.print(y);
Serial.println("");
randomArray[y] = random(1, 5); // Assigning a random number (1-4) to the randomArray[y], y being the turn count
for (int x = 0; x <= turn; x++)
{
Serial.print(randomArray[x]);
for (int y = 0; y < 4; y++)
{
if (randomArray[x] == y + 1)
{ // if statements to display the stored values in the array
digitalWrite(ledpin[y], HIGH);
switch (note[y]) {
case NOTE_G4:
speakerpin.play(NOTE_G3, 100);
break;
case NOTE_A4:
speakerpin.play(NOTE_A3, 100);
break;
case NOTE_B4:
speakerpin.play(NOTE_B3, 100);
break;
case NOTE_C5:
speakerpin.play(NOTE_C4, 100);
break;
}
delay(500);
digitalWrite(ledpin[y], LOW);
delay(250);
}
}
}
}
input(); // Move input handling here to wait for player input after each sequence
}
}
void input()
{
for (int x = 0; x <= turn; x++)
{
int correctButton = randomArray[x] - 1; // Adjust for zero-based index
int pressedButton = -1;
while (pressedButton != correctButton) {
for (int y = 0; y < 4; y++)
{
buttonstate = digitalRead(button[y]);
if (buttonstate == LOW)
{
pressedButton = y;
digitalWrite(ledpin[y], HIGH);
speakerpin.play(note[y], 100);
delay(500);
digitalWrite(ledpin[y], LOW);
delay(500);
Serial.print(" ");
Serial.print(y + 1);
if (pressedButton != correctButton) {
fail();
startup();
}
break;
}
}
}
}
delay(500);
turn++;
}
// void input()
// { // Function for allowing user input and checking input against the generated array
// for (int x = 0; x <= turn; x++)
// { // Statement controlled by turn count
// for (int y = 0; y < 4; y++)
// {
// buttonstate = digitalRead(button[y]);
// if (buttonstate == HIGH)
// { // Checking for button release
// continue; // Skip to the next iteration if button is not pressed
// }
// // Checking for button push
// digitalWrite(ledpin[y], HIGH);
// switch (button[y]) {
// case 8:
// speakerpin.play(NOTE_G3, 100);
// break;
// case 9:
// speakerpin.play(NOTE_A3, 100);
// break;
// case 10:
// speakerpin.play(NOTE_B3, 100);
// break;
// case 11:
// speakerpin.play(NOTE_C4, 100);
// break;
// }
// delay(200);
// digitalWrite(ledpin[y], LOW);
// inputArray[x] = y + 1;
// delay(250);
// Serial.print(" ");
// Serial.print(inputArray[x]);
// if (inputArray[x] != randomArray[x])
// { // Checks value input by user and checks it against
// fail(); // the value in the same spot on the generated array
// } // The fail function is called if it does not match
// }
// }
// delay(500);
// turn++; // Increments the turn count, also the last action before starting the output function over again
// }
void fail()
{ // Function used if the player fails to match the sequence
for (int y = 0; y <= 2; y++)
{ // Flashes lights for failure
digitalWrite(ledpin[0], HIGH);
digitalWrite(ledpin[1], HIGH);
digitalWrite(ledpin[2], HIGH);
digitalWrite(ledpin[3], HIGH);
speakerpin.play(NOTE_G3, 300);
delay(200);
digitalWrite(ledpin[0], LOW);
digitalWrite(ledpin[1], LOW);
digitalWrite(ledpin[2], LOW);
digitalWrite(ledpin[3], LOW);
speakerpin.play(NOTE_C3, 300);
delay(200);
}
delay(500);
// Reset game
turn = 0;
for (int i = 0; i < 100; i++) {
randomArray[i] = 0;
inputArray[i] = 0;
}
}