]> git.piffa.net Git - arduino/blob - books/pdummies/APFD_Chapter9_RFID_Reader/APFD_Chapter9_RFID_Reader.ino
first commit
[arduino] / books / pdummies / APFD_Chapter9_RFID_Reader / APFD_Chapter9_RFID_Reader.ino
1 /* Arduino Projects for Dummies
2  * by Brock Craft 
3  *
4  * Chapter 9: Building an RFID Tag Reader
5  * A system that Reads and matches RFID tag to trigger a relay
6  *
7  * v0.1 30.04.2013
8  * Adapted from bildr.com
9 */
10
11
12 const int relayPin = 11;      // The pin the relay is connected to
13 const int ledPin = 12;        // The pin the indicaor LED is connected to
14 const int RFIDResetPin = 13;  // This pin tells the reader to read again
15 const int relayOntime = 5000; // The time to leave the relay on, in ms.
16
17 // Make a list of your RFID tags here.
18 // If you don't know them, leave at least one in place as starter
19 // a starter ID and delete it later. Otherwise the code will break.
20 // (You always have to have something to compare the read tags to.)
21
22 char tag1[13] = "4B00DDBF9FB6";  // Your scanned tag ID goes here
23 char tag2[13] = "010203AABBCC";  // these are example Iag IDs only
24 char tag3[13] = "010203AABBDD";  // these are example Iag IDs only
25 // etc. for more tags
26
27 void setup(){
28   Serial.begin(9600);               // Start the serial connection to TX/RX data
29   pinMode(RFIDResetPin, OUTPUT);    // Tells the reader to start again
30   pinMode(ledPin, OUTPUT);          // Set the LED pin to output
31   pinMode(relayPin, OUTPUT);          // Set the LED pin to output
32   digitalWrite(RFIDResetPin, HIGH); // Make it ready to read
33   Serial.println("Ready.");         // Advise the consoe we are ready.
34 }
35
36 void loop(){
37
38   Serial.println("Looking for a tag..."); // Say what we are currently doing
39   char tagString[13];      // Create an array to hold the tag we are reading
40   int index = 0;           // A utility counter to track where we are in tagstring[]
41   boolean reading = false; // Stores whether we have a reading
42
43   while(Serial.available()){  // If there is a serial connection...
44
45     int readByte = Serial.read();      // Read next available byte
46     if(readByte == 2) reading = true;  // 2 indicates the begining ofa tag
47     if(readByte == 3) reading = false; // 3 indicates the end of tag
48     
49     // If there is a reading and it's not the beginning or end, store it
50     if(reading && readByte != 2 && readByte != 10 && readByte != 13){
51      
52       tagString[index] = readByte;  // Store the tag at the index point
53       index ++;                     // increment where we are in the storage array
54     }
55   }
56
57   checkTag(tagString); // Check if it is a match
58   clearTag(tagString); // Clear the char array of all values
59   resetReader();       // Reset the RFID reader
60
61 }
62
63 void checkTag(char tag[]){
64
65   // Compare this tag to the stored tags
66   if(strlen(tag) == 0) return; // If there's nothing here, do nothing
67
68   if(compareTag(tag, tag1)){   // If read tag matches tag1, do this
69     lightLED(); 
70     triggerRelay();
71   }
72   else if(compareTag(tag, tag2)){ // If read tag matches tag2, do this
73     lightLED();                   // You could have any function here!
74   }
75   else if(compareTag(tag, tag3)){ // If read tag matches tag3, do this
76     lightLED();
77   } else {                        // If it doesn't match the list of tags...
78     Serial.println("New tag found: "); 
79     Serial.println(tag);         // Print the tag number of this new tag 
80     delay(5000);                 // Wait 5 seconds, so we can make note of it
81   }
82
83 }
84
85 boolean compareTag(char one[], char two[]){
86   // Compare our two chars to see whether the tag
87   // we just read is the same as the stored tag
88   
89   if(strlen(one) == 0) return false; // If there's nothing here, do nothing
90
91   for(int i = 0; i < 12; i++){
92     if(one[i] != two[i]) return false; // If the two tags are are not the same, return no match
93   }
94   Serial.println("Valid tag found!");
95   return true; // Otherwise, return that there is a match!
96 }
97
98 void lightLED(){
99   digitalWrite(ledPin, HIGH);  // Turn on the ledPin
100   delay(250);                  // Wait a moment
101   digitalWrite(ledPin, LOW);   // Turn off the ledPin
102 }
103
104 void triggerRelay(){
105   digitalWrite(relayPin, HIGH);  // Turn on the ledPin
106   delay(relayOntime);            // Wait a moment
107   digitalWrite(relayPin, LOW);   // Turn off the ledPin
108 }
109
110 void clearTag(char one[]){
111   // Clear the tag reading char array by filling it with ASCII 0
112   // If not null, it could indicate another tag read
113   for(int i = 0; i < strlen(one); i++){
114     one[i] = 0; 
115   }
116
117   
118 void resetReader(){
119   // Toggle the reset pin so the RFID reader will read again
120   digitalWrite(RFIDResetPin, LOW);
121   digitalWrite(RFIDResetPin, HIGH);
122   delay(150);
123 }
124
125