Saturday, April 18, 2009

RFID cat door

Ok, it's been a while.  I apologize to all my dear fans.  Things have been going like gangbusters at work and I just didn't have time.  I mean, I've hardly been able to pick up my camera.

Anyway, I did take a break yesterday afternoon.  I had to, because I got an arduino, a parallax rfid antenna and some rfid tags in the mail.  You see, we need a better cat door and yesterday I got started on the KD2000 (Kitty Door 2000) by adapting the reader code from arduino.cc.  I've modified to make it work with my tags.



int  val = 0;
char code[12]; //The arduino.cc code shows a [10].  You may need to put [12] in here.  This solved a major frustration for me.
int bytesread = 0;
int ledPin = 3; //I wanted to prototype some interactivity, so I attached an led to digital pin 3.
/* In defining the variables for the tags, you'll want to list these as arrays separated by commas.  For example:
  char Biscuit[12] = "3600738183" didn't work for me.
I then revised that to do if(strcmp(code,Biscuit == 0)... That didn't work.  It was all in how I defined Biscuit as a string.  See below. */
char Biscuit[12] = {'3', '6', '0', '0', '7', '3', '8', '1', '8', '3'};       //These are obviously the tags.  With these arrays, the strcmp function worked.
char Marathon[12] = {'3', '6', '0', '0', '8', 'A', '9', '4', '4', '3'};
char Max[12] = {'3', '6', '0', '0', '5', 'C', '6', 'F', '3', '6'};
char Sage[12] = {'3', '6', '0', '0', '5', 'C', '2', 'F', 'B', '2'};
char Veeps[12] = {'3', '6', '0', '0', '5', 'B', '8', '6', 'C', '6'};


void setup() {

Serial.begin(2400); // RFID reader SOUT pin connected to Serial RX pin at 2400bps
pinMode(2,OUTPUT);   // Set digital pin 2 as OUTPUT to connect it to the RFID /ENABLE pin
digitalWrite(2, LOW);                  // Activate the RFID reader
pinMode(3,OUTPUT); // for the led on/off
}  


 void loop() {

  if(Serial.available() > 0) {          // if data available from reader
    if((val = Serial.read()) == 10) {   // check for header
      bytesread = 0;
      while(bytesread<10) {              // read 10 digit code
        if( Serial.available() > 0) {
          val = Serial.read();
          if((val == 10)||(val == 13)) { // if header or stop bytes before the 10 digit reading
            break;                       // stop reading
          }
          code[bytesread] = val;         // add the digit           
          bytesread++;                   // ready to read next digit  
        }
      }
 
      if(bytesread == 10) {              // if 10 digit read is complete
        Serial.print("TAG code is: ");   // possibly a good TAG
        Serial.println(code);     // print the TAG code        
        {
        if(strcmp(code,Biscuit) == 0) {        //This is my own code.  Not hard.  There's probably a way to do this all in one line by having the the if statement run through the list of tags.
          Serial.println("Biscuit, you're in");
        }
          if(strcmp(code,Marathon) == 0) {
            Serial.println("Marathon, you're in.");
          }
          if(strcmp(code,Max) == 0) {
            Serial.println("Max, you're in.");
          }
          if(strcmp(code,Sage) == 0) {
            Serial.println("Sagebrush, you're in.");
          }
          if(strcmp(code,Veeps) == 0) {
            Serial.println("Veeps, you're in.");
            digitalWrite(ledPin, HIGH);   // sets the LED on
            delay(250);                  // waits for a second
            digitalWrite(ledPin, LOW);    // sets the LED off
          }
          if((strcmp(code,Biscuit) !=0) && (strcmp(code,Marathon) != 0) && (strcmp(code,Max) !=0) && (strcmp(code,Sage) !=0) && (strcmp(code,Veeps) !=0)) {
          Serial.println("Illegal attempted entry");  //Any cat that shows up with an rfid will be denied and it will go on record.
          Serial.println(code);
      {
   
      }}}}
    
     
      bytesread = 0;
           delay(1000);                       // wait for a second
    }
  }
}

// extra stuff
// digitalWrite(2, HIGH);             // deactivate RFID reader

It took me a while, but now everything is working.  I know my engineering and math friends will be able to cut this down to 25% of its current length.  Nonetheless, the "proof of concept" (the proof of the concept that I could do this) worked.  Of course, lots of tweaking remains for the servo, a clock, and the mandatory network interface for twittering.  Luckily I've got friends to help do this--I'm going to need them.

Here's a screenshot from the arduino serial window:

 
 A virtual cat just came in the house.

The biggest challenge will be getting a better name for this thing.