Problem clearing LCD using mills()

hello, i am having trouble switching between what i want printed on my lcd. i am doing a project that includes an alarm. When the alarm is activated, i want it to display “password Invalid…” every time a wrong password is entered for 2 seconds , and go right back to displaying “alarm”. But my problem is it prints “password invalid” and it doesn’t clear or go back to displaying “alarm”.Having difficult understanding the mills function. Any ideas on how to fix it?

unsigned long lcdinterval = 2000;
unsigned long LcdMillis = 0;
unsigned long currentMillis2=0;
void loop() {

keypad.getKey();
switch (var) {
        case 0:
          lcd.setCursor(0, 0);
          lcd.print(" *** ALARM *** ");
          
          break;

        case 1:
          currentMillis2=millis();
          lcd.setCursor(0, 0);
          lcd.print("PASSWORD INVALID");
          lcd.setCursor(0, 1);
          lcd.print("  TRY AGAIN !");
          if (currentMillis2 - LcdMillis >= lcdinterval) {
           currentMillis2 = LcdMillis;
          lcd.clear(); 
          var = 0;
          }
          

          break;

        default:
          break;

}
}

currentMillis2=millis();
. . .
currentMillis2 = LcdMillis; ?

Always show us your current compete sketch. Use CTRL T to format the sketch. Please use code tags. Use the </> icon in the posting menu.

[code] Paste sketch here. [/code]

.

sorry its kinda of long, just wanted to show the part of the problem.

#include <Password.h>
#include <LiquidCrystal.h>
#include <Keypad.h>

const int RED = 42; //RED LED
const int intervalMinor = 100;  // number of millisecs that LED is off between flashes
const int intervalMajor = 200; // number of millisecs that LED is off between cycles
const int blinkDuration = 80;  // number of millisecs that LED is on
const int flashesInCycle = 3;  // number of flashes inside a cycle
const int GREEN = 40; //GREEN LED
const int Buzz = 10; // Buzzer
const int YELLOW = 44; // YELLOW LED to test PIR Sensor
const int PIR = 50; // PIR Sensor
const int relay = 37;
int horizPos = 5;
boolean activateMotion = false;
boolean MalarmActivated = false;
boolean activated = false; //variable to activate
boolean alarm = false;
int var = 0;
int countdown = 9; // 9 second count down before activating the alarm

byte targetLedState = HIGH; // this variable is necesary to help arduino monitor the status of LED
//the initial condition (t=0) can be either HIGH or LOW
unsigned long currentMillis = 0;    // stores the value of millis() in each iteration of loop()
unsigned long previousLedMillis = 0;   // will store last time the LED was updated
int flashCount = 0;

unsigned long lcdinterval = 2000;
unsigned long LcdMillis = 0;
unsigned long currentMillis2 = 0;



LiquidCrystal lcd(22, 24, 26, 28, 30, 32); // initialize the library with the numbers of the interface pins

Password password = Password("1234"); //PASSWORD TO ACTIVATE MOTION ALARM
Password password2 = Password("A"); //PASSWORD TO DEACTIVATE MOTION ALARM

const byte ROWS = 4; // Four rows
const byte COLS = 4; // Four columns

char keys[ROWS][COLS] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};

// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte rowPins[ROWS] = { 5, 4, 3, 2}; //connect to the row pinouts of the keypad
byte colPins[COLS] = { 9, 8, 7, 6}; //connect to the column pinouts of the keypad


// Create the Keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS);

void setup() {
  Serial.begin(9600);
  lcd.begin(16, 2);
  keypad.addEventListener(keypadEvent);
  pinMode(GREEN, OUTPUT); // Green Light
  pinMode(RED, OUTPUT); // Red Light
  pinMode (YELLOW, OUTPUT); // Yellow light to test PIR
  pinMode(Buzz, OUTPUT); // Piezo buzzer
  pinMode(PIR, INPUT); // PIR sensor
  pinMode(relay, OUTPUT);

  ShowEnterPass();


}

void ShowEnterPass() {
  lcd.clear();
  lcd.setCursor(1, 0);
  lcd.print("    PLEASE ");
  lcd.setCursor(0, 1);
  lcd.print(" ENTER PASSWORD ");
  delay(2000);
  lcd.clear();


}


void loop() {

  keypad.getKey();

  if (activated == true) {

    activateMotion = true;
  }

  if (activateMotion == true) {

    lcd.setCursor(0, 0);
    lcd.print("  MOTION ALARM");
    lcd.setCursor(0, 1);
    lcd.print("ACTIVATING IN");

    int countdown = 9; // 9 second count down before activating the alarm

    while (countdown != 0) {
      lcd.setCursor(14, 1);
      lcd.print(countdown);

      countdown--;
      tone(Buzz, 700, 100);
      delay(1000);


    }


    lcd.clear();
    lcd.setCursor(0, 0);
    digitalWrite(RED, HIGH);
    lcd.print("  MOTION ALARM");
    lcd.setCursor(0, 1);
    lcd.print("   ACTIVATED");
    tone(Buzz, 1110, 700);
    delay(2500);
    digitalWrite(RED, LOW);
    lcd.clear();
    activateMotion = false;
    MalarmActivated = true;
    activated = false;


  }

  if (MalarmActivated == true) {

    if (digitalRead(PIR) == HIGH) {


      MotionAlarmTriggered();

      Serial.println(currentMillis2 / 1000);
      switch (var) {
        case 0:
          lcd.setCursor(0, 0);
          lcd.print(" *** ALARM *** ");
          if (digitalRead(PIR) == LOW) {
            lcd.clear();
          }
          break;

        case 1:
          currentMillis2 = millis();
          lcd.setCursor(0, 0);
          lcd.print("PASSWORD INVALID");
          lcd.setCursor(0, 1);
          lcd.print("  TRY AGAIN !");
          if (currentMillis2 - LcdMillis >= lcdinterval) {
            currentMillis2 = LcdMillis;
            lcd.clear();
            var = 0;
          }


          break;

        default:
          break;
      }


    } else {
      Serial.println("MOTION STOPPED");
      digitalWrite(relay, LOW); digitalWrite(RED, LOW);


    }
  }

} //void loop ends



void MotionAlarmTriggered() {

  currentMillis = millis();   // capture the latest value of millis()
  alarm = true;
  digitalWrite(relay, HIGH);
  playtone();

  int offInterval ;

  if (targetLedState == LOW) { //if the LED is OFF
    Serial.println("LOW");
    offInterval = (flashCount < flashesInCycle) ? intervalMinor : intervalMajor;  //choose interval value
    if ((unsigned long)currentMillis - previousLedMillis >= offInterval) {
      targetLedState = HIGH;
      digitalWrite(RED, targetLedState);
      previousLedMillis += offInterval;
      flashCount = (flashCount < flashesInCycle) ? (flashCount += 1) : 0; //update the counter

    }
  }
  else {  // i.e. if onBoardLedState is HIGH (if the LED is ON)
    Serial.println("HIGH");
    if ((unsigned long)currentMillis - previousLedMillis >= blinkDuration) {
      targetLedState = LOW;
      digitalWrite(RED, targetLedState);
      previousLedMillis += blinkDuration;


    }
  }




}


void playtone() {
  tone(Buzz, 200, 160);

}





void keypadEvent(KeypadEvent eKey) {

  lcd.setCursor(0, 1);
  lcd.print("PASS:");


  switch (keypad.getState()) {
    case PRESSED:

      lcd.setCursor(horizPos, 1);
      horizPos = horizPos + 1;
      lcd.print('*');


      switch (eKey) {
        case '*':
          checkPassword();   //* is to validate password
          horizPos = (horizPos * 0) + 5;


        case '#':

          horizPos = (horizPos * 0) + 5;
          password.reset();
          password2.reset();
          lcd.clear();
          break; //# is to reset password attempt

        default:
          password.append(eKey);
          password2.append(eKey);
          if (keypad.getState() == PRESSED) {
            tone(Buzz, 1000, 20);
          }
      }
  }
}



void checkPassword() {

  keypad.getKey();
  if (password.evaluate()) {
    password.reset();
    activated = true;

  }

  else if (password2.evaluate()) {
    Serial.println("MOTION ALARM DEACTIVATED");
    var = 0;
    lcd.clear();
    alarm = false;
    password2.reset();
    activated = false;
    MalarmActivated = false;
    digitalWrite(relay, LOW);
    digitalWrite(RED, LOW);
    digitalWrite(GREEN, HIGH);

    tone(Buzz, 1000, 1000);
    delay(100);
    tone(Buzz, 500, 100);
    lcd.setCursor(0, 0);
    lcd.print("  MOTION ALARM");
    lcd.setCursor(0, 1);
    lcd.print("  DEACTIVATED");
    delay(1500);
    lcd.clear();
    digitalWrite(GREEN, LOW);

  }

  else { //if password is wrong

    var = 1;

    if (alarm == true) {

    } else {
      tone(Buzz, 1600, 1500);
      password.reset(); //resets password after incorrect entry
      password2.reset();
      digitalWrite(RED, HIGH); //RED Light will turn on
      lcd.setCursor(0, 0);
      lcd.print("PASSWORD INVALID");
      lcd.setCursor(0, 1);
      lcd.print("  TRY AGAIN !");
      delay(2500);
      digitalWrite(RED, LOW); //RED light will turn off
      lcd.clear();

    }

  }
}

Why this line?
currentMillis2 = LcdMillis;

.

keypad.getKey();

Why are you reading from the keypad if you don't care what, if any, key is being pressed?

The names of the variables that hold times do NOT need to contain millis. In fact, I think that the fact that so many examples use millis in the name is part of why people have trouble using millis() and the blink without delay technique.

What if you named the variable that holds now now, instead of currentMillis2? What if you named the variable that holds the time that the LCD was last cleared lastCleared instead of LcdMillis?

Then, could you see that

   now = lastCleared;

was wrong?

   lastCleared = now;
unsigned long lcdinterval = 2000;
unsigned long lastCleared = 0;
unsigned long now = 0;
 switch (var) {

        case 0:
          lcd.setCursor(0, 0);
          lcd.print(" *** ALARM *** ");
          if (digitalRead(PIR) == LOW) {
            lcd.clear();
          }
          break;

        case 1:
          now = millis();
          Serial.println(now/ 1000);
          if (now - lastCleared >= lcdinterval) {
            lcd.setCursor(0, 0);
            lcd.print("PASSWORD INVALID");
            lcd.setCursor(0, 1);
            lcd.print("  TRY AGAIN !");
            

         }

          else {
            lcd.clear(); var = 0;
          }


          break;

        default:
          break;
      }

Thanks for the response but still not getting it. Every time case 1 occurs it prints and holds "Password invalid". Obviously because its greater than the lcd interval. The else in my code is pointless because the time will continuously increment.So how would i code it to display for 2 seconds, clear, and switch to case 0
Do i have to add another unsigned long variable?
Also many examples have lastCleared=now; (in terms of my example) what is the point of this?

          if (digitalRead(PIR) == LOW) {
            lcd.clear();
          }

You cleared the LCD but you FAILed to record the time.

          else {
            lcd.clear(); var = 0;
          }

See above.

Thank you sir i got it to work but there is still a small problem. It only displays case 1 and goes back to case 0 on the 2nd, 3rd etc attempt of a wrong password. it doesn’t display on the first wrong password. How can i fix this?

 switch (var) {

        case 0:
          lcd.setCursor(0, 0);
          lcd.print(" *** ALARM *** ");

          break;

        case 1:
          now = millis();
          Serial.println(now / 1000);
          lcd.setCursor(0, 0);
          lcd.print("PASSWORD INVALID");
          lcd.setCursor(0, 1);
          lcd.print("  TRY AGAIN !");
          if (now - lastCleared >= lcdinterval) {
            lastCleared = now;
            lcd.clear();
            var = 0;

          }



          break;

        default:
          break;
      }

How can i fix this?

You need to ask the fine folks at http://snippets-r-us.com that question.