I am trying to make a Simon Says game with the ARDUINO UNO

I started on what I thought was a fun and easy little project for my skill level in ARDUINO and it was to make a Simon says game with 4 LEDs 4 push buttons and a buzzer.

I am using the ARDUINO UNO.

so for starters I used only two LEDs and two pushbuttons just to get the concept and I am stock and I need help with the code.

the problem is that I can't find a way to use the pushbuttons to repeat the sequence that is shown with the LEDs like int a Simon Says game.
here is the code so you can look at what I have done I tried my best explaining the code lines hope you can understand and find help me find the solution.

// this code is for simon says with two LEDs(I will add two more when i can do the first two)
// i can show the circit that i built in tinkercad
int ledPinB = 7, ledPinR = 6; //the power pins of the LEDs
int buttonPin1 = 9, buttonPin2 = 8; //the pushbutton pins
int buttonVal1, buttonVal2; //the pushbuttton value
int blueVal = 0, redVal = 0; //the LEDs counter
int lose = 0; //the buzzer activator not important righ now
int show = 1; //the number of time the LEDs light up
int numShow = 0; //the counter for the number of time the LEDs light up
long randNumber;//a random number variabel
/*  button 1 = BlueLED
    button 2 = redLED
*/
void setup()
{
  Serial.begin(115200);
  randomSeed(analogRead(0));//the random number genarator
  digitalWrite(buttonPin1, HIGH); //for the pull up resistors
  digitalWrite(buttonPin2, HIGH);
}

void loop()
{
  while (show > numShow)/*this while loop is responsible for blinking the LEDs a spesific amount of
  times and counting how much every LED blinked and how much times the loop looped basicly its for generating random sequences*/
  {
    randNumber = random(2);
    Serial.print("the random number: ");
    Serial.println(randNumber);
    if (randNumber == 0)
    {
      digitalWrite(ledPinR, HIGH);
      digitalWrite(ledPinB, LOW);
      delay(500);
      digitalWrite(ledPinR, LOW);
      digitalWrite(ledPinB, LOW);
      delay(500);
      redVal++;
    }
    else if (randNumber == 1)
    {
      digitalWrite(ledPinR, LOW);
      digitalWrite(ledPinB, HIGH);
      delay(500);
      digitalWrite(ledPinR, LOW);
      digitalWrite(ledPinB, LOW);
      delay(500);
      blueVal++;
    }
    numShow++;
  }
  Serial.println(redVal);//those are prints for trouble shooting
  Serial.println(blueVal);
  Serial.println(numShow);
  while (numShow >= 0)/* this while loop is suppose to read the values from the push buttons
  and if you were able to repeat the sequence correctly or you failed it 
should have values in the variables to show you that*/
    // P.S this the part i think i have the issue
  {
    while (digitalRead(buttonPin1) == HIGH && digitalRead(buttonPin2) == HIGH) {}
    buttonVal1 = digitalRead(buttonPin1);
    buttonVal2 = digitalRead(buttonPin2);
    if (buttonVal1 == 0 && blueVal >= 0)
    {
      blueVal--;
      Serial.print("first button: ");
      Serial.println(buttonVal1);
    }
    else if (buttonVal2 == 0 && redVal >= 0)
    {
      redVal--;
      Serial.print(" Second button: ");
      Serial.println(buttonVal2);
    }
    numShow--;
  }
  Serial.println(redVal);//again some trouble shoot
  Serial.println(blueVal);
  // and this part is just to tell you if you did the sequence right or wrong
  if (redVal != 0 || blueVal != 0)
  {
    Serial.println("LOSER");
    lose = 1;
  }
  else
  {
    Serial.println("Winner");
    show++;
  }
  delay(2000);// this is a delay so you can have time to breath between sequences
}

P.S

i know there is a code for Simon says for ARDUINO UNO but i just wanted to make my own one.

What is wrong? What happens that shouldn’t happen? What shouldn’t happen that does happen?

shouldn’t your simon says game do these things

  • wait for a button press to start displaying a sequence
  • determine a sequence of some length (stored in an array)
  • blink an LED corresponding to each each value in the sequence
  • check for corresponding button presses for a limited amount of time
  • recognize incorrect button press and end/restart the game
  • increment the length of the sequence after each correctly entered sequence and restart

you shouldn’t wait forever for a button press, only a limited amount of time for any button press (use millis())

your code is also not checking for a sequence, but simply that the corresponding button was pressed the same number of times that the LED was blinked.

in your code, you also need to make sure that blue and redVal are both not zero

consider

// this code is for simon says with two LEDs (I will add two more when i can do the first two)
// i can show the circit that i built in tinkercad

#if 0
int ledPinB = 7, ledPinR = 6; //the power pins of the LEDs
int buttonPin1 = 9, buttonPin2 = 8; //the pushbutton pins

#else       // my hardware
int buttonPin1 = A1;
int buttonPin2 = A2;
int ledPinB    = 13;
int ledPinR    = 12;
#endif

enum { On = LOW, Off = HIGH };

int buttonVal1 = Off;
int buttonVal2 = Off;

int blueVal = 0, redVal = 0; //the LEDs counter

int lose = 0;       //the buzzer activator not important righ now
int show = 1;       //the number of time the LEDs light up
int numShow = 0;    //the counter for the number of time the LEDs light up
long randNumber;    //a random number variabel

/*  button 1 = BlueLED
button 2 = redLED
*/

char s [80];

// -----------------------------------------------------------------------------
void setup ()
{
    Serial.begin (115200);

    pinMode (buttonPin1, INPUT_PULLUP);
    pinMode (buttonPin2, INPUT_PULLUP);
    pinMode (ledPinB,    OUTPUT);
    pinMode (ledPinR,    OUTPUT);

    randomSeed (analogRead (0));//the random number genarator

    digitalWrite (buttonPin1, Off);
    digitalWrite (buttonPin2, Off);

    Serial.println ("press button to start");
}

// -----------------------------------------------------------------------------
int
isButPressed (
    int   butPin,
    int & butState )
{
    byte but = digitalRead (butPin);
    if (butState != but)  {
        butState = but;

        sprintf (s, "isButPress: pin %d, state %d, result %d",
            butPin, butState, On == but);
        Serial.println (s);

        return (On == but);
    }
    return 0;
}

// -----------------------------------------------------------------------------
void loop ()
{
    if (! isButPressed (buttonPin1, buttonVal1))
        return;

    // this while loop is responsible for blinking the LEDs a spesific amount
    // of times and counting how much every LED blinked and how much times
    // the loop looped basicly its for generating random sequences

    redVal = blueVal = 0;

    while (show > numShow)
    {
        randNumber = random (2);
        Serial.print ("the random number: ");
        Serial.println (randNumber);

        if (randNumber == 0)
        {
            digitalWrite (ledPinR, On);
            digitalWrite (ledPinB, Off);
            redVal++;
        }

        else
        {
            digitalWrite (ledPinR, Off);
            digitalWrite (ledPinB, On);
            blueVal++;
        }

        delay (500);
        digitalWrite (ledPinR, Off);
        digitalWrite (ledPinB, Off);
        delay (500);

        numShow++;
    }

    Serial.println (redVal);//those are prints for trouble shooting
    Serial.println (blueVal);
    Serial.println (numShow);

    /* this while loop is suppose to read the values from the push buttons
    and if you were able to repeat the sequence correctly or you failed it
    should have values in the variables to show you that*/
    // P.S this the part i think i have the issue

    while (numShow)
    {
        if (isButPressed (buttonPin1, buttonVal1))  {
            sprintf (s, " but1 press, blueVal %d", blueVal);
            Serial.println (s);

            if (0 > --blueVal)
                break;
            numShow--;
        }

        if (isButPressed (buttonPin2, buttonVal2))  {
            sprintf (s, " but2 press, blueVal %d", redVal);
            if (0 > --redVal)
                break;
            numShow--;
        }
    }

    Serial.println (redVal);//again some trouble shoot
    Serial.println (blueVal);
    // and this part is just to tell you if you did the sequence right or wrong
    if (redVal != 0 || blueVal != 0)
    {
        Serial.println ("LOSER");
        lose = 1;
    }

    else
    {
        Serial.println ("Winner");
        show++;
    }

    Serial.println ("press button to start");
}

Note: Keeping track of how many times the Red LED was flashed is not the same as keeping track of the order in which the LEDs were flashed. There are two ways to record the pattern:

  • Keep an array of colors in the order they were flashed.
  • Reset the randomSeed() because the same seed will produce the same pattern.

The advantage of #2 is that it is not limited by available memory and the game can go on forever.

  • For each game, chose a randomSeed() and set the game length to 1.

  • Play the pseudo-random pattern on the LEDs and buzzer.

  • Reset the randomSeed() to get the same pattern again.

  • For the length of the game:

  • if button press doesn't arrive before timeout: Game Over, LOST

  • if button does not match the current random number: Game Over, LOST

  • Increment the game length and repeat from 2