Simon Says Game with LCD screen - problem

Hi everyone!

I’m pretty new to Arduino and programming in general. I’ve followed some youtube videos and made my own Simon Says Game enclosure and used the code by Robert Spann and digimike.

I’ve wanted to add an LCD screen, but the game code is troubling me.

I just want to have the information on the serial monitor - printed to my LCD screen.

Any help would be much appreciated :astonished:

This is my code so far:

/*Simon Says game. Now with sound effects.

Originaly made by Robert Spann
Code trimmed and sound effects added by digimike

Buttons are to be set on there designated pins without pull down resistors
and connected to ground rather then +5.
*/
#include <Tone.h>
#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(22, 23, 32, 34, 36, 38);
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};
boolean button[] = {2, 3, 4, 5}; //The four button input pins
boolean ledpin[] = {8, 9, 10, 11};  // 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 setup()
{
  Serial.begin(9600);
  speakerpin.begin(12); // speaker is on pin 13

  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);  // 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
  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);
    pinMode(12, OUTPUT);  
  analogWrite(12, 100);   
  // set up the LCD's number of columns and rows: 
  lcd.begin(16, 2);
  lcd.setCursor(1, 0);
  // Print a message to the LCD.
  lcd.print("Simon Says Game");

}

void loop()
{  
  lcd.setCursor(0, 1);
  lcd.print(turn);
 
  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
      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] == 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);
          }
        }
      }
    }
    input();
  }
}



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++;
      }
       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])
        {
          fail();
        }
        x++;
      }
    }
  }
  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);
  turn = -1; //Resets turn value so the game starts over without need for a reset button
 
}

What problem do you have ?

The information on the LCD screen doesnt change.

It says Turn (as it should) and then the round counter stays on 0 - even though the serial monitor looks like this

Turn: 0

Turn: 1

Turn: 2

Hope that makes sense.

I don't see you printing the word turn, i do see you printing the value (turn). (Maybe i didn't look close enough). You are serial sending the text "Turn: ", which is followed by the value (y). I do not see you sending this value (y) to the display. If you want to see the same data, you should send the same data.

I expected you to print to display and send serial at (almost) the same time, but don't see that happening.

The value (turn) is increased outside of void.loop, are you sure it is indeed increasing ?

Hi again, and thanks for your help!

"I don't see you printing the word turn, i do see you printing the value (turn). (Maybe i didn't look close enough). You are serial sending the text "Turn: ", which is followed by the value (y). I do not see you sending this value (y) to the display. If you want to see the same data, you should send the same data.

I expected you to print to display and send serial at (almost) the same time, but don't see that happening.

The value (turn) is increased outside of void.loop, are you sure it is indeed increasing ?"

Hmmm - indeed i want to print the value of (turn), and the display does that, it starts with zero but it doesnt keep on counting like in the serial monitor, where it says Turn 1, Turn 2 etc.

All i want on the display is: "Turn" followed by the value of the turn counter. :fearful:

So why don’t you try this then:

 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("");
      lcd.setCursor(6, 1); //Some LCD message as well
      lcd.print(y);
      randomArray[y] = random(1, 5); //Assigning a random number (1-4) to the randomArray[y], y being the turn count

This would update the value that is printed to the LCD (not the text "Turn: " as that doesn’t need to be updated), but it uses the same value as you’re using in the serial monitor, which is (y) and not (turn).
There’s a lot going on with that (y) value so i don’t know for sure that’s the value you want but won’t hurt to try i guess.
Once you go over 10 or 100 turns, you’ll need to erase the value if you fail and reset.
Can’t be too hard to do that.

It works like a charm :) :)

Thanks ALOT for your help! Really appreciate it :)

MAS3:
So why don’t you try this then:

 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(””);
     lcd.setCursor(6, 1); //Some LCD message as well
     lcd.print(y);
     randomArray[y] = random(1, 5); //Assigning a random number (1-4) to the randomArray[y], y being the turn count




This would update the value that is printed to the LCD (not the text "Turn: " as that doesn't need to be updated), but it uses the same value as you're using in the serial monitor, which is (y) and not (turn).
There's a lot going on with that (y) value so i don't know for sure that's the value you want but won't hurt to try i guess.
Once you go over 10 or 100 turns, you'll need to erase the value if you fail and reset.
Can't be too hard to do that.

Hi again - just wanted to add, that because of your help - around 30 kids a day are using my Simon Says game at the place where i work. I’ve uploaded a clip of it on Youtube

Again, thanks

Always good to see a finished product and hear it's well used.

I hope you understood what went wrong so next time you'll recognize it and are able to solve it without help.

One more tip for you next videoclip: dim the LCD backlight a lot. The camera has troubles focusing because of the brightness of the backlight, that's why the text is so hard to read in your clip.