Having problems getting my code to work with both RFID and keypad, without having to use a boolean to define which one I want to use, my aim is to give user the ability to do either or without having to change the code in anyway. My main issue is that the 'readkeypad'
function causes errors when lines 52 through 77 are within the code, (for the RFID), without these the code works perfectly fine.
#include <Keypad.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Servo.h> //-
#include <MFRC522.h>
#define SS_PIN 10
#define RST_PIN 9
MFRC522 mfrc522(SS_PIN, RST_PIN);
LiquidCrystal_I2C lcd(0x27, 16, 2);
Servo ServoMotor; //-
String pad;
const int buzzer = 9;
const byte numRows = 4;
const byte numCols = 4;
String password = "281205";
char keypressed;
char keymap[numRows][numCols] =
{
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
//------------------------------------------------------------
byte rowPins[numRows] = {10, 9, 8, 7};
byte colPins[numCols] = {6, 5, 4, 3};
Keypad myKeypad = Keypad(makeKeymap(keymap), rowPins, colPins, numRows, numCols);
void setup() {
ServoMotor.attach(11); //-
//- LockedPosition(true); //-
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Enter Password");
lcd.setCursor(0, 1);
lcd.print("Or Scan Card");
delay(3000);
lcd.clear();
pinMode(buzzer, OUTPUT); // Set buzzer - pin 9 as an output
}
void loop() {
// put your main code here, to run repeatedly:
if ( ! mfrc522.PICC_IsNewCardPresent())
{
if ( ! mfrc522.PICC_ReadCardSerial())
return;
{
String content= "";
byte letter;
for (byte i = 0; i < mfrc522.uid.size; i++)
{
content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
content.concat(String(mfrc522.uid.uidByte[i], HEX));
}
content.toUpperCase();
if (content.substring(1) == "13 E3 B7 19") //change here the UID of the card/cards that you want to give access
{
lcd.print("Authorized access");
}
else
{
lcd.print(" Access denied");
return;
}
}
}
else
{
readKeypad();
switch (keypressed) //== '#') {
{ case '#':
if (pad == password) {
lcd.setCursor(0, 1);
lcd.print("Access Granted");
ServoMotor.write(60); //- motor moving
tone(buzzer, 700); // Send 1KHz sound signal...
delay(500); // ...for 1 sec
noTone(buzzer); // Stop sound...
delay(6000);
lcd.clear();
pad = "";
} else {
lcd.setCursor(0, 1);
lcd.print("Access Denied");
} break;
case '*':
pad = "";
lcd.clear();
break;
case 'A':
ServoMotor.write(11);
ServoMotor.write(11);
lcd.clear();
pad = "";
break;
}
lcd.setCursor(0, 0);
lcd.print(pad);
delay(100);
}
void readKeypad() {
keypressed = myKeypad.getKey();
if (keypressed != '#') {
String konv = String(keypressed);
pad += konv;
}
}
}