[SOLVED] Arduino keypad application for password

hello and good day.
Im kindda stuck with my application that Im trying to build over here using a keypad. I manage to use the keypad and record the keypress. However the required application is to record the keystroke and read individual keystroke. After that, I need to trigger a relay based on the keystroke that been pressed on the keypad.

// Include Arduino Wire library for I2C
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
 
#define Password_Length 9
char Data[Password_Length];
 
// Pin connected to lock relay input
const int lock= 13;
const int entry= A0;
const int emergency= A1;
const int indicator= A2;
const int id= A3;
 
// Counter for character entries
byte data_count = 0;
 
// Character to hold key input
char customKey;
 
// Constants for row and column sizes
const byte ROWS = 4;
const byte COLS = 4;
 
// Array to represent keys on keypad
char hexaKeys[ROWS][COLS] = {
  {'1', '2', '3'},
  {'4', '5', '6'},
  {'7', '8', '9'},
  {'0', '0', '0'}
};
 
// Connections to Arduino
byte rowPins[ROWS] = {2, 3, 4, 5};
byte colPins[COLS] = {6, 7, 8};
 
// Create keypad object
Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
 
// Create LCD object
LiquidCrystal_I2C lcd(0x20, 16, 2);
 
void setup() {
  // Setup LCD with backlight and initialize
  lcd.backlight();
  lcd.init();

  pinMode(lock, OUTPUT);
  pinMode(emergency, INPUT);
  pinMode(entry, INPUT);
  pinMode(indicator, INPUT);
  pinMode(id, INPUT);
  Serial.begin(9600);
}
 
void loop() {
 
  // Look for keypress
  customKey = customKeypad.getKey();
  if (customKey) 
  {
    // Enter keypress into array and increment counter
    Data[data_count] = customKey;
    lcd.setCursor(data_count, 1);
    lcd.print(Data[data_count]);
    data_count++;
    Serial.print(customKey);
  }
 
  // See if we have reached the password length
  if (digitalRead(emergency) == HIGH)
  {    
      lcd.setCursor(0, 0);
      lcd.print("Enter Password:");
      if (data_count == Password_Length - 1 && digitalRead(emergency) == HIGH) 
      {
        lcd.clear();

        Serial.println(data_count);

// need to read individual data_count and trigger lock output
// trigger lock output based after entry mode button is pressed
        
       
        //final judgement if number correct
        if (digitalRead(indicator) == HIGH && digitalRead(id) == HIGH) 
        {
          // Password is correct
          lcd.print("Correct");
          // Turn on relay for 5 seconds
          digitalWrite(lock, HIGH);
          delay(5000);
          digitalWrite(lock, LOW);
        }
        else {
          // Password is incorrect
          lcd.print("Incorrect");
          delay(1000);
        }

        // Clear data and LCD display
        lcd.clear();
        clearData();
      }
  }
  
  else 
  {
    lcd.setCursor(0,0);
    lcd.print("smart ind low  ");
  }
}
 
void clearData() {
  // Go through array and clear data
  while (data_count != 0) {
    Data[data_count--] = 0;
  }
  return;
}

below are the link to my application on tinkerkad
arduino keypad

thank you kind sirs.

What is the problem, the question?

what I`m trying to do is read from the stored password and trigger a relay based on the password pressed.

the other controller will only give 2 signal back if the password is correct, other wise it will return 1 signal if it is a false password.

What I`m trying to do is send the password based on the keypress at 5 second interval, for example if I press 12345678, the relay will trigger once for the first digit, delay 5 second and trigger twice for the second digit until the eighth digit.

how do I do that sir?

Can you copy the wiring diagram and post it here so that we do not have to go to an external site to see it?

1 Like

Implement the above function first assuming that the stored password is "1234". The L (Built-in LED of UNO) will remain ON for 1-sec when the entered password from Keypad matches with the stored password. The key strokes will appear on the InputBox of Serial Monitor as they are entered from Keypad. After that add the remaining requirements one-by-one with this foundation sketch.

Below is the sketch prepared based on the codes of post #1.

#include <Keypad.h>

#define Password_Length 4
char Data[Password_Length + 1] = {0}; //all locations contain 0
const int lock = 13;
byte data_count = 0;
char customKey;
char myPassword[] = "1234";  //pre-stored password

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

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

byte rowPins[ROWS] = {2, 3, 4, 5}; //R1R2R3R4
byte colPins[COLS] = {6, 7, 8};  //C1C2C3

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

void setup()
{
  pinMode(lock, OUTPUT);
  Serial.begin(9600);
}

void loop()
{
  customKey = customKeypad.getKey();
  if (customKey != 0x00)
  {
    Data[data_count] = customKey;
    Serial.print(Data[data_count]);
    data_count++;
    if (data_count == 4)
    {
      Serial.println();  //insert Newline on Serial Monitor
      if (strcmp(myPassword, Data) == 0) //password has matched
      {
        digitalWrite(13, HIGH);
        delay(1500);
        digitalWrite(13, LOW);
        data_count = 0;   //index is reset
        memset(Data, 0, 5); //array is cleared
      }
    }
  }
}

But, my application is to receive another signal from another Arduino, if the password is correct, the other Arduino return 2 signal, if not it will return one. I also need to pass the password through a relay, if the first digit is 1, the relay will trigger once and delay for 5 second before sending another digit. How do I send each digit after I press a button

Reminds me of the way telephones worked in the last century.

Why are you having to use this unconventional method of digital communicating between two systems?

If you are programming both sides, do yourself a favor and get off the track you are on and learn about serial communication, the first and easiest way to get things talking. And perfect for this scenario.

a7

You need to employ some efforts to realize your objectives having have connection between two Arduinos over a suitable network.

sorry for the super late update, i found the suitable solution for my problem. thank you all for helping me.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.