My Simon Code
I added a 5th button to reset the game.
Simon Says Work in Progress… Not working code!
https://github.com/jeannettesubero/PComp-Codes/tree/master/SimonSaysNotWorking
int level = 0; int time = 1500; int ledLight[] = { 4,5,6,7}; int button[] = { 12,9,10,11}; int whatSimonSays[99]; boolean lose = false; boolean simonSaid = false; int mySteps = 0; void setup() { Serial.begin(9600); for(int i = 0; i < 4; i++){ pinMode(ledLight[i],OUTPUT); pinMode(button[i],OUTPUT); } } void loop() { if(level < 11) // if havent reach level 11, the game og on { // Serial.println("step1"); if(!lose) //if havent lost the game, the game go on { // Serial.println("step2"); if(!simonSaid) //if simon doesnt say anything { // Serial.println("step3"); ,simonSays(level); //simon start saying } else //if simon said { userTuren(level); // Serial.println("step4"); } } } else if(level == 11) { Serial.println("YOU WIN!!"); // delay(50000); // level = 0; // time = 1500; } } void simonSays(int level) { // Serial.println("step5"); for(int i = 0; i < level ; i++ ) { whatSimonSays[i] = int(random(1,5)); //in 1-2-3-4 form int nowLight = whatSimonSays[i]; digitalWrite(ledLight[nowLight], HIGH); Serial.print("Simon said "); Serial.println(ledLight[nowLight]); delay(time); digitalWrite(ledLight[nowLight], LOW); } simonSaid = true; } void userTuren(int level) { if( mySteps <= level ) { for(int i = 0; i < level; i++) { for(int j = 0; j < 4 ; j++) { if(digitalRead(button[j]) == 0) { digitalWrite(ledLight[j],HIGH); delay(500); if(j == whatSimonSays[i] ) { mySteps++; } else { Serial.println("GAME OVER!!!"); } digitalWrite(ledLight[j],LOW); } } } } if(mySteps == level) { simonSaid = false; mySteps = 0; level++; time -= 50; } }
This week I worked on getting the Simon code to respond to a user’s input. I started from the example files that Yury provided and modified the code to work with my breadboard set-up and play the “you lose” tone if the canned user input was wrong. I also worked on getting the Arduino to read the button input (HIGH or LOW) as values of user input that could be compared to Simon’s array.
The tactic I tried for evaluating the user input was to store the data as an array and check that array against Simon’s. After doing some research I found a discussion on a Processing forum that provided code for reading and storing bytes of data from an Arduino.
I was able to incorporate this code into my sketch. I could open the serial monitor, input a few numbers, and have the code write those numbers back to me. I then attempted to get button output to behave the same way, but was unsuccessful.
Here’s my code:
//Button Pins
const int redButt = 11; // declare pins for buttons
const int yellButt = 10;
const int blueButt = 9;
const int greenButt = 8;
//LED Pins
const int redLED = 2; // declare pins for LED’s
const int yellLED = 3;
const int blueLED = 4;
const int greenLED = 5;
// Color Code we Use for R,Y,B,G and 1,2,3,4 repectfully
const int redValue = 1; // declare values for colors for buttons
const int yellValue = 2;
const int blueValue = 3;
const int greenValue = 4;
const int speakerPin = 7 ; //declare your speaker pin
boolean simonDone;
int simonSays[99] ={
};
int nextStep = 0 ;
int userStep=0;
int simonSpeed = 300;
void setup()
{
pinMode(redLED, OUTPUT); //Set Pin Mode
pinMode(yellLED, OUTPUT);
pinMode(blueLED, OUTPUT);
pinMode(greenLED, OUTPUT);
pinMode(speakerPin, OUTPUT); // set speaker to output
Serial.begin(9600);
randomSeed(analogRead(0));
}
void loop ()
{
if (simonDone == false)
{
simonSays[nextStep] = random(1,5) ;
// Serial.print(“nextStep: ” );
// Serial.println(nextStep);
// Serial.print(“simonSpeed: ” ); // Show how the new time feature works
// Serial.println(simonSpeed); // Show how the new time feature works
// Serial.print(“simonSays: “);
for (int i =0; i <= nextStep ; i++)
{
Serial.print(simonSays[i]);
Serial.print(” , “);
delay(simonSpeed); // controls speed that simonSays something
playToneAndLight(simonSays[i]);
}
simonSpeed= (simonSpeed – (nextStep*5)); // This seeds up Simon on each turn by forumla (500-(nextStep*5).
// Change the multiplier (5 in this case) to make game faster or slower
// The multiplier is MAJOR factor in how hard the game is.
// This sets Simon’s TOP SPEED. Also important in making game interesting yet winable.
simonSpeed = max(simonSpeed, 200); //this maxes out simon speed at 200 ms
simonDone = true;
//Serial.println(” “);
}
if (simonDone == true)
{
if(digitalRead (redButt)==LOW) {
playToneAndLight (1);
userCheck(1);
}
else if(digitalRead (yellButt)==LOW){
playToneAndLight (2);
userCheck(2);
}
else if(digitalRead (blueButt) ==LOW) {
playToneAndLight (3);
userCheck(3);
}
else if( digitalRead(greenButt) == LOW) {
playToneAndLight(4);
userCheck(4);
}
// nothing happening here yet
// delay(1000);
// Serial.println(“User got it right, add a new step and give Simon a Turn”);
// // user completed successfully, give simon the next turn
// // add one more step to simon’s sequence
}
}
void userCheck (int x) {
Serial.println(“–starting values–“);
Serial.print(“value checking against “);
Serial.print(simonSays[userStep]);
Serial.print(” “);
Serial.print(“value user “);
Serial.println(x);
Serial.println(” user “);
Serial.println(userStep);
Serial.println(” simon “);
Serial.println(nextStep);
Serial.println(“—-“);
if(x==simonSays[userStep]) {
Serial.println(“User right”);
if(userStep == nextStep) {
simonDone = false;
nextStep++;
userStep=0;
}
else {
userStep++;
}
}
else {
tone(7, random(100,3000), 100);
digitalWrite(2,HIGH);
digitalWrite(3,HIGH);
digitalWrite(4,HIGH);
digitalWrite(5,HIGH);
delay(100);
digitalWrite(2,LOW);
digitalWrite(3,LOW);
digitalWrite(4,LOW);
digitalWrite(5,LOW);
delay(100);
tone(7, random(100,3000), 100);
digitalWrite(2,HIGH);
digitalWrite(3,HIGH);
digitalWrite(4,HIGH);
digitalWrite(5,HIGH);
delay(100);
digitalWrite(2,LOW);
digitalWrite(3,LOW);
digitalWrite(4,LOW);
digitalWrite(5,LOW);
delay(100);
tone(7, random(100,3000), 100);
digitalWrite(2,HIGH);
digitalWrite(3,HIGH);
digitalWrite(4,HIGH);
digitalWrite(5,HIGH);
delay(100);
digitalWrite(2,LOW);
digitalWrite(3,LOW);
digitalWrite(4,LOW);
digitalWrite(5,LOW);
delay(100);
nextStep=0;
userStep=0;
simonDone = false;
Serial.println(“Failure”);
}
Serial.println(“–outputnext–“);
Serial.println(userStep);
Serial.println(nextStep);
Serial.println(“—-“);
}
Here is the code I have so far in terms of the game: at the bottom is the tone and light (not sure how to otherwise upload the code)
///Attempt to get simon to listen for user imput
//program Arduino to listen for user imput and compare it to what Simon says.
//The out come should trigger a lose sound, or tell Simon to say something next.
//LOSE SOUND #4*****
// void loop() {
// for (int y=200; y>80; y-=5){
// for (int x=300; x>50; x-=25){
// tone(6, y);
// delay(200); ///play with dealy “ratio”
// noTone(6);
// delay(10);
// tone(6, x);
// delay(300); ///play with dealy “ratio”
// noTone(6);
// delay(10);
// }
// }
// delay(7000);
// }
//buttons
const int switch1 = 2; //green
const int switch2 = 3;//blue
const int switch3 = 4;//red
const int switch4 = 5;//yellow
//leds
const int greenLED = 8;
const int blueLED = 9;
const int redLED = 10;
const int yellowLED = 11;
//values
const int greenVal=1; // declare values for colors for buttons
const int blueVal=2;
const int redVal=3;
const int yellowVal=4;
const int speakerPin = 12;
int counter=0;
Serial.begin(9600);
Serial.println(“Try to Guest the Contents of simonArray, try a number between 1-4!”);
int playerSays;
boolean simonDone;
int simonSays[99]={};// array that holds simon’s sequence, allows for 99 steps
//int playerSays[99]={};
int nextStep = 0; //var that counts how many steps in simon’s current sequence, used to move up in steps
int simonSpeed = 500;
void setup () {
pinMode(greenLED, OUTPUT);
pinMode(blueLED, OUTPUT);
pinMode(redLED, OUTPUT);
pinMode(yellowLED, OUTPUT);
pinMode(speakerPin, OUTPUT);
// pinMode(switch1, INPUT);
// pinMode(switch2, INPUT);
// pinMode(switch3, INPUT);
//
// digitalWrite(switch1, HIGH); //turn on internal pull-up on siwtch1
// digitalWrite(switch2, HIGH);
// digitalWrite(switch3, HIGH);
// Serial.begin(9600);
randomSeed(analogRead(0)); //seed makes more random
}
void loop () {
if (simonDone ==false) { //did simon play sequence yet? bool is false by default
simonSays[nextStep] = random (1,5);
// Serial.println(“simonDone ==false”);
Serial.print(“nextStep: “); ///just to see whats happening
Serial.println(nextStep);
Serial.print(“simonSpeed: ” ); // Show how the new time feature works
Serial.println(simonSpeed); // Show how the new time feature works
Serial.print(“simonSays: “);
for(int i=0; i <=nextStep; i++) {
Serial.print(simonSays[i]); //to see count
Serial.print(“,”);
delay(simonSpeed);//controls speed that simonSays something
playToneAndLight(simonSays[i]);
}
simonSpeed=(simonSpeed-(nextStep*5)); //THis speeds up simon on each turn by formula (500-(nextStep*5)
//Change the multiplier (5 here) to make game faster or slower
//multiplier=MAJOR factor in how hard game is
simonSpeed=max(simonSpeed, 200);//max() assigns simonTime to the larger of simonTime or 200 in this case
//Sets Simon’s top speed. Also important in making game interesting yet winable
delay(500);
simonDone=true; //ok, simon done, set to true
Serial.println(” “);
}
if (simonDone==true) {
// int val1=digitalRead(switch1);
// int val2=digitalRead(switch2);
// int val3=digitalRead(switch3);
// int playerSays[99]={
// val1=1,
// val2=2,
// val3=3,
// };// array that holds player’s sequence, allows for 99 steps
// Serial.println(“playerSAYS”);
// if (simonSays==playerSays) {
// tone(6, 800, 500); ///sound for getting it right
// digitalWrite(greenLED, HIGH); //turn led on as TEST
// delay(500);
// }
//
void playToneAndLight(int color)
{
Serial.print(“inside PlayToneAndLight “);
Serial.print(” color = “);
Serial.println( color );
if(color == 1) // 1 = Red
{
digitalWrite( greenLED,HIGH);
tone(speakerPin,1000);
delay(simonSpeed);
digitalWrite( greenLED,LOW);
noTone(speakerPin);
}
if(color == 2)
{
digitalWrite( blueLED,HIGH);
tone(speakerPin,2000);
delay(simonSpeed);
digitalWrite( blueLED,LOW);
noTone(speakerPin);
}
if(color == 3)
{
digitalWrite( yellowLED,HIGH);
tone(speakerPin,3000);
delay(simonSpeed);
digitalWrite( yellowLED,LOW);
noTone(speakerPin);
}
if(color == 4)
{
digitalWrite( redLED,HIGH);
tone(speakerPin,4000);
delay(simonSpeed);
digitalWrite( redLED,LOW);
noTone(speakerPin);
}
}
// Serial.println(“simonDone ==true”);
delay(500);
Serial.println(“User got it right, add a new step and give Simon a Turn”);
simonDone=false; //user completeed successfully, give simon next turn
nextStep++; //add another step to simons sequence
}
}
const int ledPinWhite = 8; //white light const int ledPinRed = 9; //red light const int ledPinYellow = 10; //yellow light const int ledPinGreen = 11; //green light const int switchWhite = 3; //white switch const int switchRed = 4; //red switch const int switchYellow = 5; //yellow switch const int switchGreen = 6; //green switch int switchArray[] = {3, 4, 5, 6}; int switchCount = 4; int simonSays[99] = {}; int nextMove = 0; void setup() { Serial.begin(9600); pinMode(switchWhite, INPUT); //white switch is now an input digitalWrite (switchWhite, HIGH); //turns on a resistor inside the pin so it isnt always on pinMode(switchRed, INPUT); //red switch is now an input digitalWrite (switchRed, HIGH); //turns on the resistor pinMode(switchYellow, INPUT); //yellow switch is an input digitalWrite(switchYellow, HIGH); //turns on the resistor pinMode(switchGreen, INPUT); //green switch is an input digitalWrite(switchGreen, HIGH); //turns on the resistor int aSwitch; for (int aSwitch = 0; aSwitch < switchCount; aSwitch++) { if(aSwitch == 0) { digitalWrite(ledPinWhite, OUTPUT); } if(aSwitch == 1) { digitalWrite(ledPinRed, OUTPUT); } if (aSwitch == 2) { digitalWrite(ledPinYellow, OUTPUT); } if (aSwitch == 3) { digitalWrite(ledPinGreen, OUTPUT); } } pinMode(ledPinWhite, OUTPUT); //White LED is an output pinMode(ledPinRed, OUTPUT); //Red LED is an output pinMode(ledPinYellow, OUTPUT); //Yellow LED is an output pinMode(ledPinGreen, OUTPUT); //Green LED is an output } void loop() { simonSays[nextMove] = random(1,5); Serial.print("nextMove: "); Serial.println(nextMove); for (int i = 0; i <= nextMove; i++) { Serial.print("simonSays"); Serial.print(simonSays[i]); delay(20000); } for (int aSwitch = 0; aSwitch < switchCount; aSwitch++) { if(aSwitch == 0) { digitalWrite(ledPinWhite, HIGH); digitalWrite(ledPinRed, LOW); digitalWrite(ledPinYellow, LOW); digitalWrite(ledPinGreen, LOW); } if(aSwitch == 1) { digitalWrite(ledPinRed, HIGH); digitalWrite(ledPinYellow, LOW); digitalWrite(ledPinGreen, LOW); digitalWrite(ledPinWhite, LOW); } if (aSwitch == 2) { digitalWrite(ledPinYellow, HIGH); digitalWrite(ledPinGreen, LOW); digitalWrite(ledPinWhite, LOW); digitalWrite(ledPinRed, LOW); } if (aSwitch == 3) { digitalWrite(ledPinGreen, HIGH); digitalWrite(ledPinWhite, LOW); digitalWrite(ledPinRed, LOW); digitalWrite(ledPinYellow, LOW); } } if (simonSays[1] == switchArray[0]) { Serial.print("Correct!"); } else { Serial.print("Incorrect!"); } if (simonSays[2] == switchArray[1]) { Serial.print("Correct!"); } else { Serial.print("Incorrect!"); } if (simonSays[3] == digitalRead(aSwitch = ) { Serial.print("Correct!"); } else { Serial.print("Incorrect!"); } if (simonSays[4] == switchArray[3]) { Serial.print("Correct!"); } else { Serial.print("Incorrect!"); } } /* Serial.print(digitalRead(switchBlack)); int val1 = digitalRead(switchBlack); if (val1 == 0) { digitalWrite(ledPinRed, HIGH); //turn LED on } else { digitalWrite(ledPinRed, LOW); //turn LED off } Serial.print(digitalRead(switchGreen)); int val2 = digitalRead(switchGreen); if (val2 == 0) { digitalWrite(ledPinBlue, HIGH); //turn LED on } else { digitalWrite(ledPinBlue, LOW); //turn LED off } } */
this is YiNing’s not working Simon code….
boolean simonDone; int simonSays[99] ={}; int user[99] = {}; int nextStep = 0 ; int usernextStep = 1; int simonSpeed = 500; int ledpin[4] = {}; int btnpin[4] = {}; int count = 0; int debouncedelay = 10; boolean debouncefunction(int pin){ boolean state; boolean previousState; previousState = digitalRead(pin); for(int counter =0; counter > debouncedelay; counter++){ delay(1); state = digitalRead(pin); //readpin if(state != previousState) {counter = 0; previousState = state; } } return state; } void setup() { for(int i = 4; i < 8; i++){ pinMode(btnpin[i], INPUT); } for(int i =8; i < 12; i++){ pinMode(ledpin[i], OUTPUT); } Serial.begin(9600); randomSeed(analogRead(0)); } void loop () { if (simonDone == false) { simonSays[nextStep] = int(random(1,5)) ; Serial.print("nextStep: " ); Serial.println(nextStep); Serial.print("simonSpeed: " ); // Show how the new time feature works Serial.println(simonSpeed); // Show how the new time feature works Serial.print("simonSays: "); for (int i =0; i <= nextStep ; i++) { Serial.print(simonSays[i]); Serial.print(" , "); if(simonSays[i] == 1){ digitalWrite(8, HIGH); delay(300); digitalWrite(8, LOW); } if(simonSays[i] == 2){ digitalWrite(9, HIGH); delay(300); digitalWrite(9, LOW); } if(simonSays[i] == 3){ digitalWrite(10, HIGH); delay(300); digitalWrite(10, LOW); } if(simonSays[i] == 4){ digitalWrite(11, HIGH); delay(300); digitalWrite(11, LOW); } delay(simonSpeed); } simonSpeed= (simonSpeed - (nextStep*5)); simonSpeed = max(simonSpeed, 200); simonDone = true; Serial.println(" "); } if (simonDone == true) { //delay(3000); Serial.println("User's turn"); Serial.print("User says: "); for(int i = 0; i <= nextStep; i++){ boolean wait = true; if(user[i] != 1 || user[i] != 2 || user[i] != 3 || user[i] != 4){ if(wait == true) { for(int i = 0; i <count+1; i++){ delay(count); count++; } } } if (digitalRead(4) == HIGH){ wait = false; Serial.print("1"); user[i] = 1; delay(200); } if (digitalRead(5) == HIGH){ wait = false; Serial.print("2"); user[i] = 2; delay(200); } if (digitalRead(6) == HIGH){ wait = false; Serial.print("3"); user[i] = 3; delay(200); } if (digitalRead(7)== HIGH){ wait = false; Serial.print("4"); user[i] = 4; delay(200); } if (user[i] == simonSays[i]){ //usernextStep ++; Serial.print(" , "); //if(usernextStep == nextStep + 1){ Serial.println("User got it right, add a new step and give Simon a Turn"); simonDone = false; //} } } nextStep++; } }
Reply