Simon sais. Code doesn't work

What i am trying to accomplish is to make an simon sais game. The problem is that when i press the button i get no response if it's correct or incorrect.

const int b1 = 9;
const int b2 = 10;
const int b3 = 11;
const int b4 = 12;
const int p_LEDs[] = {2, 3, 4, 5};
long OnTime = 50;
long OffTime = 150;
const int maxSequenceLength = 10;
int sequenceLength = 1;
bool buttonstate1 = LOW;
bool buttonstate2 = LOW;
bool buttonstate3 = LOW;
bool buttonstate4 = LOW;
int sequence[maxSequenceLength];
int Points = 0;
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);

void setup() {


  for (int i = 0; i < 4; i++) {
    pinMode (p_LEDs [i], OUTPUT);
    pinMode(b1, INPUT);
    pinMode(b2, INPUT);
    pinMode(b3, INPUT);
    pinMode(b4, INPUT);

  }


}



void loop () {
  int B_1 = 0;
  int B2 = 0;
  int B3 = 0;
  int B4 = 0;


  buttonstate1 = digitalRead(b1);
  buttonstate2 = digitalRead(b2);
  buttonstate3 = digitalRead(b3);
  buttonstate4 = digitalRead(b4);

  for (int i = 0; i < maxSequenceLength; i++) {
    sequence[i] = random(0, 4); // Random sequence maken met maar 4 lampjes.

    int userArray[sequenceLength];
    int z ;
    int Y;
    int n;

    for (int x = 0; x < sequenceLength; x ++) {
      digitalWrite(p_LEDs[ sequence[i] ], HIGH);
      delay(500);
      digitalWrite(p_LEDs[ sequence[i] ], LOW);
      delay(200);
    }

    while ( z < sequenceLength) {
      for ( z = 0; z < sequenceLength;) {
        if (buttonstate1 == HIGH) {
          B_1 == 0;
          userArray[z] = B_1;
          z++;

        }

        if (buttonstate2 == HIGH) {
          B2 == 1 ;
          userArray[z] = B2;
          z++;
        }


        if (buttonstate3 == HIGH) {
          B3 == 2 ;
          userArray[z] = B3;
          z++;
        }

        if (buttonstate4 == HIGH) {
          B4 == 3 ;
          userArray[z] = B4;
          z++;
        }
      }
    }

    if (z == sequenceLength ) {
      break;
    }



    for (int n = 0; n < sequenceLength ; n++)
    {
      if (sequence[sequenceLength] == userArray[sequenceLength] )
      {
        Goed();
        sequenceLength ++;
      }
      else {
        sequenceLength = 1;
        Fout();

      }
    }
    delay(5000);
  }
}


void Goed() {

  digitalWrite (p_LEDs [0, 1, 2, 3], HIGH);
  delay (500);
  digitalWrite (p_LEDs [0, 1, 2, 3], LOW);
  delay (1000);
  Points++;
  lcd.begin(16, 2);
  lcd.home ();
  lcd.print("Correct!");
  lcd.setCursor ( 0, 1 );
  lcd.print(Points);
  lcd.print (" Punten");
  delay(1000);
  lcd.clear();
}

void Fout() {
  lcd.begin(16, 2);
  lcd.home ();
  lcd.print("Game Over!");
  delay(1000);
  lcd.clear();

}

How are the buttons wired? Do you have pulldown resistors on the the button inputs?

groundFungus:
How are the buttons wired? Do you have pulldown resistors on the the button inputs?

Example:
Arduino Pin 7 to buttonpin1
From buttonpin1 a resistor to gnd
Buttonpin2 to 5v

Do the pushbuttons have 4 pins, like the one in this picture?

The pins are already connected together as two pairs.

It is easy to select the wrong pins to connect to, if you are not aware of how they are already connected. You end up with a button which appears to be permanently pressed.

If you choose two diagonally opposite pins, it should be right every time.

JohnLincoln:
Do the pushbuttons have 4 pins, like the one in the picture?

The pins are already connected together as two pairs.

It is easy to select the wrong pins to connect to, if you are not aware of how they are already connected. You end up with a button which appears to be permanently pressed.

If you choose two diagonally opposite pins, it should be right every time.

it only has 2 pins

  B3 == 2 ;oops

Bad_At_Coding:
it only has 2 pins

That rules out my suggestion, then.

Remove the resistor and connect the input to ground through the button.
Then enable the internal pull up resistors in the pinMode call.

.

Grumpy_Mike:
Remove the resistor and connect the input to ground through the button.
Then enable the internal pull up resistors in the pinMode call.

Did that, nothing changed.

Also
When i read the value of the state of the button with serial.print i get 1 as a value even though i didn't pres it in. I also only get it 2 times

Use serial messages to trace your sketch.
Did you fix the pointless comparisons pointed out by @AWOL?

const int b1 = 9;
const int b2 = 10;
const int b3 = 11;
const int b4 = 12;
const int p_LEDs[] = {2, 3, 4, 5};
long OnTime = 50;
long OffTime = 150;
const int maxSequenceLength = 10;
int sequenceLength = 1;
int sequence[maxSequenceLength];

int Points = 0;
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);

void setup() {
Serial.begin(9600);



  for (int i = 0; i < 4; i++) {
    pinMode (p_LEDs [i], OUTPUT);
    pinMode(b1,INPUT_PULLUP);
    pinMode(b2,INPUT_PULLUP);
    pinMode(b3,INPUT_PULLUP);
    pinMode(b4,INPUT_PULLUP);
  }


}



void loop () {
  
  int B_1 = 0;
  int B2 = 0;
  int B3 = 0;
  int B4 = 0;

  
  for (int i = 0; i < maxSequenceLength; i++) {
    sequence[i] = random(0, 4); 

    int userArray[sequenceLength];
    int z ;
    int Y;
    int n;

    for (int x = 0; x < sequenceLength; x ++) {
      digitalWrite(p_LEDs[ sequence[i] ], HIGH);
      delay(500);
      digitalWrite(p_LEDs[ sequence[i] ], LOW);
      delay(200);
     
    }

    while (z = 0, z < sequenceLength)  {
        if (digitalRead(b1) == HIGH) {
          B_1 = 0;
          userArray[z] = B_1;
          z++;

        }

        if (digitalRead(b2) == HIGH) {
          B2 = 1 ;
          userArray[z] = B2;
          z++;
        }


        if (digitalRead(b3) == HIGH) {
          B3 = 2 ;
          userArray[z] = B3;
          z++;
        }

        if (digitalRead(b4) == HIGH) {
          B4 = 3 ;
          userArray[z] = B4;
          z++;
        }
     

    if (z  == sequenceLength) {
      break;
    }
   }



    for (int n = 0; n < sequenceLength ; n++)
    {
      if (sequence[sequenceLength] == userArray[sequenceLength] )
      {
        Goed();
        sequenceLength ++;
      }
      else {
        sequenceLength = 1;
        Fout();

      }
    }
    delay(5000);
  }
}


void Goed() {

  digitalWrite (p_LEDs [0, 1, 2, 3], HIGH);
  delay (500);
  digitalWrite (p_LEDs [0, 1, 2, 3], LOW);
  delay (1000);
  Points++;
  lcd.begin(16, 2);
  lcd.home ();
  lcd.print("Correct!");
  lcd.setCursor ( 0, 1 );
  lcd.print(Points);
  lcd.print (" Punten");
  delay(1000);
  lcd.clear();
}

void Fout() {
  digitalWrite (p_LEDs [3, 3, 3], HIGH);
  delay (500);
  digitalWrite (p_LEDs [3, 3, 3], LOW);
  delay (1000);
  lcd.begin(16, 2);
  lcd.home ();
  lcd.print("Game Over!");
  delay(1000);
  lcd.clear();

}

Now i removed the resistors. And edited the code

  while (z = 0, z < sequenceLength) Have another try.
Yell if you get stuck

for (int i = 0; i < 4; i++) {
    pinMode (p_LEDs [i], OUTPUT);
}
pinMode(b1,INPUT_PULLUP);
pinMode(b2,INPUT_PULLUP);
pinMode(b3,INPUT_PULLUP);
pinMode(b4,INPUT_PULLUP);

Bad_At_Coding:
.Did that, nothing changed.

Yes something did change, your buttons now work properly if you did it right. If it did not fix your code then there is something else you have wrong.

if (digitalRead(b2) == HIGH) {

If you changed the (normally open?) switch wiring as described by Grumpy Mike, the switch inputs will read low when when the switch is closed (pressed).

AWOL:
  while (z = 0, z < sequenceLength) Have another try.
Yell if you get stuck

The code does work now, the only problem is that it always says userinput is not equal to the light sequence.
Don't know why.

const int b1 = 9;
const int b2 = 10;
const int b3 = 11;
const int b4 = 12;
const int p_LEDs[] = {2, 3, 4, 5};
long OnTime = 50;
long OffTime = 150;
const int maxSequenceLength = 10;
int sequenceLength = 1;
int sequence[maxSequenceLength];

int Points = 0;
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);

void setup() {
Serial.begin(9600);


for (int i = 0; i < 4; i++) {
    pinMode (p_LEDs [i], OUTPUT);
}
pinMode(b1,INPUT_PULLUP);
pinMode(b2,INPUT_PULLUP);
pinMode(b3,INPUT_PULLUP);
pinMode(b4,INPUT_PULLUP);


}



void loop () {
  
  int B_1 = 0;
  int B2 = 0;
  int B3 = 0;
  int B4 = 0;

  
  for (int i = 0; i < maxSequenceLength; i++) {
    sequence[i] = random(0, 4); 
    Serial.print (digitalRead (b1));

    int userArray[sequenceLength];
    int z ;
    int Y;
    int n;

    for (int x = 0; x < sequenceLength; x ++) {
      digitalWrite(p_LEDs[ sequence[i] ], HIGH);
      delay(500);
      digitalWrite(p_LEDs[ sequence[i] ], LOW);
      delay(200);
     
    }

    while (z = 0, z < sequenceLength)  {
        if (digitalRead(b1) == LOW) {
          B_1 = 0;
          userArray[z] = B_1;
          z++;

        }

        if (digitalRead(b2) == LOW) {
          B2 = 1 ;
          userArray[z] = B2;
          z++;
        }


        if (digitalRead(b3) == LOW) {
          B3 = 2 ;
          userArray[z] = B3;
          z++;
        }

        if (digitalRead(b4) == LOW) {
          B4 = 3 ;
          userArray[z] = B4;
          z++;
        }
     

    if (z  == sequenceLength) {
      break;
    }
   }



    for (int n = 0; n < sequenceLength ; n++)
    {
      if (sequence[sequenceLength] == userArray[sequenceLength] )
      {
        Goed();
        sequenceLength ++;
      }
      else {
        sequenceLength = 1;
        Fout();

      }
    }
    delay(5000);
  }
}


void Goed() {

  digitalWrite (p_LEDs [0, 1, 2, 3], HIGH);
  delay (500);
  digitalWrite (p_LEDs [0, 1, 2, 3], LOW);
  delay (1000);
  Points++;
  lcd.begin(16, 2);
  lcd.home ();
  lcd.print("Correct!");
  lcd.setCursor ( 0, 1 );
  lcd.print(Points);
  lcd.print (" Punten");
  delay(1000);
  lcd.clear();
}

void Fout() {
  digitalWrite (p_LEDs [2], HIGH);
  delay (500);
  digitalWrite (p_LEDs [2] , LOW);
  delay (1000);
  lcd.begin(16, 2);
  lcd.home ();
  lcd.print("Game Over!");
  delay(1000);
  lcd.clear();

}

while (z = 0, z < sequenceLength)What does the comma do?

Yea i edited it. Z = 0 shouldn't be in there. it's now above the setup.

TolpuddleSartre:
while (z = 0, z < sequenceLength)What does the comma do?

And have you removed the

  int z ;

from inside the loop function? If not that creates a new variable also called z but with some random value in it, and the global variable z dosn't get a look in.

Also you do need to zero the variable z before every while loop, otherwise it is left at the value from the previous time round the loop.

Grumpy_Mike:
And have you removed the

  int z ;

from inside the loop function? If not that creates a new variable also called z but with some random value in it, and the global variable z dosn't get a look in.

Also you do need to zero the variable z before every while loop, otherwise it is left at the value from the previous time round the loop.

const int b1 = 9;
const int b2 = 10;
const int b3 = 11;
const int b4 = 12;
const int p_LEDs[] = {2, 3, 4, 5};
long OnTime = 50;
long OffTime = 150;
const int maxSequenceLength = 10;
int sequenceLength = 1;
int sequence[maxSequenceLength];
int z = 0 ;
int Points = 0;
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);

void setup() {
Serial.begin(9600);


for (int i = 0; i < 4; i++) {
    pinMode (p_LEDs [i], OUTPUT);
}
pinMode(b1,INPUT_PULLUP);
pinMode(b2,INPUT_PULLUP);
pinMode(b3,INPUT_PULLUP);
pinMode(b4,INPUT_PULLUP);


}



void loop () {
  
  int B_1 = 0;
  int B2 = 0;
  int B3 = 0;
  int B4 = 0;
  
  
  for (int i = 0; i < maxSequenceLength; i++) {
    sequence[i] = random(0, 4); 
    Serial.print (digitalRead (b1));

    int userArray[sequenceLength];
    for (int x = 0; x < sequenceLength; x ++) {
      digitalWrite(p_LEDs[ sequence[i] ], HIGH);
      delay(500);
      digitalWrite(p_LEDs[ sequence[i] ], LOW);
      delay(200);
     
    }

    while (z < sequenceLength)  {
        if (digitalRead(b1) == LOW) {
          B_1 = 3;
          userArray[z] = B_1;
          z++;

        }

        if (digitalRead(b2) == LOW) {
          B2 = 2 ;
          userArray[z] = B2;
          z++;
        }


        if (digitalRead(b3) == LOW) {
          B3 = 1 ;
          userArray[z] = B3;
          z++;
        }

        if (digitalRead(b4) == LOW) {
          B4 = 0 ;
          userArray[z] = B4;
          z++;
        }
     

    if (z  == sequenceLength) {
      break;
      z = 0;
    }
   }
  



    for (int n = 0; n < sequenceLength ; n++)
    {  Serial.print (userArray[sequenceLength]);
      if (sequence[sequenceLength] == userArray[sequenceLength] )
      {
        Goed();
        sequenceLength ++;
        
      }
      else {
        sequenceLength = 1;
        Fout();
       

      }
    }
    delay(5000);
  }
}


void Goed() {

  digitalWrite (p_LEDs [0, 1, 2, 3], HIGH);
  delay (500);
  digitalWrite (p_LEDs [0, 1, 2, 3], LOW);
  delay (1000);
  Points++;
  lcd.begin(16, 2);
  lcd.home ();
  lcd.print("Correct!");
  lcd.setCursor ( 0, 1 );
  lcd.print(Points);
  lcd.print (" Punten");
  delay(1000);
  lcd.clear();
}

void Fout() {
  digitalWrite (p_LEDs [2], HIGH);
  delay (500);
  digitalWrite (p_LEDs [2] , LOW);
  delay (1000);
  lcd.begin(16, 2);
  lcd.home ();
  lcd.print("Game Over!");
  delay(1000);
  lcd.clear();

}