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