Skip to content

Instantly share code, notes, and snippets.

@arduinoboard
Created March 28, 2012 00:06
Show Gist options
  • Save arduinoboard/2221672 to your computer and use it in GitHub Desktop.
Save arduinoboard/2221672 to your computer and use it in GitHub Desktop.
The file that is currently on an Arduino Mega 2560 or Mega ADK with a serial number of CC2B6802ERDG6ML0
#include <avr/pgmspace.h>
#define TAG_LEN 12 // length of a single tag in bytes
int ledPin = 13; //feedback led
int ledPin2 = 6; //feedback led
int checkrfid = 0; // after a scan for tags, this will become: 0 if no tag was read, 1 if tag is recognized, 2 if tag is not recognized,
byte code[TAG_LEN]; // var that will hold the bytes-in read from the serialBuffer
int bytes_read = 0; // number of bytes read
PROGMEM prog_char target_tag[][TAG_LEN] = //dynamic multidimensional array with known tags. It's stored into flash memory to overcome the 70 tag limit.
{{0x00, 0x00, 0xdd, 0x8e, 0x96, 0x0e, 0x00, 0x01, 0x04, 0xe0, 0x10, 0xc8 },
{0x00, 0x00, 0x1b, 0x4e, 0xc1, 0x0d, 0x00, 0x01, 0x04, 0xe0, 0xd5, 0x7b}};
void setup() {
// define pin modes for tx, rx, led pins:
pinMode(ledPin, OUTPUT); // setup for feedback led
pinMode(ledPin2, OUTPUT); // setup for feedback led
Serial.begin(19200); // opens serial port, sets data rate to 19200 bps
Serial3.begin(19200);
}
void loop ()
{
scan(); // do a scan for tags
if(checkrfid == 1) // if tag is recognized
{
fade(); //do function "fade" (see below)
delay(100);
}
if(checkrfid == 2) // if tag is not recognized
{
blink(); //do function "blink" (see below)
}
delay(100); //wait before doing a new scan
}
void scan () {
checkrfid = 0; //set "state of recogniton" back to zero
Serial3.write(0xfa);
//mySerial.print(0xfa, BYTE); //request Tag code
if(Serial3.available()){ //if data is coming back from the reader
while (Serial3.available() && bytes_read < TAG_LEN){ //while data is coming back from the reader and not all 12 bytes are read
code[bytes_read] = Serial3.read(); //add byte
bytes_read++; // update number of bytes read
}
}
Serial.println ("NO TAG "); //No data has come from the reader so "No Tag" is displayed
if( bytes_read >= TAG_LEN) //if 12 bytes were read, print them out
{
Serial.print("{"); //adds syntax so you can just copy-paste the tag into your code
for(int i=0; i<bytes_read; i++) //prints all 12 bytes one at a time
{
Serial.print("0x"); //more syntax for copy-paste
Serial.print(code[i], HEX); //print out the byte in HEX
Serial.print(", "); //more syntax for copy-paste
}
Serial.print("},"); //more syntax for copy-paste
for(int i = 0; i < ((sizeof(target_tag))/12); i++) //tag is compared to array of known tags, note that the size of the array is dynamic and calculated every time, more ease for copy-paste...
{
if(memcmp_P(code, target_tag[i], TAG_LEN) == 0 ) //uses the PROGMEM variant of memcmp -> memcmp_P This function doesn't work with the standard Arduino 009 IDE, Needs avr-libc 1.4.6.
{
checkrfid = 1; //if tag matches, set "checkrfid" to 1
}
}
if(checkrfid == 0) //if tag doesn't match, set "checkrfid" to 2
{
checkrfid = 2;
}
Serial.flush(); //flush serial buffer
}
Serial.println(); //print linefeed
bytes_read = 0; //set number of bytes read back to zero
}
void blink ()
{
digitalWrite(ledPin, HIGH); // if true blink led
delay(1000);
digitalWrite(ledPin, LOW);
delay(500);
}
void fade ()
{
int value = 0;
for(value = 0 ; value <= 255; value+=5) // fade in (from min to max)
{
analogWrite(ledPin2, value); // sets the value (range from 0 to 255)
delay(30); // waits for 30 milli seconds to see the dimming effect
}
delay(200);
for(value = 255; value >=0; value-=5) // fade out (from max to min)
{
analogWrite(ledPin2, value);
delay(30);
}
delay(500);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment