Comparing arrays [PIN combination Vault]

:slight_smile: Hi there, I am working on a vault project with 4 different buttons on a Arduino Uno and I am using a resistor ladder for the buttons (shouldn't matter that much for this project).
I will explain my code in detail and in the meantime I will explain the problems I faced.

My code uses two arrays.
-PIN entered by the user
-Secret vault PIN combination

Button one has the value PIN number 1 assigned to it.
Button two has the value PIN number 2 assigned to it.
Button three has the value PIN number 3 assigned to it.
Button four has the value PIN number 4 assigned to it.

My code uses a button state change detection. If one of the four buttons is pressed, my button state will increase by one and if the button presses exceed 4 then my button state will reset to -1(this will make sense later). The code loops through the array of the user.

For example:
The 2th time I press a button, I press the first button which has the value PIN number 1 assigned to it. The button state is used as an index of an array. So, -1 + 1 + 1 = 1. This is the 2th spot in an array because we start to count from 0. So the 2th spot in the array will be changed to 1.

Remember my button state being -1. This is the first problem I faced. If I set the value of the button state to 0 then my code will skip the first index of my array. While the code may work, it will still be confusing to read for someone else.

After pressing 4 times my code will compare the 2 arrays. If the entered PIN is incorrect then my red LED will blink and the PIN combination will reset. My PIN combination will also reset if I press 5 times.
If it is correct then my green LED will blink.

I've done some research and I tried to loop through both of my arrays and compare them simultaneously but my serial monitor always tells me that the entered PIN is not the same as the secret PIN combination even though it should be correct. So, if the PIN code is 1,1,1,1 and I enter 1,1,1,1. It still tells me that my PIN is incorrect. :confused:

I've included an image of the serial monitor if needed. I appreciate your aid.

const int redLED = 13;
const int greenLED = 12;
int LED;
int count;
int buttonState = -1;
int buttonValue;
bool buttonUnpressed = true;

void setup() {
  Serial.begin(9600);
  Serial.println("Welcome, please type in the digits of your pin.");
  pinMode(redLED, OUTPUT);
  pinMode(redLED, OUTPUT);
  pinMode(greenLED, OUTPUT);
  digitalWrite(redLED, HIGH);
  digitalWrite(greenLED, LOW);
}
void buttonStateDetect() {
  buttonState++;
  if (buttonValue > 0) {
    buttonUnpressed = false;
  }
  delay(50);
  if (buttonValue == 0) {
    delay(50);
    buttonUnpressed = true;
  }
}

void blinkLED() {
  for (count = 0; count < 3; count++) {
    digitalWrite(LED, LOW);
    delay(900);
    digitalWrite(LED, HIGH);
    delay(900);
  }
  if (LED == greenLED) {
    digitalWrite(greenLED, HIGH);
  }
}

void loop() {
  int enteredCode[4] = {0, 0, 0, 0};
  int secretCode[4] = {4, 2, 2, 1};
  buttonValue = analogRead(A5);
  delay(300);

  if (buttonValue >= 7 && buttonValue <= 14) {
    buttonStateDetect();
    enteredCode[buttonState] = 1;
    Serial.print(enteredCode[buttonState]);
  }

  else if (buttonValue >= 511 && buttonValue <= 515) {
    buttonStateDetect();
    enteredCode[buttonState] = 2;
    Serial.print(enteredCode[buttonState]);
  }

  else if (buttonValue >= 1000 && buttonValue <= 1004) {
    buttonStateDetect();
    enteredCode[buttonState] = 3;
    Serial.print(enteredCode[buttonState]);
  }

  else if (buttonValue == 1023) {
    buttonStateDetect();
    enteredCode[buttonState] = 4;
    Serial.print(enteredCode[buttonState]);
  }

  if (buttonState == 3) {
    int rightAnswer = 0;
    int wrongAnswer;
    Serial.println(" Checking your pin.");
    for (count = -1; count < 5; count++) {
      if (enteredCode[count] == secretCode[count]) {
        rightAnswer++;
      }
      if (rightAnswer == 4) {
        LED = greenLED;
        blinkLED();
        Serial.println("The pin is correct.");
      }
      if (rightAnswer < 4 && count == 4) {
        LED = redLED;
        blinkLED();
        Serial.println("The pin is wrong, sorry try again!");
        Serial.println("Resetting");
        for (count = 0; count < 5; count++) {
          enteredCode[count] = 0;
        }
        buttonState = -1;
        delay(500);
      }
    }
  }

  else if (buttonState > 3) {
    Serial.println("Resetting");
    for (count = 0; count < 5; count++) {
      enteredCode[count] = 0;
    }
    buttonState = -1;
    delay(500);
  }
}

Serial Monitor Debug.PNG