I have the below code that "opens" a servo with the scanning of the correct RFID and "locks" again on the push of a button.
What I am needing to do is have the same button able to "lock" is open and open if locked.
Obviously the servo may be in either position depending on whether the last action was done via the RFID tag.
I hope this make sense
Any thoughts?
/*
Arduino Door Lock Access Control Project
by Dejan Nedelkovski, www.HowToMechatronics.com
Library: MFRC522, https://github.com/miguelbalboa/rfid
*/
#include <SPI.h>
#include <MFRC522.h>
#include <LiquidCrystal.h>
#include <Servo.h>
#define RST_PIN 9
#define SS_PIN 10
int LED = A0;
int BUTTON = A1;
byte readCard[4];
char* myTags[100] = {};
int tagsCount = 0;
String tagID = "";
boolean successRead = false;
boolean correctTag = false;
//int proximitySensor;
//boolean doorOpened = false;
// Create instances
MFRC522 mfrc522(SS_PIN, RST_PIN);
LiquidCrystal lcd(7, 6, 5, 4, 3, 2); //Parameters: (rs, enable, d4, d5, d6, d7)
// it's a 16x2 LCD so...
int screenWidth = 20;
int screenHeight = 4;
// line1 = scrolling
String line1 = " Acedemic Support Section Enter At Your Own Risk!!! "; //Edit scrolling message
// just some reference flags
int stringStart, stringStop = 0;
int scrollCursor = screenWidth;
Servo myServo; // Servo motor
// Initialize Pins for led's, servo and buzzer
// Blue LED is connected to 5V
constexpr uint8_t greenLed = A4;
constexpr uint8_t redLed = A3;
void setup() {
// Initiating
SPI.begin(); // SPI bus
mfrc522.PCD_Init(); // MFRC522
lcd.begin(20, 4); // LCD screen
myServo.attach(8); // Servo motor
myServo.write(00); // Initial lock position of the servo motor
// Prints the initial message
lcd.print("-No Master Tag!-");
lcd.setCursor(0, 1);
lcd.print(" SCAN NOW");
// Waits until a master card is scanned
while (!successRead) {
successRead = getID();
if ( successRead == true) {
myTags[tagsCount] = strdup(tagID.c_str()); // Sets the master tag into position 0 in the array
lcd.clear();
lcd.setCursor(2, 0);
lcd.print("Master Tag Set!");
tagsCount++;
}
}
successRead = false;
printNormalModeMessage();
{
pinMode(A0, OUTPUT);
pinMode (A1, INPUT);
pinMode(A2, OUTPUT);
}
}
void loop() {
ScrollMessage();
{ //Lock Door
if (digitalRead(A1) == HIGH)
{ digitalWrite(A2, HIGH); //Switch on power to the servo
delay(3000); //Lock delay
myServo.write(0); //Reset Servo
digitalWrite(greenLed, LOW);
digitalWrite(redLed, HIGH); //Set Red Locked LED
delay(3000);
digitalWrite(A2, LOW); //Switch off power to the servo
}
}
// Set master card and add or remove tags
if ( ! mfrc522.PICC_IsNewCardPresent()) { //If a new PICC placed to RFID reader continue
return;
}
if ( ! mfrc522.PICC_ReadCardSerial()) { //Since a PICC placed get Serial and continue
return;
}
tagID = "";
// The MIFARE PICCs that we use have 4 byte UID
for ( uint8_t i = 0; i < 4; i++) { //
readCard[i] = mfrc522.uid.uidByte[i];
tagID.concat(String(mfrc522.uid.uidByte[i], HEX)); // Adds the 4 bytes in a single String variable
}
tagID.toUpperCase();
mfrc522.PICC_HaltA(); // Stop reading
correctTag = false;
// Checks whether the scanned tag is the master tag
if (tagID == myTags[0]) {
lcd.clear();
lcd.print("Program mode:");
lcd.setCursor(0, 1);
lcd.print("Add/Remove Tag");
while (!successRead) {
successRead = getID();
if ( successRead == true) {
for (int i = 0; i < 100; i++) {
if (tagID == myTags[i]) {
myTags[i] = "";
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" Tag Removed!");
printNormalModeMessage();
return;
}
}
myTags[tagsCount] = strdup(tagID.c_str());
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" Tag Added!");
printNormalModeMessage();
tagsCount++;
return;
}
}
}
successRead = false;
// Checks whether the scanned tag is authorized
for (int i = 0; i < 100; i++) {
if (tagID == myTags[i]) {
digitalWrite(A2, HIGH); //Switch on power to the servo
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" Access Granted!");
myServo.write(170); // Unlocks the door
delay(3000);
digitalWrite(A2, LOW); //Switch off power to the servo
digitalWrite(greenLed, HIGH);
digitalWrite(A0, LOW);
// delay(3000);
// digitalWrite(greenLed, LOW);
// myServo.write(0);
printNormalModeMessage();
correctTag = true;
}
}
if (correctTag == false) {
digitalWrite(redLed, HIGH);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" Access Denied!");
printNormalModeMessage();
delay(1000);
digitalWrite(redLed, LOW);
}
}
uint8_t getID() {
// Getting ready for Reading PICCs
if ( ! mfrc522.PICC_IsNewCardPresent()) { //If a new PICC placed to RFID reader continue
return 0;
}
if ( ! mfrc522.PICC_ReadCardSerial()) { //Since a PICC placed get Serial and continue
return 0;
}
tagID = "";
for ( uint8_t i = 0; i < 4; i++) { // The MIFARE PICCs that we use have 4 byte UID
readCard[i] = mfrc522.uid.uidByte[i];
tagID.concat(String(mfrc522.uid.uidByte[i], HEX)); // Adds the 4 bytes in a single String variable
}
tagID.toUpperCase();
mfrc522.PICC_HaltA(); // Stop reading
return 1;
}
void printNormalModeMessage() {
delay(1500);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" -Access Control-");
lcd.setCursor(0, 1);
lcd.print(" Scan Tag");
}
void ScrollMessage() {
lcd.setCursor(scrollCursor, 3);
lcd.print(line1.substring(stringStart,stringStop));
// lcd.setCursor(0, 0);
// lcd.print(line2);
delay(300);
// lcd.clear();
if(stringStart == 0 && scrollCursor > 0){
scrollCursor--;
stringStop++;
} else if (stringStart == stringStop){
stringStart = stringStop = 0;
scrollCursor = screenWidth;
} else if (stringStop == line1.length() && scrollCursor == 0) {
stringStart++;
} else {
stringStart++;
stringStop++;
}
}