Need help with a code, button reads too fast???

Hello, am relatively new to arduino and i have taken up a project in school where i need to make a game, which i have chosen simon says. I’m currently using a code made on youtube (Simon Says Arduino Wiring and Code Tutorial - YouTube) and have connected it the same way as the video. The game works, however when i press a single button down, it is taken as the next input. For example

Game Round 1 : Blinks red
Input presses: Red
Records as red

Game Round 2 : Blinks blue
Input not yet pressed but still records as red

Game then restarts

here is my video showing what i mean:

I also will be making a point system using LCD which i have yet to learn to code, if anyone can help explain to me what i can do to fix this error and guide me on how i can code using the LCD will be great. Sorry for bad english/explanation, and many thanks!

also, i’m using arcade push buttons and will be using LCD2004

int buttons[] = {8, 9, 10, 11}; //The four button input pins
int leds[] = {7, 6, 5, 4}; // LED pins

int sequence[100];

int largestIndex = 0;

int buttonPin = 2;

const int START = 0;
const int PLAY = 1;
const int GAMEOVER = 2;

int gameState;

int speed = 500;
boolean running = true;

long currentMillis = 0;
long previousMillis = 0;

void setup() { 
	pinMode(3,OUTPUT);
  	pinMode(2,OUTPUT);
  for(int pin=0; pin<4; pin++)
  {
  	pinMode(leds[pin], OUTPUT);
  }
  
  for(int pin=0; pin<4; pin++)
  {
  	pinMode(buttons[pin], INPUT);
  }
  
  Serial.begin(9600);
  gameState = START; 
  randomSeed(analogRead(40)); 
}

void loop() {
  
  if(gameState == START)
  {
  	waitToStart();
  }
  else if(gameState == PLAY)
  {
    Serial.println("Start"); 
    showSequence();
    readSequence();
  }
  else if(gameState == GAMEOVER)
  {
    Serial.println("Gameover");
    blinkAll(5);
    for (int x1 = 0; x1 < 5; x1++)
    {
		digitalWrite(2,HIGH);
		delay(300);
		digitalWrite(2,LOW);
		delay(300);
	}
    gameState = START;
  }
  
}

void showSequence()
{
  
  sequence[largestIndex] = random(0,4);
  Serial.println("Next out"); 
  Serial.println(sequence[largestIndex]); 
  largestIndex++;
  
  for(int index=0; index<largestIndex; index++)
  {
     delay(300);
   	 digitalWrite(leds[sequence[index]], HIGH);
     delay(700);
     digitalWrite(leds[sequence[index]], LOW);
  }
  
}

void readSequence()
{
 
  int positionPressed;
  boolean madeMistake = false;
  
  for(int index=0; index<largestIndex & madeMistake == false; index++)
  {
    Serial.println(""); 
    Serial.println("Should push"); 
    Serial.println(sequence[index]);
    
    positionPressed = waitForButton(1000); // 0, 1, 2, or 3

    Serial.println("Pressed"); 
    Serial.println(positionPressed); 
    if(positionPressed == -1 | positionPressed != sequence[index])
    {
      	madeMistake = true; // Exit the loop.
		gameState = GAMEOVER;
    }
  }
}

int waitForButton(int delay)
{
  int buttonPressed = -1;
  int input;
  boolean buttonBackUp = false;
  
  currentMillis = millis();
  previousMillis = currentMillis;

  while (currentMillis - previousMillis < delay & buttonBackUp == false)
  {
    for(int pin = 0; pin < 4 & buttonBackUp == false; pin++)
    {
      if(digitalRead(buttons[pin]) == HIGH)
      {
        buttonPressed = pin;

        digitalWrite(leds[pin], HIGH);

        while (currentMillis - previousMillis < delay & buttonBackUp == false)
        {
          input = digitalRead(buttons[pin]);
          if(input == LOW)
          {
            buttonBackUp = true;
          }
          currentMillis = millis();
        }

        digitalWrite(leds[pin], LOW);

        if(currentMillis - previousMillis > delay)
        {
          buttonPressed = -1; // They took to long to let the button up so they lose.
        }
      }
    }

    currentMillis = millis();
  }
  
  return buttonPressed;
}

void waitToStart()
{
  int buttonPressed = -1;
  allOff();
  
  for(int pin = 0; pin < 4; pin++)
  {
    if(buttonPressed == -1)
    {
      digitalWrite(leds[pin], HIGH);
      buttonPressed = waitForButton(200);
      digitalWrite(leds[pin], LOW);
    }
  }
  
  if(buttonPressed != -1)
  {
    delay(1000);
    largestIndex = 0; // Restart
   	gameState = PLAY; 
  }
}

void allOff()
{
  for(int pin = 0; pin < 4; pin++)
  {
    digitalWrite(leds[pin], LOW);
  }
}

void allOn()
{
  for(int pin = 0; pin < 4; pin++)
  {
    digitalWrite(leds[pin], HIGH);
  }
}

boolean readAnyButton(int delay)
{
  boolean buttonDown = false;
  
  currentMillis = millis();
  previousMillis = currentMillis;

  while (currentMillis - previousMillis < delay & buttonDown == false)
  {
    for(int pin = 0; pin < 4; pin++)
    {
      if(digitalRead(buttons[pin]) == HIGH)
      {
        buttonDown = true;
      }
    }

    currentMillis = millis();
  }
  
  return buttonDown;
}

void blinkAll(int times)
{
  for(int count = 0; count < times; count++)
  {
    allOn();
    delay(300);
    allOff();
    delay(300);
  } 
}

void blinkRed(int times)
{
  for(int count = 0; count < times; count++)
  {
    digitalWrite(leds[0], HIGH);
    delay(300);
    digitalWrite(leds[0], LOW);
    delay(300);
  } 
}

void blinkGreen(int times)
{
  for(int count = 0; count < times; count++)
  {
    digitalWrite(leds[1], HIGH);
    delay(300);
    digitalWrite(leds[1], LOW);
    delay(300);
  } 
}

void blinkYellow(int times)
{
  for(int count = 0; count < times; count++)
  {
    digitalWrite(leds[2], HIGH);
    delay(300);
    digitalWrite(leds[2], LOW);
    delay(300);
  } 
}

void blinkBlue(int times)
{
  for(int count = 0; count < times; count++)
  {
    digitalWrite(leds[3], HIGH);
    delay(300);
    digitalWrite(leds[3], LOW);
    delay(300);
  } 
}

I can spot a bug which i present in multiple places:

//This is how your code looks:
for(int index=0; index<largestIndex & madeMistake == false; index++)

//This is how it should look:
for(int index=0; (index<largestIndex) && (madeMistake == false); index++)

Mind that “&” becomes “&&”. Even though the extra parenthesis are not necessary, they help to group and make the code easier to understand.

thanks! just realised that too, and ive fixed all the & to &&, still facing the issue

You do not have any debouncing of the buttons, the easiest sollution is to put in a delay of 20ms to 50ms right after the button has been registred as pressed.

sketch_jan07a:120:17: error: 'delay' cannot be used as a function

         delay(20);

                 ^

exit status 1
'delay' cannot be used as a function

i got this as an error, any idea how i can fix this? i put delay(20); i put this under digitalWrite(leds[pin], HIGH); in int waitForButton(int delay)

Please post the whole of your latest attempt so we don't have to guess exactly what you have done and where

this is my code right now

int buttons[] = {8, 9, 10, 11}; //The four button input pins
int leds[] = {7, 6, 5, 4}; // LED pins

int sequence[100];

int largestIndex = 0;

int buttonPin = 2;

const int START = 0;
const int PLAY = 1;
const int GAMEOVER = 2;

int gameState;

int speed = 500;
boolean running = true;

long currentMillis = 0;
long previousMillis = 0;

void setup() { 
  for(int pin=0; pin<4; pin++)
  {
    pinMode(leds[pin], OUTPUT);
  }
  
  for(int pin=0; pin<4; pin++)
  {
    pinMode(buttons[pin], INPUT);
  }
  
  Serial.begin(9600);
  gameState = START; 
  randomSeed(analogRead(40)); 
}

void loop() {
  
  if(gameState == START)
  {
    waitToStart();
  }
  else if(gameState == PLAY)
  {
    Serial.println("Start"); 
    showSequence();
    readSequence();
  }
  else if(gameState == GAMEOVER)
  {
    Serial.println("Gameover");
    blinkAll(5);
    
    gameState = START;
  }
  
}

void showSequence()
{
  
  sequence[largestIndex] = random(0,4);
  Serial.println("Next out"); 
  Serial.println(sequence[largestIndex]); 
  largestIndex++;
  
  for(int index=0; index<largestIndex; index++)
  {
     delay(300);
     digitalWrite(leds[sequence[index]], HIGH);
     delay(700);
     digitalWrite(leds[sequence[index]], LOW);
  }
  
}

void readSequence()
{
 
  int positionPressed;
  boolean madeMistake = false;
  
  for(int index=0; index<largestIndex && madeMistake == false; index++)
  {
    Serial.println(""); 
    Serial.println("Should push"); 
    Serial.println(sequence[index]);
    
    positionPressed = waitForButton(5000); // 0, 1, 2, or 3

    Serial.println("Pressed"); 
    Serial.println(positionPressed); 
    if(positionPressed == -1 | positionPressed != sequence[index])
    {
        madeMistake = true; // Exit the loop.
    gameState = GAMEOVER;
    }
  }
}

int waitForButton(int delay)
{
  int buttonPressed = -1;
  int input;
  boolean buttonBackUp = false;
  
  currentMillis = millis();
  previousMillis = currentMillis;

  while (currentMillis - previousMillis < delay && buttonBackUp == false)
  {
    for(int pin = 0; pin < 4 && buttonBackUp == false; pin++)
    {
      if(digitalRead(buttons[pin]) == HIGH)
      {
        buttonPressed = pin;

        digitalWrite(leds[pin], HIGH);
        delay(20);
        
        while (currentMillis - previousMillis < delay && buttonBackUp == false)
        {
          input = digitalRead(buttons[pin]);
          if(input == LOW)
          {
            buttonBackUp = true;
          }
          currentMillis = millis();
        }

        digitalWrite(leds[pin], LOW);

        if(currentMillis - previousMillis > delay)
        {
          buttonPressed = -1; // They took to long to let the button up so they lose.
        }
      }
    }

    currentMillis = millis();
  }
  
  return buttonPressed;
}

void waitToStart()
{
  int buttonPressed = -1;
  allOff();
  
  for(int pin = 0; pin < 4; pin++)
  {
    if(buttonPressed == -1)
    {
      digitalWrite(leds[pin], HIGH);
      buttonPressed = waitForButton(700);
      digitalWrite(leds[pin], LOW);
    }
  }
  
  if(buttonPressed != -1)
  {
    delay(1000);
    largestIndex = 0; // Restart
    gameState = PLAY; 
  }
}

void allOff()
{
  for(int pin = 0; pin < 4; pin++)
  {
    digitalWrite(leds[pin], LOW);
  }
}

void allOn()
{
  for(int pin = 0; pin < 4; pin++)
  {
    digitalWrite(leds[pin], HIGH);
  }
}

boolean readAnyButton(int delay)
{
  boolean buttonDown = false;
  
  currentMillis = millis();
  previousMillis = currentMillis;

  while (currentMillis - previousMillis < delay && buttonDown == false)
  {
    for(int pin = 0; pin < 4; pin++)
    {
      if(digitalRead(buttons[pin]) == HIGH)
      {
        buttonDown = true;
      }
    }

    currentMillis = millis();
  }
  
  return buttonDown;
}

void blinkAll(int times)
{
  for(int count = 0; count < times; count++)
  {
    allOn();
    delay(300);
    allOff();
    delay(300);
  } 
}

void blinkRed(int times)
{
  for(int count = 0; count < times; count++)
  {
    digitalWrite(leds[0], HIGH);
    delay(300);
    digitalWrite(leds[0], LOW);
    delay(300);
  } 
}

void blinkGreen(int times)
{
  for(int count = 0; count < times; count++)
  {
    digitalWrite(leds[1], HIGH);
    delay(300);
    digitalWrite(leds[1], LOW);
    delay(300);
  } 
}

void blinkYellow(int times)
{
  for(int count = 0; count < times; count++)
  {
    digitalWrite(leds[2], HIGH);
    delay(300);
    digitalWrite(leds[2], LOW);
    delay(300);
  } 
}

void blinkBlue(int times)
{
  for(int count = 0; count < times; count++)
  {
    digitalWrite(leds[3], HIGH);
    delay(300);
    digitalWrite(leds[3], LOW);
    delay(300);
  } 
}

still facing the error

test1:120:17: error: 'delay' cannot be used as a function

         delay(20);

                 ^

exit status 1
'delay' cannot be used as a function
int waitForButton(int delay)

You are using the keyword delay as the name of a variable. Change the name of the variable in that line and wherever it occurs in the function

I think your problem is that you use also delay as a variable name (3 times):

int waitForButton(int delay)

and

while (currentMillis - previousMillis < delay && buttonBackUp == false)

Try changing this variable name.

oh my god thank you! it works fine now :slight_smile: