So I'm still having fun playing with the arduino and the parallax RFID reader, even if it won't do exactly what I'm after. Here's the latest code that does the following.
Has a single tag id stored as tag.
Reads tags and outputs debug code to serial
Lights green LED if tag is recognized
Lights red LED if not recognized
After set amount of time without reading known tag, sounds a piezo buzzer and flashes red LED
The piezo is connected - positive to digital pin 7 and negative to breadboard ground
#define TAG_LEN 12
char tag[12] = {'0', '1', '0', '3', '6', 'A', 'F', '2', 'B', '1'};
char code[12];
int bytesread = 0;
int redPin = 13; // Connect red LED to pin 13
int grnPin = 8; // Connect green LED to pin 8
int buzPin = 7; // Connect buzzer to pin 7
int rfidPin = 2; // RFID enable pin connected to digital pin 2
int val=0;
long time;
long lastUpdate=0;
void setup() {
Serial.begin(2400); // RFID reader SOUT pin connected to Serial RX pin at 2400bps
pinMode(rfidPin,OUTPUT); // Set digital pin 2 as OUTPUT to connect it to the RFID /ENABLE pin
pinMode(redPin,OUTPUT); // Set ledPin to output
pinMode(grnPin,OUTPUT);
pinMode(buzPin,OUTPUT);
digitalWrite(rfidPin, LOW); // Activate the RFID reader
}
void loop() {
digitalWrite(buzPin,0);
time = millis();
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
if(strcmp(code, tag) == 0) {
Serial.print("Tag matches: ");
Serial.println(code);
lastUpdate = millis();
Serial.print("time elapsed = ");
Serial.println(time);
digitalWrite(grnPin,HIGH); // good read light green LED
digitalWrite(redPin,LOW); // turn off red LED
}
else {
Serial.print(code);
Serial.println(" does not match");
digitalWrite(redPin,HIGH); // not recognized tag light red LED
digitalWrite(grnPin,LOW); // turn off green LED
}
}
bytesread = 0;
delay(500); // wait for a 1/2 second
}
}
if((time - lastUpdate) >= 60000) {
blink_fast();
}
}
void blink() {
digitalWrite(redPin, HIGH);
delay(250);
digitalWrite(redPin, LOW);
delay(250);
}
void blink_fast() {
digitalWrite(buzPin,HIGH); //play annoying buzzer :)
Serial.print("Time = ");
Serial.println(time);
digitalWrite(grnPin,LOW); //turn off green led
digitalWrite(redPin, HIGH);
delay(50);
digitalWrite(redPin, LOW);
delay(50);
}
// extra stuff
// digitalWrite(2, HIGH); // deactivate RFID reader
Dale