Created
January 25, 2019 13:16
-
-
Save vineeth-b/a72aa077181b39faba07ebb8390f9879 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*Simon Says game with LCD Display. | |
By Vineeth Balachnadran | |
*/ | |
#include <Tone.h> | |
#include <LiquidCrystal_I2C.h> | |
#include<Wire.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[] = {2, 3, 4, 5}; //The four button input pins | |
int ledpin[] = {8, 9, 10, 11}; // LED pins | |
int turn = 0; // turn counter | |
int buttonstate; // button state checker` | |
int buttonstate1; | |
int randomArray[100]; //Intentionally long to store up to 100 inputs (doubtful anyone will get this far) | |
int inputArray[100]; | |
int positionCounter; | |
int score = 0; | |
int tim = 500; //the value of delay time | |
char array1[] = "Simon Says!"; | |
char array2[] = "Want to Play?"; | |
char array3[] = "Lets Play"; | |
char array4[] = "You Lose"; | |
const int button1 = 13; | |
const int button2 = 0; | |
LiquidCrystal_I2C lcd(0x27, 16, 2); | |
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | |
void setup() | |
{ | |
pinMode(button1, INPUT); | |
pinMode(button2, INPUT); | |
lcd.init(); | |
lcd.backlight(); | |
Serial.begin(9600); | |
lcd.setCursor(15, 0); // set the cursor to column 15, line 0 | |
for (int positionCounter = 0; positionCounter < 11; positionCounter++) | |
{ | |
lcd.scrollDisplayLeft(); //Scrolls the contents of the display one space to the left. | |
lcd.print(array1[positionCounter]); // Print a message to the LCD. | |
delay(tim); //wait for 250 microseconds | |
} | |
lcd.clear(); //Clears the LCD screen and positions the cursor in the upper-left corner. | |
lcd.setCursor(15, 0); // set the cursor to column 15, line 1 | |
for (int positionCounter = 0; positionCounter < 13; positionCounter++) | |
{ | |
lcd.scrollDisplayLeft(); //Scrolls the contents of the display one space to the left. | |
lcd.print(array2[positionCounter]); // Print a message to the LCD. | |
delay(tim); //wait for 250 microseconds | |
} | |
} | |
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | |
void loop() | |
{ | |
buttonstate = digitalRead(button1); | |
buttonstate1 = digitalRead(button2); | |
Serial.println(buttonstate); | |
Serial.println(buttonstate1); | |
//When the user clicks the YES button, the game will begin. | |
if (buttonstate == LOW) { | |
lcd.clear(); | |
lcd.setCursor(15, 0); // set the cursor to column 15, line 0 | |
for (int positionCounter = 0; positionCounter < 9; positionCounter++) | |
{ | |
lcd.scrollDisplayLeft(); //Scrolls the contents of the display one space to the left.- | |
lcd.print(array3[positionCounter]); // Print a message to the LCD | |
delay(tim); //wait for 250 microseconds | |
} | |
lcd.clear(); //Clears the LCD screen and positions the cursor in the upper-left corner. | |
speakerpin.begin(12); // speaker is on pin 12 | |
//This for loop will register all the LEDS as outputs | |
for (int x = 0; x < 4; x++) // LED pins are outputs | |
{ | |
pinMode(ledpin[x], OUTPUT); | |
} | |
//This for loop will register buttons that control LEDs as inputs and to make them do digitalWrite | |
for (int x = 0; x < 4; x++) | |
{ | |
pinMode(button[x], INPUT); // button pins are inputs | |
digitalWrite(button[x], HIGH); // enable internal pullup; buttons start in high position; logic reversed | |
} | |
randomSeed(analogRead(0)); //Added to generate "more randomness" with the randomArray for the output function | |
//This for loop will being to play the intro song of the program | |
for (int thisNote = 0; thisNote < 13; thisNote ++) { | |
// play the next note: | |
speakerpin.play(starttune[thisNote]); | |
// hold the note: | |
if (thisNote == 0 || thisNote == 2 || thisNote == 4 || thisNote == 6) | |
{ | |
digitalWrite(ledpin[0], HIGH); | |
} | |
if (thisNote == 1 || thisNote == 3 || thisNote == 5 || thisNote == 7 || thisNote == 9 || thisNote == 11) | |
{ | |
digitalWrite(ledpin[1], HIGH); | |
} | |
if (thisNote == 8 || thisNote == 12) | |
{ | |
digitalWrite(ledpin[2], HIGH); | |
} | |
if (thisNote == 10) | |
{ | |
digitalWrite(ledpin[3], HIGH); | |
} | |
delay(duration2[thisNote]); | |
// stop for the next note: | |
speakerpin.stop(); | |
digitalWrite(ledpin[0], LOW); | |
digitalWrite(ledpin[1], LOW); | |
digitalWrite(ledpin[2], LOW); | |
digitalWrite(ledpin[3], LOW); | |
delay(25); | |
} | |
delay(1000); | |
//The actual game will being. Random LEDS will begin to genetate and be outputted to the user. | |
for (int y = 0; y <= 99; y++) | |
{ | |
//function for generating the array to be matched by the player | |
digitalWrite(ledpin[0], HIGH); | |
digitalWrite(ledpin[1], HIGH); | |
digitalWrite(ledpin[2], HIGH); | |
digitalWrite(ledpin[3], 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); | |
} | |
digitalWrite(ledpin[0], LOW); | |
digitalWrite(ledpin[1], LOW); | |
digitalWrite(ledpin[2], LOW); | |
digitalWrite(ledpin[3], LOW); | |
delay(1000); | |
for (int y = turn; y <= turn; y++) | |
{ //Limited by the turn variable | |
Serial.println(""); //Some serial output to follow along | |
lcd.print("Score: "); | |
lcd.print(score); | |
score++; | |
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]); | |
//This for loop will output the randomly generated LEDs to the user | |
for (int y = 0; y < 4; y++) | |
{ | |
if (randomArray[x] == 1 && ledpin[y] == 8) | |
{ //if statements to display the stored values in the array | |
digitalWrite(ledpin[y], HIGH); | |
speakerpin.play(NOTE_G3, 100); | |
delay(400); | |
digitalWrite(ledpin[y], LOW); | |
delay(100); | |
} | |
if (randomArray[x] == 2 && ledpin[y] == 9) | |
{ | |
digitalWrite(ledpin[y], HIGH); | |
speakerpin.play(NOTE_A3, 100); | |
delay(400); | |
digitalWrite(ledpin[y], LOW); | |
delay(100); | |
} | |
if (randomArray[x] == 3 && ledpin[y] == 10) | |
{ | |
digitalWrite(ledpin[y], HIGH); | |
speakerpin.play(NOTE_B3, 100); | |
delay(400); | |
digitalWrite(ledpin[y], LOW); | |
delay(100); | |
} | |
if (randomArray[x] == 4 && ledpin[y] == 11) | |
{ | |
digitalWrite(ledpin[y], HIGH); | |
speakerpin.play(NOTE_C4, 100); | |
delay(400); | |
digitalWrite(ledpin[y], LOW); | |
delay(100); | |
} | |
} | |
} | |
} | |
//Will go into the input function and check and see if user input mathced program generated LED | |
input(); | |
} | |
} | |
//If the user clicks the NO botton, the following message wil be outputted | |
else if (buttonstate1 == LOW){ | |
lcd.clear(); | |
lcd.print("See U Later!"); | |
} | |
} | |
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | |
void input() { //Function for allowing user input and checking input against the generated array | |
for (int x = 0; x <= turn;) | |
{ //Statement controlled by turn count | |
for (int y = 0; y < 4; y++) | |
{ | |
buttonstate = digitalRead(button[y]); | |
if (buttonstate == LOW && button[y] == 2) | |
{ //Checking for button push | |
digitalWrite(ledpin[0], HIGH); | |
speakerpin.play(NOTE_G3, 100); | |
delay(200); | |
digitalWrite(ledpin[0], LOW); | |
inputArray[x] = 1; | |
delay(250); | |
Serial.print(" "); | |
Serial.print(1); | |
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 | |
x++; | |
} | |
//These if statements will first check and see which button the user clicked. Based on that, the program will | |
//check and determine if the user chose the correct button | |
if (buttonstate == LOW && button[y] == 3) | |
{ | |
digitalWrite(ledpin[1], HIGH); | |
speakerpin.play(NOTE_A3, 100); | |
delay(200); | |
digitalWrite(ledpin[1], LOW); | |
inputArray[x] = 2; | |
delay(250); | |
Serial.print(" "); | |
Serial.print(2); | |
if (inputArray[x] != randomArray[x]) { | |
fail(); | |
} | |
x++; | |
} | |
if (buttonstate == LOW && button[y] == 4) | |
{ | |
digitalWrite(ledpin[2], HIGH); | |
speakerpin.play(NOTE_B3, 100); | |
delay(200); | |
digitalWrite(ledpin[2], LOW); | |
inputArray[x] = 3; | |
delay(250); | |
Serial.print(" "); | |
Serial.print(3); | |
if (inputArray[x] != randomArray[x]) { | |
fail(); | |
} | |
x++; | |
} | |
if (buttonstate == LOW && button[y] == 5) | |
{ | |
digitalWrite(ledpin[3], HIGH); | |
speakerpin.play(NOTE_C4, 100); | |
delay(200); | |
digitalWrite(ledpin[3], LOW); | |
inputArray[x] = 4; | |
delay(250); | |
Serial.print(" "); | |
Serial.print(4); | |
if (inputArray[x] != randomArray[x]) | |
{ | |
digitalWrite(ledpin[0], LOW); | |
digitalWrite(ledpin[1], LOW); | |
digitalWrite(ledpin[2], LOW); | |
digitalWrite(ledpin[3], LOW); | |
fail(); | |
} | |
x++; | |
} | |
} | |
} | |
lcd.clear(); | |
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); | |
} | |
//Reset score to 0 | |
score = 0; | |
lcd.clear(); | |
lcd.print("You Lost"); | |
delay(2000); | |
turn = -1; //Resets turn value so the game starts over without need for a reset button | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment