Coding issue in arduino

Screenshot 2021-05-14 164145
Hello! please could someone tell me what is causing this?Here is my code

#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
int level = 0;
int score = 0;
int symbol = -1;
int index = 0;
int winner = 10;


int notes[] = {NOTE_A4, NOTE_B4, NOTE_C3, NOTE_D4};
byte displayArray[4] = {33, 3, 24, 12};
byte gameArray[10] = {};
bool DA = false;
byte floatingPin = 1;
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));
  initGameArray();
  DA = false;

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

    }*/
  pinMode(13, OUTPUT);
}
void loop()
{
  wait4key();
  random(0, 4);
  if (DA == true) {
  }
  DA = false;
  if (level > winner) {
    Serial.print("You Win!");
    level = 0;
    randomSeed(analogRead(floatingPin));
    gameArray[index] = (random(0, 4));
  }
}

void initGameArray() {
  DA = false;
  randomSeed(analogRead(floatingPin));
  // Serial.print("Simon says game,press the switch the 7 segment display is indicating to Start!");
  Serial.println("Follow the sequence");
  // gameArray[index]=random(0,4);
  // }
  for (index = 0; index < winner; index++) {
    gameArray[index] = (random(0, 4));
    Serial.print(" ");
    Serial.print(gameArray[index]);
    displaySymbol(gameArray[index]);



  }
  Serial.print("\n");
}

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() {

  if (digitalRead(SW1) == LOW) {
    while (digitalRead(SW1) == LOW) {}
    tone(9, notes[0], 20);
    //displaySymbol(0);
    //DA=true;
    symbol = 0;
    if (symbol == gameArray[index - 1]) {
      level++;
    } else {
      Serial.print("You LOSE,Press the correct button to restart the game");
      level = 0;
      Serial.print("\n");
      return;


    }
    for (index = 0; index < level; index++) {
      Serial.print(" ");
      Serial.print(gameArray[index]);
      displaySymbol(gameArray[index]);

    }
    Serial.print("\n");
  }




  if (digitalRead(SW2) == LOW) {
    while (digitalRead(SW2) == LOW) {}
    tone(9, notes[1], 20);
    //displaySymbol(1);
    //DA=true;
    symbol = 1;
    if (symbol == gameArray[index - 1]) {
      level++;
    } else {
      Serial.print("You LOSE,Press the right button to restart the game");
      level = 0;
      Serial.print("\n");
      return;


    }
    for (index = 0; index < level; index++) {
      Serial.print(" ");
      Serial.print(gameArray[index]);
      displaySymbol(gameArray[index]);
    }
    Serial.print("\n");
  }




  if (digitalRead(SW3) == LOW) {
    while (digitalRead(SW3) == LOW) {}
    tone(9, notes[2], 20);
    // displaySymbol(2);
    // DA=true;
    symbol = 2;
    if (symbol == gameArray[index - 1]) {
      level++;
    } else {
      Serial.print("You LOSE,Press the right button to restart the game");
      level = 0;
      Serial.print("\n");
      return;

    }
    for (index = 0; index < level; index++) {
      Serial.print(" ");
      Serial.print(gameArray[index]);
      displaySymbol(gameArray[index]);
    }
    Serial.print("\n");
  }


  if (digitalRead(SW4) == LOW) {
    while (digitalRead(SW4) == LOW) {}
    tone(9, notes[3], 20);
    // displaySymbol(3);
    //DA=true;
    symbol = 3;
    if (symbol == gameArray[index - 1]) {
      level++;
    } else {
      Serial.print("You LOSE,Press the right button to restart the game");
      level = 0;
      Serial.print("\n");
      return;

    }
    for (index = 0; index < level; index++) {
      Serial.print(" ");
      Serial.print(gameArray[index]);
      displaySymbol(gameArray[index]);
    }
    Serial.print("\n");
  }


  /*  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);

      }
    }*/
}

How baud is your serial monitor configured?

RV mineirin
baud

Thanks for getting back to me 9600 baud

I changed my baud and its better but why is the a here?

Hi
I tested your sketch, at 9600 baud, and it appears correct on the serial monitor.

RV mineirin

Follow the sequence
 2 3 2 1 0 2 1 0 2 1
 2
 2 3
 2 3 2
 2 3 2 1
 2 3 2 1 0
 2 3 2 1 0 2
 2 3 2 1 0 2 1
 2 3 2 1 0 2 1 0
 2 3 2 1 0 2 1 0 2
 2 3 2 1 0 2 1 0 2 1
 2 3 2 1 0 2 1 0 2 1 10
You Win!

Hi yes but after the win message and pressing a switch to restart then issue starts

In the sketch you posted this line is commented.

 // Serial.print("Simon says game,press the switch the 7 segment display is indicating to Start!");

RV mineirin

Yes I changed the follow the sequence to that message so I didn't need that

#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
int level = 0;
int score = 0;
int symbol = -1;
int index = 0;
int winner = 10;


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

  Serial.begin(57600);
  randomSeed(analogRead(0));
  pinMode(10, INPUT_PULLUP);
  pinMode(8, INPUT_PULLUP);
  pinMode(11, INPUT_PULLUP);
  pinMode(12, INPUT_PULLUP);
  randomSeed(analogRead(0));
  initGameArray();
  DA = false;

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

    }*/
  pinMode(13, OUTPUT);
}
void loop()
{
  wait4key();
  random(0, 4);
  if (DA == true) {
  }
  DA = false;
  if (level > winner) {
    Serial.print("You Win!,press the switch indicated by the 7 segment to restart");
    Serial.print("\n");
    level = 0;
    randomSeed(analogRead(floatingPin));
    gameArray[index] = (random(0, 4));
  }
}

void initGameArray() {
  DA = false;
  randomSeed(analogRead(floatingPin));
  // Serial.print("Simon says game,press the switch the 7 segment display is indicating to Start!");
  Serial.println("Simon says game,Press the switch indicated by the 7 segment to start");
  // gameArray[index]=random(0,4);
  // }
  for (index = 0; index < winner; index++) {
    gameArray[index] = (random(0, 4));
    Serial.print(" ");
    Serial.print(gameArray[index]);
    displaySymbol(gameArray[index]);



  }
  Serial.print("\n");
}

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));

  tone(9, notes[displayArray[sym]], 80);
}

void wait4key() {

  if (digitalRead(SW1) == LOW) {
    while (digitalRead(SW1) == LOW) {}
    tone(9, notes[0], 20);
    //displaySymbol(0);
    //DA=true;
    symbol = 0;
    if (symbol == gameArray[index - 1]) {
      level++;
    } else {
      Serial.print("You LOSE,Press the correct button to restart the game");
      level = 0;
      Serial.print("\n");
      return;


    }
    for (index = 0; index < level; index++) {
      Serial.print(" ");
      Serial.print(gameArray[index]);
      displaySymbol(gameArray[index]);

    }
    Serial.print("\n");
  }




  if (digitalRead(SW2) == LOW) {
    while (digitalRead(SW2) == LOW) {}
    tone(9, notes[1], 20);
    //displaySymbol(1);
    //DA=true;
    symbol = 1;
    if (symbol == gameArray[index - 1]) {
      level++;
    } else {
      Serial.print("You LOSE,Press the right button to restart the game");
      level = 0;
      Serial.print("\n");
      return;


    }
    for (index = 0; index < level; index++) {
      Serial.print(" ");
      Serial.print(gameArray[index]);
      displaySymbol(gameArray[index]);
    }
    Serial.print("\n");
  }




  if (digitalRead(SW3) == LOW) {
    while (digitalRead(SW3) == LOW) {}
    tone(9, notes[2], 20);
    // displaySymbol(2);
    // DA=true;
    symbol = 2;
    if (symbol == gameArray[index - 1]) {
      level++;
    } else {
      Serial.print("You LOSE,Press the right button to restart the game");
      level = 0;
      Serial.print("\n");
      return;

    }
    for (index = 0; index < level; index++) {
      Serial.print(" ");
      Serial.print(gameArray[index]);
      displaySymbol(gameArray[index]);
    }
    Serial.print("\n");
  }


  if (digitalRead(SW4) == LOW) {
    while (digitalRead(SW4) == LOW) {}
    tone(9, notes[3], 20);
    // displaySymbol(3);
    //DA=true;
    symbol = 3;
    if (symbol == gameArray[index - 1]) {
      level++;
    } else {
      Serial.print("You LOSE,Press the right button to restart the game");
      level = 0;
      Serial.print("\n");
      return;

    }
    for (index = 0; index < level; index++) {
      Serial.print(" ");
      Serial.print(gameArray[index]);
      displaySymbol(gameArray[index]);
    }
    Serial.print("\n");
  }


  /*  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);

      }
    }*/
}

Now I have used you code with baud 57.600 and look the result printout.

As I do not have the seven segment display plugged into the Arduino, I again sent the line with the codes to print so I could type them.

RV mineirin

Simon says game,Press the switch indicated by the 7 segment to start
 3 1 1 2 2 1 0 1 2 1
 3
 3 1
 3 1 1
 3 1 1 2
 3 1 1 2 2
 3 1 1 2 2 1
 3 1 1 2 2 1 0
 3 1 1 2 2 1 0 1
 3 1 1 2 2 1 0 1 2
 3 1 1 2 2 1 0 1 2 1
 3 1 1 2 2 1 0 1 2 1 10
You Win!,press the switch indicated by the 7 segment to restart
 3 0 1 3 0 2 3 3 2 1
 3
 3 0
 3 0 1
 3 0 1 3
 3 0 1 3 0
 3 0 1 3 0 2
 3 0 1 3 0 2 3
 3 0 1 3 0 2 3 3
 3 0 1 3 0 2 3 3 2
 3 0 1 3 0 2 3 3 2 1
 3 0 1 3 0 2 3 3 2 1 10
You Win!,press the switch indicated by the 7 segment to restart
 1 1 0 2 2 0 3 1 0 3

Oh wow! But how come mine doesn't print like that?

Perhaps a power problem.
How are you powering the arduino?
The seven segment are on the same power supply?

RV mineirin

Yes the 7 segment is powered up to 5v by the arduino

Ok since I can't solve that issue please do you know what I can do to end the game after the win message?

Hi,
try to place this routine after this message from the winner:
That's how I did the test.

Serial.print ("You Win !, press the switch indicated by the 7 segment to restart");

getting like this:

     Serial.print ("You Win !, press the switch indicated by the 7 segment to restart");
   for (index = 0; index <winner; index ++) {
     gameArray [index] = (random (0, 4));
     Serial.print ("");
     Serial.print (gameArray [index]);
     displaySymbol (gameArray [index]);
}

RV mineirin

1 Like

Oh thank you so much it works perfectly now :grin:

Hi,
if the solution met your need,
please check as solved.

RV mineirin

1 Like