Hello, I'm an extreme newbie. Been attempting to build electronic cat door. Purpose is to let our cat (Shadow) in and out of garage and not @#%$! racoons. The mechanics of the project is easy for me but the Arduino code is not. Been researching for weeks, my mind can grasp the basic programming but not the specific programming needed to make this work.
Desired scenario as follows.
Shadow approaches from outside wearing rfid tag, Door unlocks, Shadow enters, Door locks 20 seconds later.
Shadow approaches from inside, Door unlocks and stays unlocked for about 30 seconds, Door locks.
During research I stumbled across a 2 Arduino sketchs that work,1 for entering and 1 for exiting. Sorry but I did not write down were I found them. Wish I did, would like to give credit where it's due.
Then started research on how to combine them. After several hours I almost give up and admit that coding is beyond my understanding. Can plan, design and build almost anything mechanically but not code. [sigh]
But them, some how I was successful! Just dumb luck I guess.
Thought this might help someone out there.
Components in project:
*Arduino UNO r3
*Seeed RDM630 125Khz RFID module-UART (pin 2, INPUT)
*125Khz rfid tag
- 5v 1-channel Relay (to trigger solenoid for door lock) (pin 9, OUTPUT)
*HC-SR501 PIR Motion Detector (pin 6, INPUT)
Here is the RFID sketch
#include <SoftwareSerial.h>
SoftwareSerial RFID(2, 3); // RX and TX
int data1 = 0;
int ok = -1;
int yes = 9;
int no = 11;
// use first sketch in http://wp.me/p3LK05-3Gk to get your tag numbers
int tag1[14] = {2,51,49,48,48,66,49,67,48,68,52,57,52,3};
int tag2[14] = {2,52,48,48,48,56,54,67,54,54,66,54,66,3};
int newtag[14] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0}; // used for read comparisons
void setup()
{
pinMode(13, OUTPUT);
RFID.begin(9600); // start serial to RFID reader
Serial.begin(9600); // start serial to PC
pinMode(yes, OUTPUT); // for status LEDs
pinMode(no, OUTPUT);
}
boolean comparetag(int aa[14], int bb[14])
{
boolean ff = false;
int fg = 0;
for (int cc = 0 ; cc < 14 ; cc++)
{
if (aa[cc] == bb[cc])
{
fg++;
}
}
if (fg == 14)
{
ff = true;
}
return ff;
}
void checkmytags() // compares each tag against the tag just read
{
ok = 0; // this variable helps decision-making,
// if it is 1 we have a match, zero is a read but no match,
// -1 is no read attempt made
if (comparetag(newtag, tag1) == true)
{
ok++;
}
if (comparetag(newtag, tag2) == true)
{
ok++;
}
}
void readTags()
{
ok = -1;
if (RFID.available() > 0)
{
// read tag numbers
delay(100); // needed to allow time for the data to come in from the serial buffer.
for (int z = 0 ; z < 14 ; z++) // read the rest of the tag
{
data1 = RFID.read();
newtag[z] = data1;
}
RFID.flush(); // stops multiple reads
// do the tags match up?
checkmytags();
}
// now do something based on tag type
if (ok > 0) // if we had a match
{
Serial.println("Accepted");
digitalWrite(yes, HIGH);
delay(20000);
digitalWrite(yes, LOW);
ok = -1;
}
else if (ok == 0) // if we didn't have a match
{
Serial.println("Rejected");
digitalWrite(no, HIGH);
delay(1000);
digitalWrite(no, LOW);
ok = -1;
}
}
void loop()
{
readTags();
}
Here is the motion sketch
#define led 13
#define buzzer 9
#define pirSensor 6
void setup() {
Serial.begin(9600);
pinMode(pirSensor, INPUT);
pinMode(led, OUTPUT);
pinMode(buzzer, OUTPUT);
}
void loop() {
int x = digitalRead(pirSensor);
if (x == LOW)
{
digitalWrite(led, LOW);
digitalWrite(buzzer, LOW);
Serial.println(x);
}
else
{
digitalWrite(led, HIGH);
digitalWrite(buzzer, HIGH);
Serial.println(x);
}
}
And the combined sketch that worked for me here.
#include <SoftwareSerial.h>
SoftwareSerial RFID(2, 3); // RX and TX
int data1 = 0;
int ok = -1;
int yes = 9;
int no = 11;
#define led 13
#define pirSensor 6
// use first sketch in http://wp.me/p3LK05-3Gk to get your tag numbers
int tag1[14] = {2, 51, 49, 48, 48, 66, 49, 67, 48, 68, 52, 57, 52, 3};
int tag2[14] = {2, 52, 48, 48, 48, 56, 54, 67, 54, 54, 66, 54, 66, 3};
int newtag[14] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; // used for read comparisons
void setup()
{
pinMode(13, OUTPUT);
RFID.begin(9600); // start serial to RFID reader
Serial.begin(9600); // start serial to PC
pinMode(yes, OUTPUT); // for status LEDs
pinMode(no, OUTPUT);
pinMode(pirSensor, INPUT);
}
boolean comparetag(int aa[14], int bb[14])
{
boolean ff = false;
int fg = 0;
for (int cc = 0 ; cc < 14 ; cc++)
{
if (aa[cc] == bb[cc])
{
fg++;
}
}
if (fg == 14)
{
ff = true;
}
return ff;
}
void checkmytags() // compares each tag against the tag just read
{
ok = 0; // this variable helps decision-making,
// if it is 1 we have a match, zero is a read but no match,
// -1 is no read attempt made
if (comparetag(newtag, tag1) == true)
{
ok++;
}
if (comparetag(newtag, tag2) == true)
{
ok++;
}
}
void readTags()
{
ok = -1;
if (RFID.available() > 0)
{
// read tag numbers
delay(100); // needed to allow time for the data to come in from the serial buffer.
for (int z = 0 ; z < 14 ; z++) // read the rest of the tag
{
data1 = RFID.read();
newtag[z] = data1;
}
RFID.flush(); // stops multiple reads
// do the tags match up?
checkmytags();
}
// now do something based on tag type
if (ok > 0) // if we had a match
{
Serial.println("Accepted");
digitalWrite(yes, HIGH);
delay(20000);
digitalWrite(yes, LOW);
ok = -1;
}
else if (ok == 0) // if we didn't have a match
{
Serial.println("Rejected");
digitalWrite(no, HIGH);
delay(1000);
digitalWrite(no, LOW);
ok = -1;
}
}
void loop() {
{
readTags();
}
int x = digitalRead(pirSensor);
if (x == LOW)
{
digitalWrite(led, LOW);
digitalWrite(yes, LOW);
Serial.println(x);
}
else
{
digitalWrite(led, HIGH);
digitalWrite(yes, HIGH);
Serial.println(x);
}
}
To get tag number I used this sketch.
#include <SoftwareSerial.h>
SoftwareSerial RFID(2, 3); // RX and TX
int i;
void setup()
{
RFID.begin(9600); // start serial to RFID reader
Serial.begin(9600); // start serial to PC
}
void loop()
{
if (RFID.available() > 0)
{
i = RFID.read();
Serial.print(i, DEC);
Serial.print(" ");
}
}
Hope this helps someone.