Am l doing something wrong?

So, i do not know what is wrong because when i try to write the correct number, but it is not working, it is always wrong. Any help?

  1. The user pushes the input button to enter the first digit 1 click = 1, 2 clicks = 2, etc. Each the input button is pushed the green led flashes once. When the user is done with the digit, they click “confirm.”, the red light flashes once and next input is ready. repeat step 2 until 4 digits have been entered.

So, i do not know what is wrong because when i try to write the correct number, but it is not working, it is always wrong. Any help?

enum {ST_LOCKED, ST_LOCKED_INPUTTING, ST_UNLOCKED} currentState = ST_LOCKED;

int previousMillis;
int Interval = 500;
int LedState = LOW;
int LedPin = 13;


int messageDisplayedOnce = false;


int redLed = 4;
int greenLed = 5;
int inputButton = 9;
int confirmButton = 8;


int inputButtonState;
int lastInputButtonState;
int confirmButtonState;
int lastConfirmButtonState;


int correctDigits[] = {4, 3, 2, 1};
int enteredDigits[] = {0, 0, 0, 0};
int digitIndex = 1;
int entryIsCorrectSoFar = true;


unsigned long redAndGreenBothGoOn;
unsigned long greenBlipAt;
bool greenBlip = false;
unsigned long redBlipAt;
bool redBlip = false;
unsigned long redFlashAt;
bool redFlash = false;


void setup()
{

  Serial.begin(9600);
  Serial.println("....4 digit vault code...");

  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(redLed, OUTPUT);
  pinMode(greenLed, OUTPUT);
  pinMode(inputButton, INPUT_PULLUP);
  pinMode(confirmButton, INPUT_PULLUP);

  inputButtonState = digitalRead(inputButton);
  lastInputButtonState = inputButtonState;
  confirmButtonState = digitalRead(confirmButton);
  lastConfirmButtonState = confirmButtonState;
  Serial.print(inputButtonState);
  Serial.print(lastInputButtonState);
  Serial.print(confirmButtonState);
  Serial.println(lastConfirmButtonState);


}

void loop()
{
  Life();
  States();
}


void Life()
{
  if (millis() - previousMillis >= Interval)
  {
    LedState = !LedState;
    digitalWrite(LedPin, LedState);
    previousMillis = millis();;

  }
}


void States()

{

  switch (currentState)

  {

    case ST_LOCKED:

      if (!messageDisplayedOnce)

      {

        Serial.println("\n   *** The vault is locked ***");
        Serial.println("Press Input to start entering the code...");
        messageDisplayedOnce = true;
        digitalWrite(greenLed, LOW);
        if (!redFlash)  {
          digitalWrite(redLed, LOW);
        }
      }


      if (millis() - redFlashAt >= 500 && redFlash)
      {

        digitalWrite(redLed, LOW);
        redFlash = false;
      }


      if (checkInputNewPress())


      {
        currentState = ST_LOCKED_INPUTTING;
        messageDisplayedOnce = false;
        redAndGreenBothGoOn = millis();
        digitalWrite(redLed, HIGH);
        digitalWrite(greenLed, HIGH);
      }

      break;

    case ST_LOCKED_INPUTTING:
      if (!messageDisplayedOnce)
      {

        Serial.print("Click Input to increment digit ");
        Serial.print(digitIndex);
        Serial.print(", whose current value is ");
        Serial.print(enteredDigits[digitIndex]);
        Serial.println(". Click the CONFIRM button when you done");
        messageDisplayedOnce = true;
      }

      if (millis() - redAndGreenBothGoOn >= 2000)
      {
        digitalWrite(redLed, LOW);
        digitalWrite(greenLed, LOW);
      }

      if (checkInputNewPress())
      {

        messageDisplayedOnce = false;
        enteredDigits[digitIndex]++;
        if (enteredDigits[digitIndex] > 9) enteredDigits[digitIndex] = 4; //1
        greenBlipAt = millis();
        greenBlip = true;
        digitalWrite(greenLed, HIGH);
      }

      if (millis() - greenBlipAt >= 100 && greenBlip)
      {
        digitalWrite(greenLed, LOW);
        greenBlip = false;
      }

      if (checkConfirmNewPress())
      {
        entryIsCorrectSoFar = true;
        for (int i = 0; i < 4; i++) {
          if (enteredDigits[i] != correctDigits[i])
          {
            entryIsCorrectSoFar = false;
          }
        }
        digitIndex++;
        messageDisplayedOnce = false;
        redBlipAt = millis();
        redBlip = true;
        digitalWrite(redLed, HIGH);
      }

      if (millis() - redBlipAt >= 100 && redBlip)
      {
        digitalWrite(redLed, LOW);
        redBlip = false;
      }


      if (digitIndex == 4)
      {
        digitIndex = 0;
        for (int i = 0; i < 4; i++) enteredDigits[i] = 1;
        if (entryIsCorrectSoFar == true) currentState = ST_UNLOCKED;
        else
        {
          currentState = ST_LOCKED;
          redBlip = false;
          redFlashAt = millis();
          redFlash = true;
          digitalWrite(redLed, HIGH);
          Serial.println("You made at least one mistake, sorry");
          entryIsCorrectSoFar = true;
        }
      }

      break;

    case ST_UNLOCKED:
      if (!messageDisplayedOnce)
      {
        Serial.println("\n    *** The Vault is unlocked ***");
        Serial.println("Press the CONFIRM button to lock the vault");

        messageDisplayedOnce = true;
        digitalWrite(greenLed, HIGH);
      }

      if (millis() - redBlipAt >= 100 && redBlip)
      {
        digitalWrite(redLed, LOW);
        redBlip = false;
      }

      if (checkConfirmNewPress())
      {
        Serial.println("Locking the vault");
        currentState = ST_LOCKED;
        messageDisplayedOnce = false;

      }

      break;

  }
}

bool checkInputNewPress()
{
  bool result = false;

  inputButtonState = digitalRead(inputButton);

  if (inputButtonState != lastInputButtonState)
  {
    if (inputButtonState == LOW)
    {


      result = true;
    }

    delay(50);
  }
  else //no change
  {
    result = false;
  }

  lastInputButtonState = inputButtonState;
  return result;

}

bool checkConfirmNewPress()
{
  bool result = false;

  confirmButtonState = digitalRead(confirmButton);


  if (confirmButtonState != lastConfirmButtonState)
  {
    if (confirmButtonState == LOW)
    {

      result = true;
    }

    delay(50);
  }
  else
  {
    result = false;
  }

  lastConfirmButtonState = confirmButtonState;
  return result;

}

What do you mean by "not working"? How is it behaving vs. how you want it to behave. What is the serial output?

A couple of comments.

'previousMillis' should be of type unsigned long
digitIndex should start at 0, not 1

all your constants should use the word 'const' before the type (e.g. const int...)

You are doing way too many things inside each of your states, why not break them down into smaller steps? Something like

enum {ST_LOCKED_START, ST_LOCKED, ST_UNLOCKING_START, ST_UNLOCKING, ST_UNLOCKED} currentState = ST_LOCKED_START;

and then you can display whatever message you want and transition to the next state. It will avoid all the disiplayOnce nonsense.

I'd go as far as having one state per digit:

enum States {LOCKED, DIGIT0, DIGIT1, DIGIT2, DIGIT3, UNLOCKED} CurrentState = LOCKED;
...
    switch (CurrentState)
    {
    case DIGIT0:
        if (checkInputNewPress())
           {
           CurrentDigit = (CurrentDigit+1) % 10; // 0, 1, 2,...9, 0, 1,...
           Serial.print("Current first digit is: ");
           Serial.println(CurrentDigit);
           }
        if (checkConfirmNewPress())
        {
            EnteredDigits[0] = CurrentDigit;
            CurrentDigit = 0;
            CurrentState = DIGIT1;
        }
        break;

...
    case DIGIT3:
        if (checkInputNewPress())
           {
           CurrentDigit = (CurrentDigit+1) % 10; // 0, 1, 2,...9, 0, 1,...
           Serial.print("Current fourth digit is: ");
           Serial.println(CurrentDigit);
           }
        if (checkConfirmNewPress())
        {
            EnteredDigits[3] = CurrentDigit;
            CurrentDigit = 0;
            if (EnteredDigits[0] == CorrectDigits[0] &&
                EnteredDigits[1] == CorrectDigits[1] &&
                EnteredDigits[2] == CorrectDigits[2] &&
                EnteredDigits[3] == CorrectDigits[3])
             {
             Serial.println("Successful unlock.");
            CurrentState = UNLOCKED;
             Serial.println("Press CONFIRM again to lock.");
            }
        else
             {
             Serial.println("Incorrect combination.");
             CurrentState = LOCKED;
             Serial.println("Select first digit.");
              }
        }
        break;