Simon says game using 7 segment and arduino

#define SW1 8
#define SW2 11
#define SW3 10
#define SW4 12

#define aSeg 2
#define bSeg 3
#define cSeg 4
#define dSeg 5
#define eSeg 6
#define fSeg 7
#define gSeg 13

#define NOTE_A4 440
#define NOTE_B4 440
#define NOTE_C3 440
#define NOTE_D4 440

const int MAX_LEVEL = 3;
int sequence[MAX_LEVEL];
int your_sequence[MAX_LEVEL];                                                                         
int level=1;

int notes[]={NOTE_A4,NOTE_B4,NOTE_C3,NOTE_D4};
byte displayArray[4]={33,3,24,12};
byte gameArray[10]={1,3,1,0,2,3,0,0,1,0};
bool DA=false;
void setup()
{
  
  Serial.begin(9600);
  randomSeed(analogRead(0));
  pinMode(10,INPUT_PULLUP);
  pinMode(8,INPUT_PULLUP);
  pinMode(11,INPUT_PULLUP);
  pinMode(12,INPUT_PULLUP);
  randomSeed(analogRead(0));
  
 /* for(int pin=8;pin<=12;pin++){
  pinMode(pin,INPUT_PULLUP);
  
}*/
  pinMode(13,OUTPUT);
}
void loop()
{  
  wait4key();
  random(0,4);
  if(DA==true){
    initGameArray();
    //Serial.println("Init Array");
    DA=false;
  } 
  
}
  void initGameArray(){
  Serial.println("Init Array Function");
  for(int index=0;index<10;index++){
  gameArray[index]=random(0,4);
    }
    for(int index=0;index<10;index++){
     Serial.print(index);
     Serial.print("\t");
     Serial.print(gameArray[index]);
      Serial.print("\t");
      Serial.println(displayArray[index]);
     displaySymbol(gameArray[index]);
     delay(1000);
    }
}
void displaySymbol(byte sym){
digitalWrite(aSeg,bitRead(displayArray[sym],0));
digitalWrite(bSeg,bitRead(displayArray[sym],1));
digitalWrite(cSeg,bitRead(displayArray[sym],2));
digitalWrite(dSeg,bitRead(displayArray[sym],3));
digitalWrite(eSeg,bitRead(displayArray[sym],4));
digitalWrite(fSeg,bitRead(displayArray[sym],5));
digitalWrite(gSeg,bitRead(displayArray[sym],6));
}

void wait4key(){
  for (int i = 0; i < level; i++){
 if(digitalRead(SW1)==LOW){
  while(digitalRead(SW1)==LOW){}
  Serial.println("SW1 Pressed");
   tone(9,notes[0],20);
   displaySymbol(0);
    DA=true;
  }
  
 
  if(digitalRead(SW2)==LOW){
    while(digitalRead(SW2)==LOW){}
  Serial.println("SW2 Pressed");
   tone(9,notes[1],20);
    displaySymbol(1);
     DA=true;
  }
          if(digitalRead(SW3)==LOW){
            while(digitalRead(SW3)==LOW){}
  Serial.println("SW3 Pressed");
     tone(9,notes[2],20);
            displaySymbol(2);
             DA=true;
  }
        
  if(digitalRead(SW4)==LOW){
    while(digitalRead(SW4)==LOW){}
  Serial.println("SW4 Pressed");
    tone(9,notes[3],20);
    displaySymbol(3);
     DA=true;
  }
 }
  
  
/*  for(int pin=8;pin<=12;pin++){
    if(digitalRead(pin)==LOW){
      while(digitalRead(pin)==LOW){}
      Serial.print("Button Pressed=");
      Serial.println(pin-8);
      tone(11,notes[pin-8],20);
      //displaySymbol(pin-8);
      
    }
  }*/
}

void playgame(){}

  

Hey! I’m trying to make my code to only display one array on the 7 segment display but its keeps on initializing more arrays.

Posting a Schematic, not a frizzy thing would help showing all interconnections, power, and ground. Please explain what an array on the 7 segment display is? This is new to me.

Hello! Thanks for getting back to me basically a random array of numbers from 0 to 4 forms a sequence on the 7 segment display .
Inf unction byte game Array which holds our random sequence
0 stands for a and f segment on
1 stands for a and b segment on
2 stands for c and d segment on
3 stands for e and d segment on
These are linked with the data in byte displayArray
When simulating,every time a switch is pressed a different array initializes each time but this time I only want the array I chose in byte game Array to display on the 7 segment display.

There are no pinMode() statements for the LED segment pins. These must be defined with OUTPUT.
The wiring to the buttons appears odd. Each pair of buttons seems to use a shared pin.
You should format your code in the IDE before publishing it here because otherwise it is very difficult to read due to misleading indentation.

Oh yeah thanks!The wiring appear to be overlapping due to the software used when you click on them but they are connnected to different pins.Please could you explain what you mean by IDE?

IDE : Arduino's Interactive Development Environment. That software into which you typed all that C++ code. There is a feature there ( tools => auto format ) for nicely presenting the code.

Now you know why proper schematics are preferred here instead of Fritzing diagrams.

#define SW1 8
#define SW2 11
#define SW3 10
#define SW4 12

#define aSeg 2
#define bSeg 3
#define cSeg 4
#define dSeg 5
#define eSeg 6
#define fSeg 7
#define gSeg 13

#define NOTE_A4 440
#define NOTE_B4 494
#define NOTE_C3 131
#define NOTE_D4 294

const int MAX_LEVEL = 3;
int sequence[MAX_LEVEL];
int your_sequence[MAX_LEVEL];
int level = 1;

int notes[] = {NOTE_A4, NOTE_B4, NOTE_C3, NOTE_D4};
byte displayArray[4] = {33, 3, 24, 12};
byte gameArray[10] = {1, 3, 1, 0, 2, 3, 0, 0, 1, 0};
bool DA = false;
void setup()
{

  Serial.begin(9600);
  randomSeed(analogRead(0));
  pinMode(10, INPUT_PULLUP);
  pinMode(8, INPUT_PULLUP);
  pinMode(11, INPUT_PULLUP);
  pinMode(12, INPUT_PULLUP);
  randomSeed(analogRead(0));
  pinMode(aSeg, OUTPUT);
  pinMode(bSeg, OUTPUT);
  pinMode(cSeg, OUTPUT);
  pinMode(dSeg, OUTPUT);
  pinMode(eSeg, OUTPUT);
  pinMode(fSeg, OUTPUT);
  pinMode(gSeg, OUTPUT);

  /* for(int pin=8;pin<=12;pin++){
    pinMode(pin,INPUT_PULLUP);

    }*/
  pinMode(13, OUTPUT);
}
void loop()
{
  wait4key();
  random(0, 4);
  if (DA == true) {
    initGameArray();
    //Serial.println("Init Array");
    DA = false;
  }

}
void initGameArray() {
  Serial.println("Init Array Function");
  for (int index = 0; index < 10; index++) {
    gameArray[index] = random(0, 4);
  }
  for (int index = 0; index < 10; index++) {
    Serial.print(index);
    Serial.print("\t");
    Serial.print(gameArray[index]);
    Serial.print("\t");
    Serial.println(displayArray[index]);
    displaySymbol(gameArray[index]);
    delay(1000);
  }
}
void displaySymbol(byte sym) {
  digitalWrite(aSeg, bitRead(displayArray[sym], 0));
  digitalWrite(bSeg, bitRead(displayArray[sym], 1));
  digitalWrite(cSeg, bitRead(displayArray[sym], 2));
  digitalWrite(dSeg, bitRead(displayArray[sym], 3));
  digitalWrite(eSeg, bitRead(displayArray[sym], 4));
  digitalWrite(fSeg, bitRead(displayArray[sym], 5));
  digitalWrite(gSeg, bitRead(displayArray[sym], 6));
}

void wait4key() {
  for (int i = 0; i < level; i++) {
    if (digitalRead(SW1) == LOW) {
      while (digitalRead(SW1) == LOW) {}
      Serial.println("SW1 Pressed");
      tone(9, notes[0], 20);
      displaySymbol(0);
      DA = true;
    }


    if (digitalRead(SW2) == LOW) {
      while (digitalRead(SW2) == LOW) {}
      Serial.println("SW2 Pressed");
      tone(9, notes[1], 20);
      displaySymbol(1);
      DA = true;
    }
    if (digitalRead(SW3) == LOW) {
      while (digitalRead(SW3) == LOW) {}
      Serial.println("SW3 Pressed");
      tone(9, notes[2], 20);
      displaySymbol(2);
      DA = true;
    }

    if (digitalRead(SW4) == LOW) {
      while (digitalRead(SW4) == LOW) {}
      Serial.println("SW4 Pressed");
      tone(9, notes[3], 20);
      displaySymbol(3);
      DA = true;
    }
  }


  /*  for(int pin=8;pin<=12;pin++){
      if(digitalRead(pin)==LOW){
        while(digitalRead(pin)==LOW){}
        Serial.print("Button Pressed=");
        Serial.println(pin-8);
        tone(11,notes[pin-8],20);
        //displaySymbol(pin-8);

      }
    }*/
}

void playgame() {}




That looks a lot better. But does it still fail ?
If so, explain the symptoms, including what you see on the serial console. If it is getting stuck somewhere, start adding Serial.print() statements to find out what is happening.

Yes its still initializing different arrays each time I press a switch instead of the one I want.I used the arduino in tinkercad.
1

2

3

4

That initialisation is being done in function: initGameArray()
It currently happens every time a key is pressed.
When should it happen ?

It should only happen once and display the array I chose in byte gameArray[10]

And from that array it displays a sequence in each level lets say first level it displays the sequence 1 then the user presses the switch.
Next level the sequence displayed is 1,3 and the user presses the switch again

If you want to fix the contents of gameArray here only,

byte gameArray[10] = {1, 3, 1, 0, 2, 3, 0, 0, 1, 0};

Then start by commenting out the following line in initGameArray()

gameArray[index] = random(0, 4);

It will still print the contents, but this should not change.

Yes its works thank you so much

I'm glad that was successful. If you don't want all the printing each time, you can move this from the loop() and put in at the end of setup().

Alright thanks but do you know how I can make the 7 segment display read the switches I input so that I can win or lose the game?

You'll have to explain the game to me. How, by reading the switches, do you determine whether you have won or lost the game ?