Problem Arduino Simon Game button constantly activated without clicking it

When trying to start the simulator it 's constantly thinking that the buttons are being pushed which results in the console being flooded with messages says you've pressed a button and you've lost.
What can I do to resolve this issue?

Console

Code

const int btnGreen = 6;
const int btnRed = 7;
const int btnBlue = 8;
const int btnYellow = 9;
int patern[10];

void setup() {
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(btnGreen, INPUT);
  pinMode(btnRed, INPUT);
  pinMode(btnBlue, INPUT);
  pinMode(btnYellow, INPUT);
  
  Serial.begin(9600);  
  
  resetPins();
  generate_patern();
  start();
}

void loop() {

}

void resetPins() {
  for (int i = 2; i < 7; i++) {
    digitalWrite(i, LOW);
  }
}

void start() 
{
  int round = 0;
  int playersMove = 1;
  bool btnPushed = false;
  int currentLed;
  int playerInput = 0;
  delay(1000);
  for (int currentRound = 1; (currentRound - 1)<= 10; currentRound++) // Number of rounds to play
  {
    ++round;
    playersMove = 1;
    btnPushed = false;
    playerInput = 0;
    
    for(int i = 0; i < playersMove; ++i) {
      currentLed = patern[i];
      digitalWrite(currentLed, HIGH);
      delay(750);
      digitalWrite(currentLed, LOW);
    }
    
    while (playersMove < currentRound) {
   	  currentLed = patern[playersMove - 1];
      
      while (btnPushed == false) {
        if (digitalRead(btnGreen) == LOW) // Knop 1
        {
          Serial.println("Pressed_1");
          playerInput = 1;
          btnPushed = true;
        }
        else if (digitalRead(btnRed) == LOW) // Knop 2
        {
          Serial.println("Pressed_2");
          playerInput = 2;
          btnPushed = true;
        }
        else if (digitalRead(btnBlue) == LOW) // Knop 3
        {
          Serial.println("Pressed_3");
          playerInput = 3;
          btnPushed = true;
        }
        else if (digitalRead(btnGreen) == LOW) // Knop 4
        {
          Serial.println("Pressed_4");
          playerInput = 4;
          btnPushed = true;
        }    
      }
      
      if (btnPushed == true)
        {
          Serial.println("Button has been pushed");
             
          if(currentLed == playerInput)
          {
            playersMove++;
          }
          else
          {
            won(false);
          }
        }
    	btnPushed = false;
    }
  }
  won(true);
}
    


void generate_patern() {
  randomSeed(analogRead(1));
  for (int i=0; i< 10; i++)
  {
    patern[i] = random(1, 5);
  }
}

void won(bool win) {
  if (win) {
    Serial.println("You have won");
  }
  
  else {
    Serial.println("You have lost");
  }
  Serial.println("Game over");
}

The console after starting the simulation

Hi @uapylnjff .
Can you post your project schematic?
So that we can better understand how these buttons are connected.

RV mineirin

Sorry forgot it

Is it because you should have 6 and not 7 in this, as you've set green low by this line?

Hi @uapylnjff .
modify these lines

pinMode(btnGreen, INPUT);
pinMode(btnRed, INPUT);
pinMode(btnBlue, INPUT);
pinMode(btnYellow, INPUT);

to look like this:

pinMode(btnGreen, INPUT_PULLUP);
pinMode(btnRed, INPUT_PULLUP);
pinMode(btnBlue, INPUT_PULLUP);
pinMode(btnYellow, INPUT_PULLUP);

RV mineirin

The spamming has stopped with the two suggestions that were made but now whenever I start the game and click the button next to the led which is turning on it's telling me I've lost and game over

Here's the link to the simulator
https://www.tinkercad.com/things/2EpLiCIvjLe-copy-of-simon-keuzedeel-examen/editel?sharecode=hALDvMFFwjm8MrhGd6mfigLUUxeicx92nrCDxNgq9jU

It's no longer spamming but it created another issue and I'm not sure how to fix it when clicking on the button next to the led which turned on it keeps saying that You've lost
here's the link to the simulator
https://www.tinkercad.com/things/2EpLiCIvjLe-copy-of-simon-keuzedeel-examen/editel?sharecode=hALDvMFFwjm8MrhGd6mfigLUUxeicx92nrCDxNgq9jU

Hi @uapylnjff .
I don't like simulators.
I'm real riding here and testing.

RV mineirin

Hi,

As @ruilviana has said, get out the hardware and BUILD IT.

Tom... :smiley: :+1: :coffee: :australia:

Hi @uapylnjff .
There were several errors, so correct your sketch.
Test and then count the result.

RV mineirin

const int btnGreen = 6;
const int btnRed = 7;
const int btnBlue = 8;
const int btnYellow = 9;
int patern[10];

void setup() {
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(btnGreen, INPUT_PULLUP);
  pinMode(btnRed, INPUT_PULLUP);
  pinMode(btnBlue, INPUT_PULLUP);
  pinMode(btnYellow, INPUT_PULLUP);
  
  Serial.begin(9600);  
  
  resetPins();
  generate_patern();
  start();
}

void loop() {

}

void resetPins() {
  for (int i = 2; i < 6; i++) {
    digitalWrite(i, LOW);
  }
}

void start() 
{
  int round = 0;
  int playersMove = 1;
  bool btnPushed = false;
  int currentLed;
  int playerInput = 0;
  delay(1000);
  for (int currentRound = 1; (currentRound - 1)<= 10; currentRound++) // Number of rounds to play
  {
    ++round;
    btnPushed = false;
    playerInput = 0;
   
    for(int i = 0; i < playersMove; ++i) {
      currentLed = patern[i];
      digitalWrite(currentLed, HIGH);
      delay(250);
      digitalWrite(currentLed, LOW);
    }
    
    while (playersMove < currentRound) {

      Serial.println(currentLed);
      
      while (btnPushed == false) {
        if (digitalRead(btnGreen) == LOW) // Button 1
        {
          Serial.println("Pressed_2");
          playerInput = 2;
          btnPushed = true;
        }
        else if (digitalRead(btnRed) == LOW) // Button 3
        {
          Serial.println("Pressed_3");
          playerInput = 3;
          btnPushed = true;
        }
        else if (digitalRead(btnBlue) == LOW) // Button 4
        {
          Serial.println("Pressed_4");
          playerInput = 4;
          btnPushed = true;
        }
        else if (digitalRead(btnYellow) == LOW) // Button 5
        {
          Serial.println("Pressed_5");
          playerInput = 5;
          btnPushed = true;
        }    
      }
      
      if (btnPushed == true)
      {
        Serial.println("Button has been pushed");

        if(currentLed == playerInput)
        {
          Serial.println("ok");
          playersMove++;
        }
        else
        {
          Serial.println(currentLed);
          won(false);
        }
      }
      btnPushed = false;
    }
  }
  won(true);
}
    


void generate_patern() {
  randomSeed(analogRead(1));
  for (int i=0; i< 10; i++)
  {
    patern[i] = random(2, 6);
  }
}

void won(bool win) {
  if (win) {
    Serial.println("You have won");
  }
  
  else {
    Serial.println("You have lost");
  }
  Serial.println("Game over");
}

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