So I’m new here , sorry for the title. I’ve got this shared project by Marcelo of Instructables. It’s an access control system. So program works like this , if a registered RFID card is scanned the servo motor will unlock and automatically lock after 5 secs. What I want to now is , I want the servo to lock again if the same tag is being read. Can you help me with this? I’m totally new with this. Thanks! By the way , here’s the code:
// libraries
#include <SPI.h>
#include <RFID.h>
#include <Servo.h>
#include "pitches.h"
#include <Wire.h>
// RFID definition
RFID rfid(10,5);
byte ealdsem[5] = {0x14, 0xBA, 0xA2, 0x45};// Al (e+ald+sem)
byte emodsem[5] = {0xFF, 0xFF , 0xFF , 0xFF , 0xFF};
// Put here the another allowed cards
byte serNum[5];
byte data[5];
// Melodies definition: access, welcome and rejection
int access_melody[] = {NOTE_G4,0,NOTE_A4,0, NOTE_B4,0,NOTE_A4,0,NOTE_B4,0, NOTE_C5,0};
int access_noteDurations[] = {8,8,8,8,8,4,8,8,8,8,8,4};
int fail_melody[] = {NOTE_G2,0,NOTE_F2,0,NOTE_D2,0};
int fail_noteDurations[] = {8,8,8,8,8,4};
// pins definition - LED, Buzzer and Servo-motor
int LED_access = 3;
int LED_intruder = 4;
int speaker_pin = 8;
int servoPin = 9;
// servo motor definition
Servo doorLock;
void setup(){
doorLock.attach(servoPin); // servo motor attaching
Serial.begin(9600); // Serial communication initialization
SPI.begin(); // SPI communication initialization
rfid.init(); // RFID module initialization
Serial.println("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
Serial.println("+ Welcome to my World +");
Serial.println("+ Access Control System with Log Manager +");
Serial.println("+ Waiting for a valid card... +");
Serial.println("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
delay(1000);
pinMode(LED_access,OUTPUT);
pinMode(LED_intruder,OUTPUT);
pinMode(speaker_pin,OUTPUT);
pinMode(servoPin,OUTPUT);
}
void loop(){
// Here you can create a variable for each user
// NAME_card ou KEY_card
boolean ealdsem_card = true; // that is my card (e + ald + sem)...e + almod = ald, seldom = sem
boolean emodsem_card = true;
if (rfid.isCard()){ // valid card found
if (rfid.readCardSerial()){ // reads the card
delay(1000);
data[0] = rfid.serNum[0]; // stores the serial number
data[1] = rfid.serNum[1];
data[2] = rfid.serNum[2];
data[3] = rfid.serNum[3];
data[4] = rfid.serNum[4];
}
rfid.halt(); // RFID module to standby
Serial.print("Card found!");
for(int i=0; i<5; i++){
if(data[i] != ealdsem[i]) ealdsem_card = false;
//if(data[i] != emodsem[i]) emodsem_card = false; // if it is not a valid card, put false to this user
// Here you can check the another allowed users, just put lines like above with the user name
}
Serial.println();
if (ealdsem_card){ // if a valid card was found
Serial.println("Hello Al!"); // print a message
for (int i = 0; i < 12; i++){ // play welcome sound
int access_noteDuration = 1000/access_noteDurations[i];
tone(speaker_pin, access_melody[i],access_noteDuration);
int access_pauseBetweenNotes = access_noteDuration * 1.30;
delay(access_pauseBetweenNotes);
noTone(speaker_pin);
}
}
// another cards analysis put many blocks like this as many user you have
else if(emodsem_card){// put the another allowed user here
Serial.println("Hello MODLES"); // put the user name in the "USER NAME"
for (int i = 0; i < 12; i++){
int access_noteDuration = 1000/access_noteDurations[i];
digitalWrite(LED_access, HIGH);
tone(speaker_pin, access_melody[i],access_noteDuration);
int access_pauseBetweenNotes = access_noteDuration * 1.30;
delay(access_pauseBetweenNotes);
noTone(speaker_pin);
}
}
else{ // if a card is not recognized
Serial.println("Card not recognized! Please contact your administrator!"); // print a message
digitalWrite(LED_intruder, HIGH); // turn on the red LED
for (int i = 0; i < 6; i++){ // playa rejection sound
int fail_noteDuration = 1000/fail_noteDurations[i];
tone(speaker_pin, fail_melody[i],fail_noteDuration);
int fail_pauseBetweenNotes = fail_noteDuration * 1.30;
delay(fail_pauseBetweenNotes);
noTone(speaker_pin);
}
delay(1000);
digitalWrite(LED_intruder, LOW); // turn off the red LED
}
if (ealdsem_card || emodsem_card){// add another user using an logical or condition ||
// Welcome messages and access permission
Serial.println("Access Granted!... Welcome!"); // print a message
digitalWrite(LED_access,HIGH); // turn on the green LED
doorLock.write(180); // releases the door
Serial.println("This Door will automatically lock in 5 seconds!");
delay(5000); // wait
doorLock.write(0); // Locks the door
digitalWrite(LED_access,LOW); // turn off the green LED
}
Serial.println();
delay(500);
rfid.halt();
}
}