Door requires password for entrance(Two incorrect attempts allowed before alarm)

I’m having trouble inputting the passwords for the door. When I input the password I always get the serial print of “Invalide code” no matter what attempt it is on or if the password is correct. When an incorrect password is input the first time it should serial print “Invalid code”. When an incorrect password is input a second time it is supposed to serial print “Trials exceeded”. When a correct password is input, there are three(700, 316, and 279) a servo is supposed to be activated. Any advice is greatly appreciated. Thanks.

#include <Keypad.h>
#include <Servo.h>

int sensorPin = A0;
int sensorValue = 0;
int speakerPin = 11;
int sensorValue2 = 0;

const byte ROWS = 4; //four rows

const byte COLS = 4; //four columns



char hexaKeys[ROWS][COLS] = {  //define the symbols on the buttons of the keypads

  {'1', '2', '3', 'A'},

  {'4', '5', '6', 'B'},

  {'7', '8', '9', 'C'},

  {'*', '0', '#', 'D'}

};



byte rowPins[ROWS] = {9, 8, 7, 6}; //connect to the row pinouts of the keypad

byte colPins[COLS] = {5, 4, 3, 2}; //connect to the column pinouts of the keypad



Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS); //initialize an instance of class NewKeypad

int value = 0;  // Will not go above 255 using byte identifier - use int for higher

char customKey = 0;


Servo myservo;
int pos = 0;
int numDigitsEntered = 0;
int numTrials = 0;

void setup() {
  Serial.begin(9600);
  myservo.attach(10);
  pinMode(11, OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  int sensorValue = analogRead(A0);

  if (sensorValue <= 20) { //photoeye sensor to check if someone is at the keypad
    Serial.print("Welcome Agent. Please enter your access code:");
    delay(50);

    while (numDigitsEntered <= 3) {

      customKey = customKeypad.getKey();   //check keypad for input



      if (customKey != NO_KEY)  //If any key is pressed

      {

        if ((customKey >= '0') && (customKey <= '9')) //Only valid for number keys

        {

          value = value * 10; //First time through is zero then multiply by ten for each input

          value = value + customKey - '0';  //CustomKey input is ASCII - zero is 48, one is 49, etc. Subtract ASCII zero to get to decimal zero

        }

        if (customKey == '#')

        {

          Serial.println(value);

          value = 0; //reset value once it is printed


          if (value == 700 || value == 316 || value == 279) { // open door if correct input code
            myservo.write(180);
            delay(15);
            //re-initialize values for a new entry
            numTrials = 0;
            numDigitsEntered = 0;
          } else if (numTrials = 1) { // incorrect code but still less than 2 trials
            numTrials = numTrials + 1;
            numDigitsEntered = 0;
            Serial.print("Invalid Code!");
          } else if (numTrials >= 2 ) { //incorrect code and exceeded the number of trials
            numDigitsEntered = 0;
            Serial.print("Trials Exceeded!");
            digitalWrite(11, HIGH);
            delay(2);   //Changes tone frequency by changing delay
            digitalWrite(11, LOW);
            delay(2);
          }
        }
      }
    }

  }
}

Untitled document.txt (2.6 KB)

Read the advicing topic "How to use this Forum", especially the "How to attach code". .txt file is not good.
We rather spend time think and replying than coppying, editing text.

Sorry, I should've figured that out before I posted.

Autoformat the code in the IDE, mark it, Ctrl c, click the tag up to the left in this window and then Ctrl v.

Question for you, @OP:

Does the code in your IDE really look like this:

.
.
.
  } else if (numTrials >=2 ){ //incorrect code and exceeded the number of trials
    numDigitsEntered = 0;
    Serial.print("Trials Exceeded!");
   digitalWrite(11, HIGH); 
    delay(2);   //Changes tone frequency by changing delay         
   digitalWrite(11, LOW);   
    delay(2);}
  }
    }
  }

      }
}

Specifically, the crazy indenting of the '{' and '}' braces?

Isn't something like:

.
.
.
                    else if (numTrials >=2 )
                    { 
                        //incorrect code and exceeded the number of trials
                        numDigitsEntered = 0;
                        Serial.print("Trials Exceeded!");
                        digitalWrite(11, HIGH); 
                        delay(2);   //Changes tone frequency by changing delay         
                        digitalWrite(11, LOW);   
                        delay(2);
                        
                    }//else
                    
                }//if
                
            }//if
            
        }//while
        
    }//if
    
}//loop

easier to follow, debug and easier on the eyes?

Just curious. I see this a lot and I don't understand how people stand to have their code like that or if it's that they use a different IDE or there's some weird spacing/tabbing thing that happens when copy-pasting from the IDE to the code-tags block.

Anyway, I suggest moving from a single in-line if/while setup to a state machine to handle the code entry and attempts-counting.

Right before you check value for one of the correct numbers, you set it to zero.