Instructables

RFID Controlled Car Trunk

FeaturedContest WinnerContest Winner

Step 5: Understanding the Code

Picture of Understanding the Code
Thanks to an unknown code writer for most of this code!
/////////////////////////////////////////////
#include <Servo.h>  // Includes libraries (computer files) that include lines of code for the servo
Servo myservo; // Gives the servo a name

int RFIDResetPin = 0;

//Register your RFID tags here
char tag1[13] = "------------";  //First tag ID, I replaced the letters and numbers with a corresponding amount of dashes (i'd rather you              
                                                  //not hack my system...)

char tag2[13] = "------------";  //Second tag ID

void setup(){                        // everything within the void setup runs the code once on start-up 
  Serial.begin(9600);    //Sets up a connection to the computer when prototyping, incredibly useful to figure out whats happening in                      
                                        //the code because it can print lines of code onto a computer screen 
  myservo.attach(8);     //Attaches the servo to a specific pin on the arduino, this is like the lane of a highway for information 
  myservo.write(178);  // Sets the limited rotation (0-180 degrees) servo to 178, the idle position
  pinMode(RFIDResetPin, OUTPUT);  //Clears the slate for sensing a tag
  digitalWrite(RFIDResetPin, HIGH);    //puts it on "high alert"
 

}

void loop(){              //everything within the void loop is run through in a linear fashion over and over again 
 
  char tagString[0];                    //the next couple of lines work with the incoming data to turn it into numbers and letters which form
  int index = 0;                            //the ID tag 
  boolean reading = false;

  while(Serial.available()){               //while there is a serial connection (while the reader is communicating to the board...)

    int readByte = Serial.read(); //read next available byte          //code that begins decoding (haha) the binary 

    if(readByte == 2) reading = true; //begining of tag
    if(readByte == 3) reading = false; //end of tag

    if(reading && readByte != 2 && readByte != 10 && readByte != 13){
      //store the tag
      tagString[index] = readByte;
      index ++;
    }
  }

  checkTag(tagString); //Check if it is a match
  clearTag(tagString); //Clear the char of all value
  resetReader(); //reset the RFID reader
}

void checkTag(char tag[]){                   //Check the read tag against known tags


  if(strlen(tag) == 0) return; //empty, no need to contunue

  if(compareTag(tag, tag1)){                 // if matched tag1, do this
    servoGo();                                            //Activate the servo!
   
    Serial.println("card 1 read");             //print "card 1 read" to the computer, helps see if the code reaches this point when and how it
                                                                  //should
  }
/*
  else if(compareTag(tag, tag2)){           //if matched tag2, do this
   servoGo();                                                //Activate the servo!
   Serial.println("card 2 read");
  }*/
 
/* else{
    Serial.println(tag); //read out any unknown tag
    Serial.println("unknown card");
  }*/

}

void servoGo(){                                     
  Serial.println("Servo Activated");         //prints a line to the computer that tells me that its reaches this point 
    myservo.write(20);                               //writes the servo to a position that will pull the cord that pops the trunk
    delay(500);
    myservo.write(178);                            //resets the position for the next activation 
    delay(2000);
  Serial.println("Servo Action Complete");    //prints a line to the computer confirming the completion of this method (external code of
                                                                            //sorts)
}

/////////////////////////////////// The rest is code used to control the system that reads the code

void resetReader(){

//Reset the RFID reader to read again.

  digitalWrite(RFIDResetPin, LOW);
  digitalWrite(RFIDResetPin, HIGH);
  delay(150);
}

void clearTag(char one[]){

//clear the char array by filling with null - ASCII 0
//Will think same tag has been read otherwise

  for(int i = 0; i < strlen(one); i++){
    one[i] = 0;
  }
}

boolean compareTag(char one[], char two[]){

//compare two value to see if same,
//strcmp not working 100% so we do this


  if(strlen(one) == 0) return false; //empty

  for(int i = 0; i < 12; i++){
    if(one[i] != two[i]) return false;
  }

  return true; //no mismatches
}

//picture: http://www.thinkgeek.com/images/products/additional/large/wrapping_paper-binary.jpg
 
Remove these adsRemove these ads by Signing Up
Great place to start. I can see where it could be integrated into the car a little more. One could use the car's own battery, make sure your Arduino can take 14 volts or use a regulator. Most cars now have an electric trunk release so you could hook into that using a relay instead of the servo.
Heat sinks, good idea, I rarely use one though. I would recommend anyone new to soldering use one. I find if you move quickly you get the best joints. If your flux is smoking get the joint completed before it quits and don't be afraid to use lots.
Pro

Get More Out of Instructables

Already have an Account?

close

PDF Downloads
As a Pro member, you will gain access to download any Instructable in the PDF format. You also have the ability to customize your PDF download.

Upgrade to Pro today!