Program Alteration

Hello again, yet another thing I am working on is RFID for my door and am using the current code. However while it does run, it is very buggy when adding and removing cards for some reason, can anyone see an easy fix to remove the button sections and just manually add my RFID code 244A2B7 directly to the code?

#include <EEPROM.h>
 
volatile unsigned long tagID = 32;  
//32 = bitwise 100000 - Set an initial mask to keep all tags 4 bytes long. WG26 (bits) plus another 6 = 32 bits or 4 bytes.

volatile unsigned long tmptagID;

volatile unsigned long lastBitArrivalTime;

volatile int bitCount = 0;
int buttonState = 0;
int rec_chips = 0;
int MAX_CARDS = 10;
byte CardBytes[4];
int MatchedTag = -1;
 
void ISRone(void)
{
  lastBitArrivalTime = millis();
  
  bitCount++;
  tagID <<= 1;
  tagID |= 1;
}
 
 
void ISRzero(void)
{
  lastBitArrivalTime = millis();
  bitCount++;
  tagID <<= 1;
}
 
void setup()
{
  // Arduino connections:
  pinMode(2, INPUT);   // Data0
  pinMode(3, INPUT);   // Data1
  pinMode(4, OUTPUT);  // Beep
  pinMode(5, OUTPUT);  // Green LED
  pinMode(6, INPUT);   // Programming switch
  pinMode(13, OUTPUT);   // To Transistor for lock output


  digitalWrite(4,HIGH);// Turns off beep
  digitalWrite(5,HIGH);// Turns off green led
  digitalWrite(6,HIGH);// Enable Pullup on switch
  digitalWrite(13,LOW); // Make sure the door is locked by default
  
  Serial.begin(57600); // Serial Start
  
  delay(1000); //Give the reader some time to settle so we don't fire interrupts yet.
  
  buttonState = digitalRead(6);
  if(buttonState == LOW){
  //If the button is held on power up, clear the EEPROM
  for(int i = 0; i < 40; i++){
      EEPROM.write(i, 0xFF);
      }
   Serial.println("All recorded Chips Erased!");   
   }
 
// --- Data0 / Green Wire ----------------------------
  digitalWrite(2, HIGH);  // Enable pull-up resistor
  attachInterrupt(0, ISRzero, FALLING);
//----------------------------------------------------
 
// --- Data1 / White Wire ----------------------------
  digitalWrite(3, HIGH);  // Enable pull-up resistor
  attachInterrupt(1, ISRone,  FALLING);
//----------------------------------------------------

  tagID = 32;
  bitCount = 0;
  
}
 
void loop()
{
  if(bitCount > 0 && millis() - lastBitArrivalTime >  250)
    {
    //convert the 32 bits into 4 byte arrays
    CardBytes[0] = lowByte(tagID);
    CardBytes[1] = highByte(tagID);
    tmptagID = tagID >> 16;
    CardBytes[2] = lowByte(tmptagID);
    CardBytes[3] = highByte(tmptagID);
    
    buttonState = digitalRead(6);
    if(buttonState == LOW){
      //Programming mode
      WriteChip();
      }
    else {
      //Normal mode
      rec_chips = 0;
      Serial.print(CardBytes[0],HEX);
      Serial.print(CardBytes[1],HEX);
      Serial.print(CardBytes[2],HEX);
      Serial.println(CardBytes[3],HEX);
      MatchedTag = Checkchip(CardBytes);
      if (MatchedTag != -1){
        Serial.print("Matched Tag: ");
        Serial.print(MatchedTag);
        Serial.println(" Access Granted");
        delay (200);
        digitalWrite(4,LOW);// Turns on beep
        delay (500);
        digitalWrite(4,HIGH);// Turns off beep
        delay (200);
        digitalWrite(4,LOW);// Turns on beep
        delay (500);
        digitalWrite(4,HIGH);// Turns off beep
        digitalWrite(5,LOW); // Turns on green led
        digitalWrite(13,HIGH); // Unlock the door     
        delay (5000);
        digitalWrite(5,HIGH); // Turns off green led
        digitalWrite(13,LOW); // Lock the door again
      }
      else {
        Serial.println("Access denied");
        digitalWrite(4,LOW);// Turns on beep
        delay (5000);
        digitalWrite(4,HIGH);// Turns off beep
     }
  }
  tagID = 32;
  bitCount = 0;
  }
}

void WriteChip(){
  if (rec_chips < MAX_CARDS){
    Serial.print("Added Tag : ");
    for(int i = 0; i < 4; i++){
      EEPROM.write(4*rec_chips + i, CardBytes[i]);
      Serial.print(CardBytes[i],HEX);
      }
      //give some feedback that the card was added by beeping weird.
      delay(200);
      digitalWrite(4,LOW);// Turns on beep
      delay(20);
      digitalWrite(4,HIGH);// Turns off beep
      delay(20);
      digitalWrite(4,LOW);// Turns on beep
      delay(20);
      digitalWrite(4,HIGH);// Turns off beep
      delay(20);
      digitalWrite(4,LOW);// Turns on beep
      delay(20);
      digitalWrite(4,HIGH);// Turns off beep 
  }
  Serial.println("");
  ++rec_chips;
}
  
int Checkchip(byte TagData[]){
  int count = 0;
  byte tmpByte[4];
  while (count < 10){
    for(int i = 0; i < 4; i++){
      tmpByte[i] = EEPROM.read(4*count + i);
      // Serial.print(tmpByte[i], HEX);
      // Serial.print(" - ");
      // Serial.println(TagData[i], HEX);
    }
      if (tmpByte[0] == TagData[0] && tmpByte[1] == TagData[1] && tmpByte[2] == TagData[2] && tmpByte[3] == TagData[3])
        {
        // We matched all 4 bytes of data to one in EEPROM
        //Serial.print("Matched tag ");
        //Serial.println(count);
        return count;
        }
      else
        {
          //Serial.print("Did not match tag : ");
          //Serial.println(count);
          count++;
        }
  }
 // If we got here, no tags matched - Access denied!
 //Serial.println("No match found"); 
 return -1; 
}

However while it does run, it is very buggy when adding and removing cards for some reason

You get serial output. You don't share that. Why not?

Well my Serial gives out usually correct information, this is from this morning:

All recorded Chips Erased! (Took three resets while holding pin6 low, after the fourth time and 30 second it registered)
All recorded Chips Erased!
Added Tag : 244A2B7 (Again had to reset a few times before the reader would see the card)
244A2B7
Matched Tag: 0 Access Granted (Took another board reset until the reader grabbed the id and about 10 seconds)

I am currently using a HID MiniProx but I have swapped the reader out with a new one and still no difference. The only thing I stupidly did was while building my button for pin6 (see attached pic) I shorted 5v to a Pin, so I'm wondering if I may have possibly buggered pin6, will run a simple code now to find out if its reading and outputting okay.

Sorry about the lack of info off the first post, end of a 19 hour day, brain wasn't working :frowning:

Hi Paul,

I came across an old post of yours from a while back which might help however, my code is a little more complex to start with the void loop and void writechip. Do I need to remove more of the if / else statements from my above code to implement.

int validSiteCodes[] = {196, 452};
int validSerialNumbers[] = {15809, 3285};
boolean IsTagValid(int siteCode, int serialNumber)
{
   boolean valid = false;

   // Determine how many valid tags there are
   int validTagCount = sizeof(validSiteCodes)/sizeof(int);

   // Loop through the arrays to see if siteCode 
   // and serialNumber are present

   for(int t=0; t<validTagCount; t++)
   {
      if(validSiteCode[t] == siteCode &&
         validSerialNumber == serialNumber)
      {
          valid = true;
          break;
      }
   }
 
   return valid;
}
if(IsTagValid(siteCode, serialNumber)
{
   // Open the door. It's cold out here!
}

see attached pic

Attached to what? I don't see a pic (it could be a firewall issue; not all pictures show up while I'm at work).

All recorded Chips Erased! (Took three resets while holding pin6 low, after the fourth time and 30 second it registered)

If you are having to press the switch and reset multiple times, the switch is not wired correctly, or the pin is toast.

Added Tag : 244A2B7 (Again had to reset a few times before the reader would see the card)

Then, again, there is something wrong with the way the reader is attached. Resetting does not make code sometimes behave better.

Attached to what? I don't see a pic (it could be a firewall issue; not all pictures show up while I'm at work).

same case here this is not firewall issue. Actually he didn't attach...

Only just noticed that the picture didn't attach, but either way it turns out I fried my pin6 so I'm moved to pin 8 and changed the code to get it working which it now does. So all sweet. I think I'll worry about adding the RFID codes into the code manually later down the track. Out to buy a doorstrike now.