// // aya karpinska :: spring 2007 :: for this we pray v13 // if any RFID card is detected (that has NOT been read within last 10 secs), // randomly select an LED that is not yet lit, and light it. // if this is the first time that the card is detected, turn off the LED after 30 secs. // TO DO: each time a card is read again, add 1 minute to the "stay lit" time. // // credits: // RFID reader for Arduino // Wiring version by BARRAGAN // Modified for Arudino by djmatic // Cris Mendoza assisted with programming design and development // David Durand #include //variables to handle reading the RFID cards int val = 0; int bytesread = 0; //lets control LEDs, and their relevant properties struct light { boolean isOn; long expirationDate; int pin; }; const int TAGLEN = 12; //lets control RFID tags, and their relevant properties struct tag { boolean isBanned; long expirationDate; char tagValue[TAGLEN]; }; //how long a tag cannot be read after initially being read (10 secs for now) long banTime = 30000; //keep track of the tags that have just been read const int bannedListLength = 10; struct tag bannedList[bannedListLength]; int bannedListPointer = 0; //keep track of the lights that are off (available) const int numLights = 9; struct light lights[numLights]; int whichLight = 0; //how long an LED remains on by default long lightDuration = 30000; void setup() { Serial.begin(2400); // RFID reader SOUT pin connected to Serial RX pin at 2400bps pinMode(2,OUTPUT); // set digital pin 2 as output, connect it to the RFID /ENABLE pin //where are the LEDS? connected to digital pins 4-12 int lightPins[9] = { 4, 5, 6, 7, 8, 9, 10, 11, 12 }; for (int i = 0; i < numLights; i++){ lights[i].isOn = false; lights[i].expirationDate = 0; lights[i].pin = lightPins[i]; pinMode(lights[i].pin, OUTPUT); } for (int i = 0; i < bannedListLength; i++) { bannedList[i].isBanned = false; bannedList[i].expirationDate = 0; bannedList[0].tagValue[i] = '\0'; // clear out the code just read to make room for a new one } randomSeed(analogRead(0)); // maybe use millis(); } //end setup void loop() { long currentTime = millis(); //get the amt of time program has been running //check if there are any banned tags that can be un-banned for (int i = 0; i < bannedListLength; i++) { if (bannedList[i].expirationDate <= currentTime) { //woah you been banned too long bannedList[i].isBanned = false; //let's unban you! } } //check if there are any lights that should be turned off for (int i = 0; i < numLights; i++) { if (lights[i].expirationDate <= currentTime) { //woah you been on too long lights[i].isOn = false; //let's turn you off! } } //check if tag has been read, and what we should do checkForTag(); //a valid tag has been read in, let's turn a light on lightSwitch(); }//end main loop void checkForTag() { char code[TAGLEN+4]; // stores a tag's 10-digit ID (larger for safety) digitalWrite(0, LOW); // Activate the RFID reader (pin 0 = aruino RX) 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 tag ID 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 code[bytesread] = '\0'; // ready to read next digit } } //now 10 digit read is complete Serial.print("TAG code is: "); // possibly a good TAG Serial.println(code); // print the TAG code bytesread = 0; digitalWrite(0, HIGH); // deactivate RFID reader delay(500); // wait for a second //let's check if this card has been read before boolean found = false; for(int i = 0; i < bannedListLength; i++) { Serial.println("checking thru banned list "); if (bannedList[i].isBanned){ Serial.println("something is banned "); //let's check if the banned tag matches the one we just read if(memcmp(code, bannedList[i].tagValue, TAGLEN) == 0) { return; } } } //ok, we read in a new tag, so let's ban it for banTime secs bannedList[bannedListPointer].isBanned = true; Serial.print("banned list pointer is "); Serial.println(bannedListPointer); bannedList[bannedListPointer].expirationDate = millis() + banTime; //store the value of the tag just read in, because now it is banned memcpy(bannedList[bannedListPointer].tagValue, code, TAGLEN); bannedListPointer++; bannedListPointer = bannedListPointer % bannedListLength; //let's tell the light data structure to turn on a random light //we're gonna be tricky and base that off of index value, not real random number! //first, go over the whole list of lights to see which ones are available: int numOff = 0; for (int i = 0; i < numLights; i++) { if (lights[i].isOn == false) { //if the light is off, count it numOff++; } } Serial.print("number of lights off: "); Serial.println(numOff); int availableLights[numOff]; //this will hold the indexes of available lights //go over the array again, this time to put the available lights in int curAvailLight = 0; for (int i = 0; i < numLights; i++) { Serial.println("yo "); if (lights[i].isOn == false) { //if the light is off, count it availableLights[curAvailLight++] = i; } } //generate random number between 0 and number of lights that are off whichLight = random(numOff); Serial.print("the light to turn on is: "); Serial.println(whichLight); lights[whichLight].isOn = true; lights[whichLight].expirationDate = millis() + lightDuration; } }//end "if serial available" }//end checkForTag void lightSwitch(){ for(int i = 0; i < numLights; i++) { if(lights[i].isOn) { digitalWrite(lights[i].pin, HIGH); } else { digitalWrite(lights[i].pin, LOW); } } }//end lightSwitch