#include <Keypad.h>
#include <LiquidCrystal.h>
#include <SPI.h>
#include <MFRC522.h>
#define SS_PIN 16
#define RST_PIN 17
LiquidCrystal lcd(12, 11, 10, 9, 8, 7);
const byte ROWS = 4;
const byte COLS = 3;
int lcdRow = 0;
char keys[ROWS][COLS] = {
{'1', '2', '3'},
{'4', '5', '6'},
{'7', '8', '9'},
{'*', '0', '#'}
};
byte rowPins[ROWS] = {5, 4, 3, 2};
byte colsPins[COLS] = {A3, A2, A1};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colsPins, ROWS, COLS);
String password;
int ledPin = 20; // Assuming pin 20 controls an LED
int buttonPin = 26; // Assuming pin 26 is for a button
unsigned long lastButtonPressTime = 0;
bool ledOn = false;
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.
void setup() {
Serial.begin(115200);
lcd.begin(16, 2); // Assuming 16x2 LCD, adjust according to your setup
lcd.print("Enter Password: ");
delay(1000);
lcd.clear();
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, HIGH); // Ensure LED is initially off
pinMode(buttonPin, INPUT_PULLUP); // Use internal pull-up resistor for button
pinMode(LED_BUILTIN, OUTPUT);
SPI.begin(); // Initiate SPI bus
mfrc522.PCD_Init(); // Initiate MFRC522
}
void loop() {
handleKeypad();
handleButton();
handleRfid();
}
void handleKeypad() {
char key = keypad.getKey();
if (key) {
lcd.print(key);
lcd.setCursor(++lcdRow, 0);
password += key;
if (key == '#') {
lcd.clear();
lcd.print("Checking");
lcd.setCursor(0, 1);
lcd.print(password);
delay(1000);
if (password == "111#") {
lcd.clear();
lcd.print("Accepted");
delay(1000);
digitalWrite(ledPin, LOW); // Turn on LED
ledOn = true;
delay(5000);
digitalWrite(ledPin, HIGH); // Turn off LED
ledOn = false;
lcd.clear();
} else {
lcd.clear();
lcd.print("Denied");
}
password = "";
}
if (key == '*') {
lcd.print("Execute Order66");
delay(1000);
lcd.clear();
password = "";
lcdRow = 0; // Reset LCD row
}
}
}
void handleButton() {
int buttonState = digitalRead(buttonPin);
unsigned long currentTime = millis();
if (buttonState == LOW && currentTime - lastButtonPressTime > 1000) {
lastButtonPressTime = currentTime;
digitalWrite(ledPin, LOW); // Turn off LED
delay(5000);
digitalWrite(ledPin, HIGH); // Turn on LED
}
}
void handleRfid() {
static unsigned long lastAccessTime = 0;
static bool accessGranted = false;
static bool ledStateBeforeAccess = HIGH; // Assuming the LED is initially off
// Look for new cards
if (!mfrc522.PICC_IsNewCardPresent()) {
return;
}
// Select one of the cards
if (!mfrc522.PICC_ReadCardSerial()) {
return;
}
// Show UID on serial monitor
Serial.print("UID tag: ");
String content = "";
for (byte i = 0; i < mfrc522.uid.size; i++) {
Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
Serial.print(mfrc522.uid.uidByte[i], HEX);
content.concat(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
content.concat(String(mfrc522.uid.uidByte[i], HEX));
}
Serial.println();
Serial.print("Message: ");
content.toUpperCase();
if (content.substring(1) == "35EB5843") { // Change here the UID of the card/cards that you want to give access
Serial.println("Authorized access");
if (!accessGranted) {
ledStateBeforeAccess = digitalRead(ledPin); // Save current LED state
digitalWrite(ledPin, LOW); // Turn off the LED
lastAccessTime = millis();
accessGranted = true;
}
} else {
accessGranted = false;
}
if (accessGranted && millis() - lastAccessTime >= 5000) {
digitalWrite(ledPin, ledStateBeforeAccess); // Restore original LED state
accessGranted = false;
Serial.println("Access ended");
Serial.println("LED state restored");
}
}
So basically I'm in highschool creating a security system using a button, Keypad, RFID, and motion sensor. I was successfully able to integrate the Button and Keypad together, but The RFID has been causing me all sorts of problems. Its continuously making the LED flash in 5 second intervals without any prompt to do so and throught my numerous tweaks its still doing the same exact thing. The code I have presented is what I beleive to be the closest to the anwser.
tldr: RFID make code no worky
Now with all that yappin out the way, I would like to ask for guidance on how to continue from this point on.