Combined 2 sketchs cat door SUCCESS!

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.

++Karma; // For sharing your success, well done!

After several hours I almost give up and admit that coding is beyond my understanding.

It took me a LOT longer than several hours to learn C, and I had the luxury of 2 friends to help me and I already knew how to write assembly language for Z80 and PIC. I'm glad you didn't give up! Now move on to your next project and learn some more. When you've done that come back to this one, go through your code and see how you could improve it with the new stuff you've learned, you'll be surprised.

"Hope this helps someone."

I have duct taped a chop stick across the outside of the cat flap. Cat likes to bring in live and half eaten catches which can cause issues, especially if I don't know about them until later. Cat can go out anytime, but he has learned to bang on the flap until I come let him in. Cats can learn pretty quick when they need to.

EdgSam:
Hope this helps someone.

What does the door look like?
Is it store bought or did you build it yourself?

I have some friends with cats so this would be useful.

zoomkat:
I have duct taped a chop stick across the outside of the cat flap.

Can you explain what you mean by this?
I am having a hard time picturing it.

What does the door look like?
Is it store bought or did you build it yourself?

I built the cat door about 4 months ago. Made with piece of plexiglass and pvc hinge left over from golf cart windshield project. Cut into man door with aluminum trim covering raw edges. It goes into detached garage. Racoons started becoming a problem about 3 months ago.
Still working on lock system, that will be easy but wanted to get programming done first.

ieee488:
Can you explain what you mean by this?
I am having a hard time picturing it.

The chop stick is taped horizontally on the outside face of the cat flap, with the ends of the chop stick extending past the edges of the flap and resting against the flap frame. The flap can swing out, but can't swing in because the chop stick ends hit the flap frame. Cat check valve.

"Still working on lock system, that will be easy but wanted to get programming done first."

You could probably use a servo with an arm horn, attached to the door inside below the cat flap. Normally the arm would be up, blocking the in swing of the door. When the cat s near, the servo arm rotates 90 deg. unblocking the door.

zoomkat:
Cats can learn pretty quick when they need to.

But more often than not, only when they need to. :grinning:

You could probably use a servo with an arm horn, attached to the door inside below the cat flap. Normally the arm would be up, blocking the in swing of the door. When the cat s near, the servo arm rotates 90 deg. unblocking the door.

That's a good idea zoomkat, but racoons out here are pretty smart. They use their claws to hook flap and pull towards them. Am designing a U clamp lock to block it in both directions. Using lever and solenoid.

And they would chew through chop sticks. They're tough here. Haha

Got the lock mechanically working. and everything seams to be going as planned EXCEPT, rfid tag is being read multiple times. It sends signal to relay (lock solenoid), waits programmed 20 second delay then repeats the cycle 2, 3 even 4 times depending on how long tag is in range. There is a line in the sketch "RFID.flush(); // stops multiple reads" put that isn't working.

Need some way to read only once while in range.

After some research thought a millis() would do the trick. Found this reply by MarkT in a post where someone else was having the same problem put don't know how to edit it properly for my sketch or where to insert it.

What is happening is that many packets are being queued up in the SoftwareSerial buffer
while you are executing delay(20000);

You need to use a state machine and use millis(), not delay(), in order to allow these multiple requests
to be read, processed, and ignored while the LED is on.

So reading a request should change state to LED on, and if it wasn't already on record the start_time.

If millis() - start_time >= 20000, then transition to led OFF state.

This means that the requests get handled immediately, and effectively ignored while the LED is still on.

Been trying to include this command for hours with no success. Can someone point me in the direction to figure this out?

State machine is the answer. Search the site for examples. I think there's a thread in the introductory tutorials section too.

Be warned though - State machines are simple things, but they're a concept that almost everyone struggles with to start with. Eventually, you see the light, but there's an almost compulsory period of pain and suffering you have to go through first.

A simpler way might be that when you open the lock, record millis. Loop around reading data from the RFID and throw it away for 20,000 mS. Then lock the door. Then loop and throw away for some additional period to let the cat get out of range.

zoomkat:
The chop stick is taped horizontally on the outside face of the cat flap, with the ends of the chop stick extending past the edges of the flap and resting against the flap frame. The flap can swing out, but can't swing in because the chop stick ends hit the flap frame. Cat check valve.

Love it!

Ingenious low-tech. :slight_smile:

"Ingenious low-tech."

The cat is smart in that it learned that batting the flap with his paw making some noise, I will come and let him in.

A simpler way might be that when you open the lock, record millis. Loop around reading data from the RFID and throw it away for 20,000 mS. Then lock the door. Then loop and throw away for some additional period to let the cat get out of range.

HUH?.....Haha

Remember the first line of this topic says "extreme newbie".

But really thank you wildbill, that gives me a place to start.

Thinking the unlock repeats are gonna keep happening for a while.

Was able to stop the multiple reads using millis(). Explained how in topic:

Stopped RFID multiple read/unlocks using Millis()

Search for it.

Thanks to everyone who helped me through.