Greetings and salutations, and a Happy New Year to all :D.
Just wanted to post my sketch here for some general comments on my choices.
I have the following connected to my Arduino Uno:
1x Arduino Sensor Shield (It was on there when I started playing, not really using it...)
2xRFID-RC522
1x I2C RTC DS1307 AT24C32 Real Time Clock Module
3xLED to simulate locks
When I wave an RFID card in front of either RFID reader, it opens the lock (switches on a LED) and use scheduler to keep the lock open for 30 seconds, and then locks it again (switches the LED off). This will probably change going forward, depending on what type of door lock system I go for.
I also use the data from the RTC to log which card serial openend which lock at what time.
It all seems to work ok, but it needs some cleaning up of the code and is constantly under development. I am waiting for a microSD card module that I will use to write a logfile, and also keep a file with the allowed cards. That is the plan anyways ;).
My sketch:
#include <Scheduler.h>
#include <RFID.h>
#include <SPI.h>
#include <DS1307RTC.h>
#include <Time.h>
#include <Wire.h>
#define lock1_pin 2
#define lock2_pin 3
#define lock3_pin 4
#define rfid_reader_1_ss_pin 6
#define rfid_reader_1_rst_pin 5
#define rfid_reader_2_ss_pin 8
#define rfid_reader_2_rst_pin 7
#define rfid_reader_3_ss_pin 10
#define rfid_reader_3_rst_pin 9
//setup variables
int reader1cardreadtime;
int reader2cardreadtime;
int reader3cardreadtime;
int reader1active;
int reader2active;
int reader3active;
int timetolockagain;
//some other stuff
RFID rfid1(rfid_reader_1_ss_pin,rfid_reader_1_rst_pin);
RFID rfid2(rfid_reader_2_ss_pin,rfid_reader_2_rst_pin);
RFID rfid3(rfid_reader_3_ss_pin,rfid_reader_3_rst_pin);
//creating schedules
Scheduler lock1_lock = Scheduler();
Scheduler lock2_lock = Scheduler();
Scheduler lock3_lock = Scheduler();
void setup()
{
Serial.begin(9600);
SPI.begin();
rfid1.init();
rfid2.init();
rfid3.init();
pinMode(lock1_pin, OUTPUT);
pinMode(lock2_pin, OUTPUT);
pinMode(lock3_pin, OUTPUT);
reader1active=0;
reader2active=0;
reader3active=0;
timetolockagain=30000;
}
void loop(){
//update scheduler to see if it is time
lock1_lock.update();
lock2_lock.update();
lock3_lock.update();
//Checking reader 1 if card has been presented
if (rfid1.isCard()) {
// Serial.print(reader1active);
//check if reader is already active
if (reader1active==1){} else {
// Check if serial can be read
if (rfid1.readCardSerial()) {
Serial.println(" ");
Serial.print("Reader 1: ");
digitalWrite(lock1_pin,HIGH);
Serial.print("Lock1 unlocked on: ");
timestamp();
Serial.print(" by card number: ");
Serial.print(rfid1.serNum[0],DEC);
Serial.print(rfid1.serNum[1],DEC);
Serial.print(rfid1.serNum[2],DEC);
Serial.print(rfid1.serNum[3],DEC);
Serial.print(rfid1.serNum[4],DEC);
Serial.println(" ");
reader1active=1;
lock1_lock.schedule(lock_lock1,timetolockagain);
}//stop check if serial can be read
}
//Checking reader 2
} else if (rfid2.isCard()) {
if (rfid2.readCardSerial()) {
// Serial.print(reader1active);
//check if reader is already active
if (reader2active==1){} else {
// Check if serial can be read
if (rfid2.readCardSerial()) {
Serial.println(" ");
Serial.print("Reader 2: ");
digitalWrite(lock2_pin,HIGH);
Serial.print("Lock2 unlocked on: ");
timestamp();
Serial.print(" by card number: ");
Serial.print(rfid2.serNum[0],DEC);
Serial.print(rfid2.serNum[1],DEC);
Serial.print(rfid2.serNum[2],DEC);
Serial.print(rfid2.serNum[3],DEC);
Serial.print(rfid2.serNum[4],DEC);
Serial.println(" ");
reader2active=1;
lock2_lock.schedule(lock_lock2,timetolockagain);
}
}
}
//Checking reader 3
} else if (rfid3.isCard()) {
if (rfid3.readCardSerial()) {
Serial.println(" ");
Serial.print("Reader 3: ");
digitalWrite(lock3_pin,HIGH);
Serial.print("Lock3 is unlocked");
delay(10000);
digitalWrite(lock3_pin,LOW);
Serial.print("Lock3 is locked");
}
}
rfid1.halt();
rfid2.halt();
rfid3.halt();
}
//Functions
//Function for stuff
void print2digits(int number) {
if (number >= 0 && number < 10) {
Serial.write('0');
}
Serial.print(number);
}
//Functions for locking locks again
void lock_lock1(){
digitalWrite(lock1_pin,LOW);
Serial.println(" ");
Serial.print("Lock1 was locked again at: ");
timestamp();
reader1active=0;
}
void lock_lock2(){
digitalWrite(lock2_pin,LOW);
Serial.println(" ");
Serial.print("Lock2 was locked again at: ");
timestamp();
reader2active=0;
}
void lock_lock3(){
digitalWrite(lock3_pin,LOW);
Serial.println(" ");
Serial.println("Lock3 was locked again at: ");
timestamp();
reader3active=0;
}
//Function for writing date and time in a specific format DD/MM/YYYY at HH:MM:SS
void timestamp(){
tmElements_t tm;
if (RTC.read(tm)) {
Serial.print(tm.Day);
Serial.write('/');
Serial.print(tm.Month);
Serial.write('/');
Serial.print(tmYearToCalendar(tm.Year));
Serial.print(" at ");
print2digits(tm.Hour);
Serial.write(':');
print2digits(tm.Minute);
Serial.write(':');
print2digits(tm.Second);
} else {
Serial.print('Unable to initialize RTC, check your system!');
}
}