Interesting problem(2 functions at same time)

HI!

I am working on a project - garage door keypad lock… I managed to create a password to give offer to change it but… I have an arduino uno, 4x4 keypad matrix a relay, lcd display and a limit switch. The problem is that the limit switch needs to function as a sensor. When a garage door goes up it presses on the limit switch and then the 10 min count down starts after 10 mins it automaticaly closes garage door through a relay… The thing is that i have a waitForKey function on keypad and it blocks the loop until it gets a key, if I use getKey function than it rapidly goes through loop and the lcd is just flashing. To sum up. I want the limit switch to be scanned the whole time but I want to be able to enter the code in also. Oh and i forgot to mention we use a remote to open a garage when we arrive by car so thats why limit switch needs to constantly be scanned - if i open garage remotely i do not enter a password into keypad, therefore arduino is still waiting for key. Does anybody have any suggestions?

Thanks in Advance!

Here is my code(The last thing I tried was millis(), but since waitForKey stops the loop it is useless)

#include <Wire.h> 
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>

char Pin1, Pin2, Pin3, Pin4;
char vPin1, vPin2, vPin3, vPin4;
char Potrditev = 10;
char vPotrditev;
char Menjava;

int stikalo = 5;
int stanje = 0;
int prejsnjeStanje = 0;
int pot = 1;

long milis;


const byte ROWS = 4; //four rows
const byte COLS = 4; //three columns
char keys[ROWS][COLS] = {
  {'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] = {10, 11, 12, 13}; //connect to the column pinouts of the keypad

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

LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);

void kStikalo(){
  stanje = digitalRead(5);
  if(stanje == HIGH){
  digitalWrite(4, HIGH);
  delay(1000);
  digitalWrite(4,LOW);
  }
  else{
  kStikalo();
  }
}

void kodaSprejeta(){
 lcd.clear();
 delay(750);
 lcd.print(" PIN JE SPREJET ");
 lcd.setCursor(0,2);
 lcd.print("----->****<-----");
 delay(750);
 lcd.clear();
 delay(750);
 lcd.print(" PIN JE SPREJET ");
 lcd.setCursor(0,2);
 lcd.print("----->****<-----");
 delay(750); 
 lcd.clear();
 delay(750);
 lcd.print(" PIN JE SPREJET ");
 lcd.setCursor(0,2);
 lcd.print("----->****<-----");
 delay(750); 
}

void pinZavrnjen(){
 lcd.clear();
 delay(750);
 lcd.print("  PIN ZAVRNJEN  ");
 lcd.setCursor(0,2);
 lcd.print("----->****<-----");
 delay(750);
 lcd.clear();
 delay(750);
 lcd.print("  PIN ZAVRNJEN  ");
 lcd.setCursor(0,2);
 lcd.print("----->****<-----");
 delay(750); 
 lcd.clear();
 delay(750);
 lcd.print("  PIN ZAVRNJEN  ");
 lcd.setCursor(0,2);
 lcd.print("----->****<-----");
 delay(750); 
}

void setup()   
{
  Serial.begin(9600);
  pinMode(5, INPUT);
  pinMode(4, OUTPUT);
  Potrditev = 35;
  Menjava = 42;
  lcd.begin(16,2);
  lcd.clear();  
  lcd.print(" VNESI NOVI PIN ");
  lcd.setCursor(0,2);
  lcd.print("----->    <-----");
  Pin1 = keypad.waitForKey();
  lcd.setCursor(0,2);  
  lcd.print("----->*   <-----");
  Pin2 = keypad.waitForKey();
  lcd.setCursor(0,2);  
  lcd.print("----->**  <-----");
  Pin3 = keypad.waitForKey();
  lcd.setCursor(0,2);  
  lcd.print("----->*** <-----");
  Pin4 = keypad.waitForKey();
  lcd.setCursor(0,2);  
  lcd.print("----->****<-----");
  vPotrditev = keypad.waitForKey();
  Serial.print(vPotrditev);
  if(vPotrditev == Potrditev){
   kodaSprejeta(); 
  }else{
    setup();
  }
}
void loop(){
  milis = millis();
  if(pot < milis){
   kStikalo(); 
  }
  
  lcd.clear();
  delay(750);
  lcd.print(" VNESI KODO PIN ");
  lcd.setCursor(0,2);
  lcd.print("----->    <-----");
  vPin1 = keypad.waitForKey();
  lcd.setCursor(0,2);
  lcd.print("----->*   <-----");
  vPin2 = keypad.waitForKey();
  lcd.setCursor(0,2);
  lcd.print("----->**  <-----");
  vPin3 = keypad.waitForKey();
  lcd.setCursor(0,2);
  lcd.print("----->*** <-----");
  vPin4 = keypad.waitForKey();
  lcd.setCursor(0,2);
  lcd.print("----->****<-----");
  vPotrditev = keypad.waitForKey();
  if(vPin1 == Pin1 && vPin2 == Pin2 && vPin3 == Pin3 && vPin4 == Pin4 && vPotrditev == Potrditev){
   lcd.clear();
   lcd.print(" PIN JE SPREJET ");
   lcd.setCursor(0,2);
   lcd.print("----->****<-----");
   digitalWrite(4, HIGH);
   delay(1000);
   digitalWrite(4, LOW);
   delay(17500);
  }
  else if(vPin1 == Pin1 && vPin2 == Pin2 && vPin3 == Pin3 && vPin4 == Pin4 && vPotrditev == Menjava){
   setup(); 
  }
  else{
   pinZavrnjen();
 }

  
}
  
Oh and sorry that some text parts are in my language but you should understand the commands

Kljucavnica_za_vrata.ino (3.42 KB)

You must scrap your current code as it uses delays and then read this

Mark

Thank you for suggestion I will definitely look into it!