Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SPI.h>
- #include <Wire.h>
- #include <MFRC522.h>
- #include <LiquidCrystal_I2C.h>
- #include <Servo.h>
- #include <Keypad.h>
- #include <Password.h>
- #include <SdFat.h>
- Servo doorLock;
- #define MFRC_RST_PIN 9
- #define MFRC_SS_PIN 10
- #define SD_SS_PIN 53
- #define STATE_STARTUP 0
- #define STATE_STARTUP_ERROR 1
- #define STATE_STARTING 2
- #define STATE_WAITING 3
- #define STATE_SCAN_INVALID 4
- #define STATE_SCAN_VALID 5
- #define STATE_SCAN_MASTER 6
- #define STATE_ADDED_CARD 7
- #define STATE_REMOVED_CARD 8
- #define REDPIN 6
- #define BLUEPIN 4
- #define BUZZER 3
- #define RELAY3 5
- const int cardSize = 4;
- byte masterCard[cardSize] = {149,80,173,117};
- byte readCard[cardSize] = {0,0,0,0};
- // Create MFRC522 instance
- MFRC522 mfrc522(MFRC_SS_PIN, MFRC_RST_PIN);
- // Set the LCD I2C address
- LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
- SdFat sd;
- Password password = Password( "00000" );
- const byte ROWS = 4; // Four rows
- const byte COLS = 3; // Three columns
- // Define the Keymap
- char keys[ROWS][COLS] = {
- {'1','2','3',},
- {'4','5','6',},
- {'7','8','9',},
- {'*','0',' ',}
- };
- // Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
- byte rowPins[ROWS] = {25, 24, 23, 22}; //connect to the row pinouts of the keypad
- byte colPins[COLS] = {28, 27, 26}; //connect to the column pinouts of the keypad
- const int buttonPin = 7;
- int buttonState = 0;
- // Create the Keypad
- Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
- byte currentState = STATE_STARTUP;
- unsigned long LastStateChangeTime;
- unsigned long StateWaitTime;
- char cardFile[] = "cards.txt";
- char cardTempFile[] = "cardsTemp.txt";
- //------------------------------------------------------------------------------------
- void PrintCard(byte printCard[cardSize])
- {
- int index;
- Serial.print("Card - ");
- for(index = 0; index < 4; index++)
- {
- if (index > 0)
- {
- Serial.print(",");
- }
- Serial.print(printCard[index]);
- }
- Serial.println(" ");
- }
- //------------------------------------------------------------------------------------
- boolean findCard()
- {
- byte currentCard[cardSize];
- char text[10];
- char c1;
- int index;
- int value;
- //Serial.print("find ");
- //PrintCard(readCard);
- // open input file
- ifstream readStr(cardFile);
- // check for open error
- if (!readStr.is_open())
- {
- return false;
- }
- index = 0;
- // read until input fails
- while (!readStr.eof())
- {
- readStr >> value >> c1;
- if (readStr.fail())
- {
- break;
- }
- currentCard[index] = value;
- index++;
- if (index > 3)
- {
- //Serial.print("file read ");
- //PrintCard(currentCard);
- if ((memcmp(currentCard, readCard, 4)) == 0)
- {
- return true;
- }
- index = 0;
- }
- }
- return false;
- }
- //------------------------------------------------------------------------------------
- void addCard()
- {
- int index;
- SdFile writeFile;
- //Serial.print("add ");
- //PrintCard(readCard);
- if (writeFile.open(cardFile, O_RDWR | O_CREAT | O_AT_END))
- {
- for(index = 0; index < 4; index++)
- {
- writeFile.print(readCard[index]);
- writeFile.print(",");
- }
- writeFile.close();
- }
- return;
- }
- //------------------------------------------------------------------------------------
- void removeCard()
- {
- byte currentCard[cardSize];
- char text[10];
- char c1;
- int readIndex, writeIndex;
- int value;
- SdFile writeFile;
- //Serial.print("remove ");
- //PrintCard(readCard);
- // open input file
- ifstream readStr(cardFile);
- // check for open error
- if (!readStr.is_open())
- {
- return;
- }
- if (writeFile.open(cardTempFile, O_RDWR | O_CREAT | O_AT_END))
- {
- readIndex = 0;
- while (!readStr.eof())
- {
- readStr >> value >> c1;
- if (readStr.fail())
- {
- break;
- }
- currentCard[readIndex] = value;
- readIndex++;
- if (readIndex > 3)
- {
- //Serial.print("file write ");
- //PrintCard(currentCard);
- if (!((memcmp(currentCard, readCard, 4)) == 0))
- {
- for (writeIndex = 0; writeIndex < 4; writeIndex++)
- {
- writeFile.print(currentCard[writeIndex]);
- writeFile.print(",");
- }
- writeFile.close();
- }
- }
- readIndex = 0;
- }
- }
- sd.remove(cardFile);
- sd.rename(cardTempFile, cardFile);
- return;
- }
- //------------------------------------------------------------------------------------
- int readCardState()
- {
- int index;
- for(index = 0; index < 4; index++)
- {
- readCard[index] = mfrc522.uid.uidByte[index];
- }
- //Serial.print("State ");
- //PrintCard();
- //Check Master Card
- if ((memcmp(readCard, masterCard, 4)) == 0)
- {
- return STATE_SCAN_MASTER;
- }
- if (findCard() == true)
- {
- return STATE_SCAN_VALID;
- }
- return STATE_SCAN_INVALID;
- }
- //------------------------------------------------------------------------------------
- void DisplayInfo(char *Line1Str, char *Line2Str, int RedPinState, int BluePinState)
- {
- lcd.clear();
- lcd.print(Line1Str);
- lcd.setCursor(0,1);
- lcd.print(Line2Str);
- digitalWrite(REDPIN, RedPinState);
- digitalWrite(BLUEPIN, BluePinState);
- }
- //------------------------------------------------------------------------------------
- void updateState(byte aState)
- {
- if (aState == currentState)
- {
- return;
- }
- // do state change
- switch (aState)
- {
- case STATE_STARTING:
- DisplayInfo("RFID Scanner", "Starting up", HIGH, HIGH);
- StateWaitTime = 1000;
- break;
- case STATE_STARTUP_ERROR:
- DisplayInfo("Error", "SD card", HIGH, HIGH);
- StateWaitTime = 1000;
- break;
- case STATE_WAITING:
- DisplayInfo("Waiting for Card", "to be swiped", LOW, LOW);
- StateWaitTime = 1000;
- break;
- case STATE_SCAN_INVALID:
- if (currentState == STATE_SCAN_MASTER)
- {
- addCard();
- aState = STATE_ADDED_CARD;
- DisplayInfo("Card Scanned", "Card Added", LOW, HIGH);
- StateWaitTime = 2000;
- }
- else if (currentState == STATE_REMOVED_CARD)
- {
- return;
- }
- else
- {
- DisplayInfo("Card Scanned", "Invalid Card", HIGH, LOW);
- StateWaitTime = 2000;
- digitalWrite(BUZZER, HIGH);
- delay(200);
- digitalWrite(BUZZER, LOW);
- delay(200);
- digitalWrite(BUZZER, HIGH);
- delay(200);
- digitalWrite(BUZZER, LOW);
- delay(200);
- digitalWrite(BUZZER, HIGH);
- delay(200);
- digitalWrite(BUZZER, LOW);
- }
- break;
- case STATE_SCAN_VALID:
- if (currentState == STATE_SCAN_MASTER)
- {
- removeCard();
- aState = STATE_REMOVED_CARD;
- DisplayInfo("Card Scanned", "Card Removed", LOW, HIGH);
- StateWaitTime = 2000;
- }
- else if (currentState == STATE_ADDED_CARD)
- {
- return;
- }
- else
- {
- DisplayInfo("Card Scanned", "Valid Card", LOW, HIGH);
- StateWaitTime = 2000;
- digitalWrite(RELAY3,LOW);
- Serial.println("Light ON");
- digitalWrite(BUZZER, HIGH);
- doorLock.write(0); // releases the door, you need to adjust this to positioning the servo according your door locker
- delay(8000); // wait 5 senconds
- doorLock.write(90);
- digitalWrite(BUZZER, LOW);
- digitalWrite(RELAY3,HIGH);
- delay(10000);
- }
- break;
- case STATE_SCAN_MASTER:
- DisplayInfo("Master Card", "", LOW, HIGH);
- StateWaitTime = 5000;
- digitalWrite(BUZZER, HIGH);
- delay(200);
- digitalWrite(BUZZER, LOW);
- break;
- }
- //Serial.print("Current State - ");
- //Serial.print(currentState);
- //Serial.print(", New State - ");
- //Serial.println(aState);
- currentState = aState;
- LastStateChangeTime = millis();
- }
- //------------------------------------------------------------------------------------
- void setup()
- {
- SPI.begin(); // Init SPI Bus
- mfrc522.PCD_Init(); // Init MFRC522
- if (!sd.begin(SD_SS_PIN, SPI_HALF_SPEED))
- {
- updateState(STATE_STARTUP_ERROR);
- }
- lcd.begin(16,2);
- LastStateChangeTime = millis();
- updateState(STATE_STARTING);
- doorLock.attach(11);
- pinMode(REDPIN, OUTPUT);
- pinMode(BLUEPIN, OUTPUT);
- pinMode(BUZZER, OUTPUT);
- pinMode(RELAY3, OUTPUT);
- Serial.begin(9600);
- pinMode(buttonPin, INPUT);
- keypad.addEventListener(keypadEvent); //add an event listener for this keypad
- keypad.setDebounceTime(250);
- }
- //------------------------------------------------------------------------------------
- void loop()
- {
- keypad.getKey();
- buttonState = digitalRead(buttonPin);
- if (buttonState == HIGH) {
- }
- byte cardState;
- if ((currentState != STATE_WAITING) &&
- (currentState != STATE_STARTUP_ERROR) &&
- (StateWaitTime > 0) &&
- (LastStateChangeTime + StateWaitTime < millis()))
- {
- updateState(STATE_WAITING);
- }
- // Look for new cards
- if ( ! mfrc522.PICC_IsNewCardPresent())
- {
- return;
- }
- // Select one of the cards
- if ( ! mfrc522.PICC_ReadCardSerial())
- {
- return;
- }
- if (currentState != STATE_STARTUP_ERROR)
- {
- cardState = readCardState();
- updateState(cardState);
- }
- }
- void keypadEvent(KeypadEvent eKey){
- switch (keypad.getState()){
- case PRESSED:
- Serial.print(eKey);
- switch (eKey){
- case ' ': guessPassword(); break;
- default:
- password.append(eKey);
- }
- }}
- void guessPassword(){
- if (password.evaluate()){
- lcd.setCursor(0,0);
- lcd.print(" VALID PASSWORD "); //
- password.reset(); //resets password after correct entry
- delay(600);
- lcd.setCursor(0,1);
- lcd.print(" Welcome ");
- digitalWrite(BLUEPIN,HIGH);
- digitalWrite(BUZZER, HIGH);
- doorLock.write(0); // releases the door, you need to adjust this to positioning the servo according your door locker
- delay(5000); // wait 5 senconds
- doorLock.write(120);
- digitalWrite(BUZZER, LOW);
- digitalWrite(BLUEPIN,LOW);
- delay(2000);
- lcd.clear();
- }
- else{
- lcd.print("INVALID PASSWORD ");
- password.reset(); //resets password after INCORRECT entry
- delay(600);
- digitalWrite(REDPIN,HIGH);
- digitalWrite(BUZZER, HIGH);
- delay(200);
- digitalWrite(BUZZER, LOW);
- delay(200);
- digitalWrite(BUZZER, HIGH);
- delay(200);
- digitalWrite(BUZZER, LOW);
- delay(200);
- digitalWrite(BUZZER, HIGH);
- delay(200);
- digitalWrite(BUZZER, LOW);
- digitalWrite(REDPIN,LOW);
- lcd.clear();
- }
- }
Add Comment
Please, Sign In to add comment