that would be an idea - not tested if this would even compile.... assuming all your stuff is connected correctly and that your tag reader is in Serial and properly sending codes in the right way.
#include <Wire.h>
#include <Keypad.h>
const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {22, 23, 24, 25};
byte colPins[COLS] = {26, 27, 28, 29};
Keypad customKeypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS);
char customKey;
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
enum STATES {WAITING_RFID, ACQUIRING_RFID, ACQUIRING_PWD, ACTION} currentState;
const byte PasswordLength = 6;
char entryPassword[PasswordLength + 1];
const char MasterPassword[PasswordLength + 1] = "123456";
const unsigned long TimeOut = 10000ul;
unsigned long startTime;
unsigned long tickTime;
const byte tagLength = 12;
char correctTag[tagLength + 1] = "000003026869"; // could be declared as const, not sure what you want to do
char tagInput[tagLength + 1];
byte inputIndex;
const byte MOTOR_PIN1 = 31;
const byte MOTOR_PIN2 = 32;
void initializeState()
{
lcd.clear();
lcd.print(F("Present Card"));
while (Serial.available()) Serial.read(); // empty the Serial buffer
currentState = WAITING_RFID;
}
void setup()
{
Serial.begin(9600);
lcd.begin(20, 4); // Assuming you have 20 Cols and 4 Rows in your LCD
initializeState();
}
void loop()
{
// ------------------ STATE WAITING FOR RFID ------------------
if (currentState == WAITING_RFID) {
if (Serial.available()) {
inputIndex = 0;
currentState = ACQUIRING_RFID;
}
// ------------------ STATE ACQUIRING RFID ------------------
} else if (currentState == ACQUIRING_RFID) {
while (Serial.available()) {
int c = Serial.read();
if ((inputIndex < tagLength) && (c != -1)) tagInput[inputIndex++] = (char) c; // ignore extra ones
}
if (inputIndex == tagLength) {
tagInput[tagLength] = '\0';
if (!strcmp(correctTag, tagInput)) {
lcd.setCursor(0, 0);
lcd.print(F("Valid Card"));
lcd.setCursor(0, 2);
lcd.print(F("Welcome"));
delay(1000);
lcd.clear();
lcd.print("Enter Password");
inputIndex = 0;
currentState = ACQUIRING_PWD;
} else {
lcd.clear();
lcd.print(F("Invalid Card"));
lcd.setCursor(0, 2);
lcd.print(F("Go away"));
delay(1000);
initializeState();
}
}
// ------------------ STATE ACQUIRING PWD ------------------
} else if (currentState == ACQUIRING_PWD) {
customKey = customKeypad.getKey();
if (customKey != NO_KEY) {
entryPassword[inputIndex] = customKey;
lcd.setCursor(inputIndex, 1);
lcd.print(customKey);
if (++inputIndex == PasswordLength) {
entryPassword[inputIndex] = '\0';
if (!strcmp(MasterPassword, entryPassword)) {
lcd.clear();
lcd.print(F("CORRECT"));
lcd.setCursor(0, 2);
lcd.print(F("ACCESS GRANTED"));
delay(3000);
lcd.clear();
// ===========================================================
// password is good
// TRIGGER THE MOTORS HERE MAY BE TO OPEN SOMETHING
// ....
// ....
// ....
// ===========================================================
lcd.print(F("REMAINING TIME"));
lcd.setCursor(0, 2);
lcd.print(TimeOut / 1000);
startTime = millis();
tickTime = startTime + 1000;
currentState = ACTION;
} else {
lcd.clear();
lcd.print(F("INCORRECT"));
lcd.setCursor(0, 2);
lcd.print(F("ACCESS REFUSED"));
delay(3000);
initializeState();
}
}
}
// ------------------ STATE ACTION TRIGGERED ------------------
} else if (currentState == ACTION) {
// stay in that state for TimeOut ms
if (millis() - startTime >= TimeOut) {
// ===========================================================
// TRIGGER THE MOTORS HERE MAY BE TO CLOSE SOMETHING
// ....
// ....
// ....
// ===========================================================
initializeState();
} else {
if (millis() - tickTime >= 1000) {
lcd.setCursor(0, 2);
lcd.print((TimeOut - (millis() - startTime)) / 1000);
lcd.print(F(" ")); // erase previous numbers that could be trailing
tickTime += 1000;
}
}
}
}