Helping combine keypad code into Void loop

Greeting tinkers, i need some help from professional to combine my keypad 3x4 membrane password lock coding into my new void loop function.

This is my keypad password coding:

#define Password_Length 8 


char Data[Password_Length]; 
char Master[Password_Length] = "1234567"; 
byte data_count = 0, master_count = 0;
bool Pass_is_good;
char customKey;


const byte ROWS = 4; 
const byte COLS = 3; 

char hexaKeys[ROWS][COLS] = {
  {'1', '2', '3'},
  {'4', '5', '6'},
  {'7', '8', '9'},
  {'*', '0', '#'}
};

byte rowPins[ROWS] = {30, 31, 32, 33}; 
byte colPins[COLS] = {34, 35, 36}; 

Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS); 

void setup(){
  lcd.begin(16,2);
  lcd.backlight();
}

void loop(){

  lcd.setCursor(0,0);
  lcd.print("Enter Password:");
  customKey = customKeypad.getKey();
  if (customKey){
    Data[data_count] = customKey; 
    lcd.setCursor(data_count,1); 
    lcd.print(Data[data_count]); 
    data_count++; 
    }

  if(data_count == Password_Length-1){
    lcd.clear();

    if(!strcmp(Data, Master)){
      lcd.print("Correct"); 
      delay(5000);
      }
    else{
      lcd.print("Incorrect");
      delay(1000);
      }
    
    lcd.clear();
    clearData();  
  }
}

void clearData(){
  while(data_count !=0){
    Data[data_count--] = 0; 
  }
  return;
}

Below is my new void loop that need to combine with my keypad password coding:

void loop() 
{
 if(digitalRead(up)==0 || digitalRead(down)==0)
 {
  for(int i=0;i<5;i++)
    {
    lcd.clear();
    lcd.print("Place Finger");
    delay(2000);
    int result=getFingerprintIDez();
    if(result>=0)
       {
        digitalWrite(openLight, HIGH);
        digitalWrite(closeLight, LOW);
        lcd.clear();
        lcd.print("Verification no.");
        lcd.setCursor(0,1);
        lcd.print("has been send");
        delay(3000);
        lcd.clear();
        lcd.print("Please check");
        lcd.setCursor(0,1);
        lcd.print("your mobile");
        delay(3000);
        open_close_motor();
        delay(1000);
       }
 checkKeys();
 delay(2000);
  }
 }
}

Any help would be appreciate :slight_smile:

Why not try to combine the two first and then let's see what the problems are. Or would you rather somebody just did it for you?

Those long delays in your code will prevent the keypad from being responsive. You have to re-write the code to NOT use delay.

Can we please have some clues as to what the combined program should do ?

johnwasser:
Those long delays in your code will prevent the keypad from being responsive. You have to re-write the code to NOT use delay.

Yes very true indeed sir, when im trying to combine & compile my keypad code with the void loop, the keypad is not responsive. im clueless

johnwasser:
Those long delays in your code will prevent the keypad from being responsive. You have to re-write the code to NOT use delay.

can you highlight which one long delays in my code that prevent the keypad from being responsive ?

munihkim:
can you highlight which one long delays in my code that prevent the keypad from being responsive ?

Any delay() will cause the keypad to be unresponsive

munihkim:
can you highlight which one long delays in my code that prevent the keypad from being responsive ?

If your loop() takes longer than about 30 milliseconds to complete your keypad will seem sluggish. That means any one delay over 30 milliseconds or any combination of delays (including in loops) that total to more than 30 milliseconds.

johnwasser:
If your loop() takes longer than about 30 milliseconds to complete your keypad will seem sluggish. That means any one delay over 30 milliseconds or any combination of delays (including in loops) that total to more than 30 milliseconds.

Thank you very much yr tips :). i will try to re-write the code in the future

One good way to get rid of delay() is to use a State Machine model.

In a recent question someone wanted to count up or count down at 500 millisecond intervals while still reacting to two buttons. This is the State Machine I wrote to do that:

const byte Button2Pin = 12;
const byte Button1Pin = 2;

const unsigned long Wait = 500;
const unsigned long DebounceTime = 30;

unsigned long DebounceStartTime = 0;
unsigned long TimerStartTime = 0;

int Count = 0; // current display count

bool Last_State1 = false;
bool Last_State2 = false;

void setup()
{
  pinMode(Button1Pin, INPUT);
  pinMode(Button2Pin, INPUT);

  DisplayCount();
}

enum States {Stopped, CountingUp, CountingDown} State = Stopped;

void loop()
{
  unsigned long currentTime = millis();

  boolean button1State = digitalRead(Button1Pin);
  boolean button2State = digitalRead(Button2Pin);

  switch (State)
  {
    case Stopped:

      // If button1 is pressed, count up
      if (button1State != Last_State1 && currentTime - DebounceStartTime > DebounceTime)
      {
        DebounceStartTime = currentTime;
        if (button1State)  // Just pressed, start counting
        {
          Count = 0;
          DisplayCount();
          State = CountingUp;
          TimerStartTime = currentTime;
        }
      }

      // If button2 is pressed, count down
      if (Count > 0 && button2State != Last_State2 && currentTime - DebounceStartTime > DebounceTime)
      {
        DebounceStartTime = currentTime;
        if (button2State)  // Just pressed button2, Count down
        {
          State = CountingDown;
          TimerStartTime = currentTime;
        }
      }

      break;


    case CountingUp:
      if (currentTime - TimerStartTime >= Wait)
      {
        Count = (Count + 1) % 10;  // Modulo 10: 0,1,2,3,4,5,6,7,8,9,0,1,2,3...
        DisplayCount();
        TimerStartTime += Wait;
      }

      // If button1 is pressed, Stop
      if (button1State != Last_State1 && currentTime - DebounceStartTime > DebounceTime)
      {
        DebounceStartTime = currentTime;
        if (button1State)  // Just pressed button1, Stop counting
        {
          State = Stopped;
          TimerStartTime = currentTime;
        }
      }

      // If button2 is pressed, CountDown
      if (button2State != Last_State2 && currentTime - DebounceStartTime > DebounceTime)
      {
        DebounceStartTime = currentTime;
        if (button2State)  // Just pressed button2, Count down
        {
          State = CountingDown;
          TimerStartTime = currentTime;
        }
      }

      break;


    case CountingDown:
      if (Count > 0 && currentTime - TimerStartTime >= Wait)
      {
        Count--;
        DisplayCount();
        TimerStartTime += Wait;
      }

      // Stop when you reach zero
      if (Count == 0)
      {
        State = Stopped;
      }

      break;
  }
}

void DisplayCount()
{
}