Hi all,
I am very new to Arduino and coding. I found a tutorial online and the codes were posted. I was really interested on the idea of making similar project to what I have found. I bought the hardware and hook everything up. I have installed the same exact code but did not work with me and it didn't give me an error either. Furthurmore, I have installed all the necessary libraries that are needed for the LCD and the keypad. here is my code. Please if anyone has any idea of what is going on and why it is not working let me know pleaaaaase.
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
int greenLED = 12;
int redLED = 13;
const int RFIDEnablePin = 2;
const byte numEmployees = 4;
char *employees[][4] = {
{"04162C5775","Mike","Myers","1234"},
{"0F0296AEE7","Dennis","Hawkins","9876"},
{"0F0296AF66","Michelle","Myers","2580"},
{"0F0296AEE8","John","Gibert","1245"}
};
int currentState = 1;
const int waitingForTagID = 1;
const int waitingForPasscode = 2;
// Misc Global Variables
char* ourCode;
int currentPosition = 0;
int val = 0;
char code[10];
int bytesread = 0;
int currentEmployee;
long myTimer;
const char locationName[] = "Let's Make It Studio A";
//define the keypad
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] = {11,10,9,8};
byte colPins[cols] = {7,6,5,4};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, rows, cols);
LiquidCrystal_I2C lcd(0x3F,20,4); //
void setup()
{
lcd.init(); //
lcd.init();
delay(500);
lcd.backlight(); //
delay(500);
Serial.begin(2400); //
pinMode(RFIDEnablePin, OUTPUT);
//setup and turn off both LEDs
pinMode(redLED, OUTPUT);
pinMode(greenLED, OUTPUT);
digitalWrite(redLED, LOW);
digitalWrite(greenLED, LOW);
displayTagScanScreen(); //Put up default screen for reading the tags
}
void loop()
{
int l;
int empid;
switch (currentState)
{
case waitingForTagID:
digitalWrite(RFIDEnablePin, LOW);
if(Serial.available() > 0) {
if((val = Serial.read()) == 10) {
bytesread = 0;
while(bytesread<10) {
if( Serial.available() > 0) {
val = Serial.read();
if((val == 10)||(val == 13)) {
break;
}
code[bytesread] = val;
bytesread++;
}
}
if(bytesread == 10) {
code[10] = '\0';
empid = getEmployee(code);
if (empid == -1) {
digitalWrite(RFIDEnablePin, HIGH);
invalidCard();
digitalWrite(RFIDEnablePin, LOW);
} else {
currentEmployee = empid;
displayCodeEntryScreen(employees[empid][1]);
currentState = waitingForPasscode;
ourCode = employees[empid][3];
clearSerial();
myTimer = millis();
}
}
bytesread = 0;
}
}
break;
case waitingForPasscode:
digitalWrite(RFIDEnablePin, HIGH);
char key = keypad.getKey();
if (int(key) != 0) {
// Reset timeout timer to current time
myTimer = millis();
// Clear the asterisk area for code entry
lcd.setCursor(14,3);
lcd.print(" ");
lcd.setCursor(14,3);
// display asterisk for each code entered
for (l=0; l<=currentPosition; ++l)
{
lcd.print("*");
}
if (key == ourCode[currentPosition])
{
++currentPosition;
if (currentPosition == 4)
{
logEmployee(currentEmployee, true);
unlockDoor();
currentPosition = 0;
currentState = waitingForTagID;
clearSerial();
}
} else {
// Does not match so it is an invalid code
logEmployee(currentEmployee, false); // Log to the serial port the failue
invalidCode(); // Display invalid code screen
currentPosition = 0; // Set the read position back to 0
currentState = waitingForTagID; // Change the state back to waiting for RFID read
clearSerial(); // Clear and junk that is on the serial port
}
} else {
if ((myTimer+10000) <= millis()) {
//no key pressed for timeout period
codeEntryTimeout(); // Display invalid code screen
currentPosition = 0; // Set the read position back to 0
currentState = waitingForTagID; // Change the state back to waiting for RFID read
clearSerial();
}
}
}
}
FUNCTION: getEmployee
RETURNS:
The array element id of the employee that matches the RFID TAG. If no employee matches
then -1 is returned.
INPUTS:
tagID (char) : The value read from the RFID TAG
int getEmployee(char tagID[])
{
int l;
for (l=0; l<numEmployees; ++l)
{
if (strcmp(tagID, employees[l][0]) == 0)
return l;
}
return -1;
}
FUNCTION: logEmployee
void logEmployee(int empid, int grantedAccess)
{
if (grantedAccess)
Serial.println("ACCESS GRANTED:");
else
Serial.println("ACCESS DENIED:");
Serial.print(" TO: ");
Serial.println(locationName);
Serial.print(" FOR: ");
Serial.print(employees[empid][1]);
Serial.print(" ");
Serial.println(employees[empid][2]);
Serial.println(" ");
Serial.println(" ");
}
FUNCTION: clearSerial
RETURNS:
NOTHING (void)
INPUTS:
NONE
void clearSerial()
{
while (Serial.available() > 0) {
Serial.read();
}
}
void invalidCode()
{
digitalWrite(redLED, HIGH);
clearScreen();
lcd.setCursor(0,0);
lcd.print("********************");
lcd.setCursor(0,1);
lcd.print("** ACCESS DENIED! **");
lcd.setCursor(0,2);
lcd.print("** INVALID CODE **");
lcd.setCursor(0,3);
lcd.print("********************");
delay(5000);
digitalWrite(redLED, LOW);
displayTagScanScreen();
}
void invalidCard()
{
digitalWrite(redLED, HIGH);
clearScreen();
lcd.setCursor(0,0);
lcd.print("********************");
lcd.setCursor(0,1);
lcd.print("** ACCESS DENIED! **");
lcd.setCursor(0,2);
lcd.print("** INVALID CARD **");
lcd.setCursor(0,3);
lcd.print("********************");
delay(5000);
digitalWrite(redLED, LOW);
displayTagScanScreen();
}
void unlockDoor()
{
digitalWrite(greenLED, HIGH);
clearScreen();
lcd.setCursor(0,0);
lcd.print("********************");
lcd.setCursor(0,1);
lcd.print("** ACCESS GRANTED **");
lcd.setCursor(0,2);
lcd.print("** WELCOME!! **");
lcd.setCursor(0,3);
lcd.print("********************");
//add any code to unlock the door here
delay(5000);
digitalWrite(greenLED, LOW);
displayTagScanScreen();
}
void codeEntryTimeout()
{
digitalWrite(redLED, HIGH);
clearScreen();
lcd.setCursor(0,0);
lcd.print("********************");
lcd.setCursor(0,1);
lcd.print("** CODE ENTRY **");
lcd.setCursor(0,2);
lcd.print("** TIMEOUT!! **");
lcd.setCursor(0,3);
lcd.print("********************");
//add any code to unlock the door here
delay(5000);
digitalWrite(greenLED, LOW);
displayTagScanScreen();
}
void displayTagScanScreen()
{
clearScreen();
lcd.setCursor(0,0);
lcd.print("Let's Make It Two");
lcd.setCursor(0,1);
lcd.print("Factor Auth Project");
lcd.setCursor(2,3);
lcd.print("Scan Tag...");
}
void displayCodeEntryScreen(char *firstName)
{
clearScreen();
lcd.setCursor(0,0);
lcd.print("Welcome");
lcd.setCursor(8,0);
lcd.print(firstName);
lcd.setCursor(0,2);
lcd.print("Please Enter Your");
lcd.setCursor(1,3);
lcd.print("Secret Code:");
}
void clearScreen()
{
lcd.setCursor(0,0);
lcd.print(" ");
lcd.setCursor(0,1);
lcd.print(" ");
lcd.setCursor(0,2);
lcd.print(" ");
lcd.setCursor(0,3);
lcd.print(" ");
}
Your help is highly appreciated.