Simon says game issues

i am trying to make a simon says game but my code doesnt work fully any help is appreciated

const int redButton = 7;
const int blueButton = 8;
const int greenButton = 9;
const int yellowButton = 10;
const int redLed = 6;
const int blueLed = 5;
const int greenLed = 4;
const int yellowLed = 3;
const int maxCount = 32;


int redRead = 0;
int blueRead = 0;
int greenRead = 0;
int yellowRead = 0;


int dataStorage[maxCount];
byte nextFreeIndex = 0;
int picker = 0;
int counter = 0;


void setup() {
  pinMode(redButton, INPUT);
  pinMode(blueButton, INPUT);
  pinMode(greenButton, INPUT);
  pinMode(yellowButton, INPUT);
  pinMode(redLed, OUTPUT);
  pinMode(blueLed, OUTPUT);
  pinMode(greenLed, OUTPUT);
  pinMode(yellowLed, OUTPUT);
  randomSeed(analogRead(0));
  Serial.begin(9600);


}

void loop() {
  picker = random(1, 5);
  if (nextFreeIndex < maxCount) {
    switch (picker) {
      case 1:
        dataStorage[nextFreeIndex++] = 6;
        break;
      case 2:
        dataStorage[nextFreeIndex++] = 5;
        break;
      case 3:
        dataStorage[nextFreeIndex++] = 4;
        break;
      case 4:

        dataStorage[nextFreeIndex++] = 3;
        break;
    }
    for (int i = 0; i <= nextFreeIndex; i++) {
      digitalWrite(dataStorage[i], HIGH);
      delay(400);
      digitalWrite(dataStorage[i], LOW);
      while (redRead != 1 && blueRead != 1 && greenRead != 1 && yellowRead != 1) {
        redRead = digitalRead(redButton);
        blueRead = digitalRead(blueButton);
        greenRead = digitalRead(greenButton);
        yellowRead = digitalRead(yellowButton);
      }
      if (redRead == 1 && dataStorage[i] == 6) {
        digitalWrite(redLed, HIGH);
        delay(500);
        digitalWrite(redLed, LOW);
        redRead = 0;
        blueRead = 0;
        greenRead = 0;
        yellowRead = 0;
      }
      else if (blueRead == 1 && dataStorage[i] == 5) {
        digitalWrite(blueLed, HIGH);
        delay(500);
        digitalWrite(blueLed, LOW);
        redRead = 0;
        blueRead = 0;
        greenRead = 0;
        yellowRead = 0;
      }
      else if (greenRead == 1 && dataStorage[i] == 4) {
        digitalWrite(greenLed, HIGH);
        delay(500);
        digitalWrite(greenLed, LOW);
        redRead = 0;
        blueRead = 0;
        greenRead = 0;
        yellowRead = 0;
      }
      else if (yellowRead == 1 && dataStorage[i] == 3) {
        digitalWrite(yellowLed, HIGH);
        delay(500);
        digitalWrite(yellowLed, LOW);
        redRead = 0;
        blueRead = 0;
        greenRead = 0;
        yellowRead = 0;
      }
      else {
        break;
      }
      delay(100);
    }
  }
  else {
    while (counter > 4) {
      digitalWrite(redLed, HIGH);
      digitalWrite(blueLed, HIGH);
      digitalWrite(greenLed, HIGH);
      digitalWrite(yellowLed, HIGH);
      delay(500);
      counter++;
    }
  }
}



any help you have is greatly appreciated

What does that mean exactly?

ya lol
the first bit of code

for (int i = 0; i <= nextFreeIndex; i++) {
      digitalWrite(dataStorage[i], HIGH);
      delay(400);
      digitalWrite(dataStorage[i], LOW);
      while (redRead != 1 && blueRead != 1 && greenRead != 1 && yellowRead != 1) {
        redRead = digitalRead(redButton);
        blueRead = digitalRead(blueButton);
        greenRead = digitalRead(greenButton);
        yellowRead = digitalRead(yellowButton);

till about here works
then it gets stuck in an enless loop flashing random leds

Looking at your code I don't understand how the game is supposed to work.

I would expect the pattern of LEDs would first be displayed, then the user would try and copy. If they get it right another LED gets added to the pattern, and the cycle continues.

But in your code you seems to have the display of the pattern in the same loop with the button checking?

Adding to what @red_car said, try to code this in pseudocode first.

Here's a link to a blog post and code that works: https://arduinoplusplus.wordpress.com/2020/04/22/an-easy-simon-game/.

Looking at someone else's code may help you work out how to structure yours.

Here is one that works, and can tested in the Wokwi simulator: simon.ino - Wokwi Arduino and ESP32 Simulator
That code is smaller than all other "simon" sketches that I have seen.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.