Skip to content

Instantly share code, notes, and snippets.

@ngerakines
Created September 30, 2012 22:44
Show Gist options
  • Save ngerakines/3808648 to your computer and use it in GitHub Desktop.
Save ngerakines/3808648 to your computer and use it in GitHub Desktop.
#include <Time.h>
int RFIDResetPin = 13;
//Register your RFID tags here
char tag1[13] = "84003378CA05";
char tag2[13] = "840033725D98";
char cat1name[5] = "Toby";
char cat2name[8] = "Biscuit";
int cat1last = 0;
int cat2last = 0;
int lastFeeding = 0;
int cat1amount = 0;
int cat2amount = 0;
int GLOBAL_DELAY = 15;
int FEED_DELAY = 30;
void setup(){
Serial.begin(9600);
pinMode(RFIDResetPin, OUTPUT);
digitalWrite(RFIDResetPin, HIGH);
//ONLY NEEDED IF CONTROLING THESE PINS - EG. LEDs
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
}
void loop(){
char tagString[13];
int index = 0;
boolean reading = false;
while(Serial.available()){
int readByte = Serial.read(); //read next available byte
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(); //eset the RFID reader
}
void checkTag(char tag[]) {
///////////////////////////////////
//Check the read tag against known tags
///////////////////////////////////
if(strlen(tag) == 0) {
return; //empty, no need to contunue
}
time_t t = now();
if (compareTag(tag, tag1)) {
if (canDispense(t, cat1last)) {
cat1last = t;
lastFeeding = t;
lightLED(2);
dispenseFood(cat1amount);
} else {
lightLED(3);
}
} else if (compareTag(tag, tag2)) {
if (canDispense(t, cat2last)) {
cat2last = t;
lastFeeding = t;
lightLED(2);
dispenseFood(cat2amount);
} else {
lightLED(3);
}
} else {
Serial.println(tag); //read out any unknown tag
}
delay(1100);
}
bool canDispense(time_t currentTime, int lastTime) {
if (currentTime <= GLOBAL_DELAY || currentTime <= FEED_DELAY) {
return false;
}
// if we've never fed before, then yes
if (lastFeeding == 0) {
return true;
}
// if we have fed before and it was more than 15 seconds ago
if (currentTime - GLOBAL_DELAY > lastFeeding ) {
if (lastTime == 0) {
return true;
}
// if the last time we fed this cat was over 30 seconds ago.
if (lastTime > 0 && currentTime - FEED_DELAY > lastTime) {
return true;
}
}
return false;
}
void dispenseFood(int amount) {
}
void lightLED(int pin){
///////////////////////////////////
//Turn on LED on pin "pin" for 250ms
///////////////////////////////////
Serial.println(pin);
digitalWrite(pin, HIGH);
delay(250);
digitalWrite(pin, LOW);
}
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
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment