RFID Protected Mini-Fridgs (updated)

Can a mod please put this as post #2?

please note that i have FF'f and 69'd the unique bytes in my two tags.

you unique bytes would go there!

Also, this is actually a pretty small program

Binary sketch size: 6282 bytes (of a 30720 byte maximum)
/**
 * AUTHOR   : DarthTater
 * PURPOSE  : Control electromechanical lock on dorm room mini fridge
 * VERSION  : 2.8AF
 * LICENSE  : Completely open, be sure to credit me and Benjamin. Don't blame us for things this code does!
 *
 *
 * This is very loosely based on the work of Benjamin Eckel (http://www.gumbolabs.org/2009/10/17/parallax-rfid-reader-arduino/)
 * Some of his RFID handling code was used and modified to make more modular
 *
 * CIRCUIT:
 *  RFID VCC ==> ARDUINO VCC
 *  RFID ENA ==> ARDUINO 2 (SERIAL ENABLE)
 *  RFID SOT ==> ARDUINO 0 (GPIO)
 *  RFID GND ==> ARDUINO GND
 *
 *  SERVO POS ==> ARDUINO 5
 */

// INCLUDES
#include <Servo.h>               // This is used to  open the door's lock

//////////// Definitions ////////////


// RFID
#define RFID_Enable 2            // define the pin for RFID ENABLE
#define RFID_codeLength 10       // length of RFID tag (IN BYTES)
#define RFID_tagValiddate 1      // if set to 1, tag will be 'validated' by checking to makes sure we get the same UID twice
#define RFID_validateLength 200  // maximum time to wait for a second pass of the same tag
#define RFID_Pause 2000          // length of time that we set the reader to disable
#define RFID_startByte 0x0A      // this is the start of the serial string
#define RFID_stopByte 0x0D       // this is the end of the serial string

// SERVO
#define SERVO_Pin 5              // this is the servo's home!
#define SERVO_Open 155           // this is the position for open
#define SERVO_Closed  10         // this is the position for closed
#define SERVO_Delay  2500        // this is the amount of time to hold the lock open
#define SERVO_MiniDelay 10       // This is the number of miliseconds we wait before incrementing the servo's position.

// DEBUG
int ledPin = 13;

// SECURITY
boolean FirstRun;






////////////////////////////////////////////////////////////////////////////////////////////

////////// ALLOWED TAGS ////////// 
// put all tags you want to ALLOW in the arrays below \\



byte legit1[] = {
  0x31, 0x42, 0x30, 0x30, 0x38, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};


byte legit2[] = {
  0x31, 0x42, 0x30, 0x30, 0x38, 0x69, 0x69, 0x69, 0x69, 0x69};
////////////////////////////////////////////////////////////////////////////////////////////


// we will store the serial string (one byte at a time) into an array
byte tag[RFID_codeLength];       

// we will need a servo object
Servo lock;

void setup()
{ 
  // lets set up our serial for PC use  
  Serial.begin(2400);  // This is our PC connections
  Serial.println("PC Serial Should be ALIVE");
  BlinkLED(1);

  pinMode(RFID_Enable, OUTPUT);  //sets a single pin to high so we can tell the reader to activly search for tags... this will que up a red LED

  FirstRun = false;

  // blink the LED a few times to indicate startup is done.
  Serial.println("we have lift off!");
  RFID_SETUP_BLINK();
  Serial.println("SETUP and BLINKING are DONE!");

  lock.attach(SERVO_Pin);  // configure our servo pin

    BlinkLED(0);

}


void loop() { 

  Serial.println("Entering the LOOP");
  Serial.print("locking servo at position : ");
  Serial.println(SERVO_Closed);
  Serial.print("we are set to open at position : ");
  Serial.println(SERVO_Open);

  // we do this in the event of power loss.  the servo goes to 1/2 way (90 deg)
  // the if statement makes sure it is only done ONCE per power cycle. 
  // as it is now, a bad tag jumps to the top of the loop which will cause this to run
  // which will open the lock enough to forcefully pull it out!  


  if(FirstRun = true){
    for (int post = 90; SERVO_Closed <= post; post -=1)
    {
      lock.write(post);
      delay(5);
    }
    FirstRun = false;
  }

  Serial.println("SERVO IS LOCKING");

  BlinkLED(1);
  delay(500);
  BlinkLED(0);
  delay(500);
  BlinkLED(1);
  delay(500);
  BlinkLED(0);
  delay(500);
  BlinkLED(1);
  delay(500);
  BlinkLED(0);


  // Just to be sure, we are going to set the reader to ENABLE / RED
  RFID_Status(1);

  // lets open up and scan for tags

  Serial.println("checking for tags...");

  getRFIDTag();

  Serial.println("tag has been found");

  if(isTagValid){

    RFID_Status(0);     // if the code is valid, disable the reader for just a second (this also shows a green light)
    Serial.println("Reader is GREEN");

    sendCode();         // WE should send the code to the PC, if it is valid
    Serial.println("Tag is SENT - - - - - - AUTHORIZING");

    RFID_Status(1);     // go back to red for a second


    if(checkTagForAuth()){

      RFID_Status(0);  // go green

      Serial.println("Tag was good, AND IS GOOD!");

      Serial.println("Attempting to OPEN LOCK");
      Lock(0);
      BlinkLED(1);
      Serial.println("Should be open! NOW ENTERING DELAY...");
      delay(SERVO_Delay);

      Serial.println("Delay is over, now closing");
      Lock(1);
      BlinkLED(1);
      delay(200);
      BlinkLED(0);
      delay(200);
      BlinkLED(1);


    }
    else {

      Serial.println("Tag was good, but is not authed!"); 
      RFIDErrorBlink();
      BlinkLED(1);
    }


  } 
  else {

    Serial.println("sorry, NOISE"); 
    RFIDErrorBlink();  // blink the led a series of red / greens to indicate a problem
    BlinkLED(1);

  }

  delay(2000);  // We should just give everything a second or two to clear out;
  Serial.flush();    // clear the buffer for serial port.
  clearTagArr();    // clear out the array

}


/**************************************************************/
/**********************    Functions  *************************/
/**************************************************************/

//
// This is an infinite loop function, waits for available serial data, then checks it for validity, then fills the buffer if it is
// vaid.
//

void getRFIDTag() {

  // a temporary polace holder
  byte next_byte;
  while(Serial.available() <= 0) {

    // Do NOTHING while we don't have data coming in!

  }


  // When we do have data coming in, we process it below.
  // Check to see if we've got a PC instruction or not

  // fill our temporary buffer
  next_byte = Serial.read();

  // Check our buffer to see if it's valid.
  if(next_byte == RFID_startByte) {

    byte bytesread = 0;  // if we're staring with a valid character, start counting as we fill the buffer

    while(bytesread < RFID_codeLength) {
      if(Serial.available() > 0) {    // wait for the next byte

        if((next_byte = Serial.read()) == RFID_stopByte) break;  // if weve got a stop byte, then lets wrap up, else, heep filling the array.
        tag[bytesread++] = next_byte;               

        delay(1);
      }
    }                
  }    
}


//
//  This is a simple call to adjust the status (and led color) of the reader.
//
void RFID_Status(int S){
  if (S==1){
    // pin should be RED
    digitalWrite(RFID_Enable, LOW);    // tell the reader to start scanning
  }
  if (S==0){
    // let shoudl be GREEN
    digitalWrite(RFID_Enable, HIGH);    // tell the reader to stop scanning
  }

}


//
//  This is just a simple set of code to bink the led; its put here to clear up space above.
//

void RFID_SETUP_BLINK(){

  RFID_Status(1);  // RED
  delay(300);
  RFID_Status(0);  // GRN
  delay(300);
  RFID_Status(1);  // RED
  delay(300);
  RFID_Status(0);  // GRN
  delay(300);
  RFID_Status(1);  // RED
}


//
// clears the buffer array 
//

void clearTagArr() {
  for(int i=0; i<RFID_codeLength; i++) {
    tag[i] = 0; 
  }
}

//
// Sends the code to the PC
//

void sendCode() {
  Serial.print("TAG (HEX) : ");  
  for(int i=0; i<RFID_codeLength; i++) {
    Serial.print(tag[i] , HEX); 
    Serial.print("-");
  } 
  Serial.println();
}
//
// this is essentially a double check of the buffer
//

boolean isTagValid() {
  // Temp
  byte next_byte; 
  int count = 0;

  // We've already got 2 bytes in the buffer, its proably a stop byte
  while (Serial.available() < 2) {
    delay(2); // a very short wait, not too accurate and not too important

    // a REALLY simple check to see if the new count is the old;  if it's not, we clearly have garbage
    if(count++ > RFID_validateLength) return false;
  }

  Serial.read(); // read from the buffer and throw it to nothing; get rid of the stop byte

  // Is the new tag valid?  does it have a legit start bit?
  if ((next_byte = Serial.read()) == RFID_startByte) {  

    // Temp buffer (again)
    byte bytes_read = 0; 

    // reading the buffer and checking it for stop bytes and comparing them.
    while (bytes_read < RFID_codeLength) {
      if (Serial.available() > 0) { //wait for the next byte      
        if ((next_byte = Serial.read()) == RFID_stopByte) break;
        if (tag[bytes_read++] != next_byte) return false;                     
      }
    }                
  }
  return true;   
}


//
// indicate an error in reading the tag
//
void RFIDErrorBlink(){

  // blink the led to let user know there was noise on the line...
  RFID_Status(1);  // RED
  delay(200);
  RFID_Status(0);  // GREEN
  delay(200);
  RFID_Status(1);  // RED
  delay(250);
  RFID_Status(0);  // GREEN
  delay (250);
  RFID_Status(1);  // RED

}