hello and good day.
Im kindda stuck with my application that I
m 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.