RFID buffer keeping old UID a few times before printing the new one


i'm new whole c++/arduino thing, please bare me! i'm trying can rfid cards, working properly, when trying scan new card after scanning previous one, old card's uid shows few more times before new 1 starts appearing, , i'm not sure why.

i'm sure it's simple i'm overlooking, i'm stumped. help! haha..

code: [select]
int readpin = 20;    //pin being read from
boolean gtisent = false; //if tag info has been sent or not
unsigned long timesent;
byte buffer[256];
byte uid[8];
byte olduid[8];
byte = 0;
byte card;

void printhex8(uint8_t *data, uint8_t length) // prints 8-bit data in hex leading zeroes
{
       for (int i=0; i<length; i++) {
         if (data[i]<0x10) {serial.print("0");}
         serial.print(data[i],hex);
       }
}

// setup routine runs once when press reset:
void setup() {
 
  // initialize serial communication @ 9600 bits per second:
  pinmode(13, output); //led pin testing
  pinmode(20, input);
  serial.begin(19200);
  serial1.begin(115200);
  serial.println("waiting card...");
}

// loop routine runs on , on again forever:
void loop() {
     
  if(digitalread(readpin)==high) {
    digitalwrite(13, low);
    return;
  }
  if(digitalread(readpin)==low){
    digitalwrite(13, high);

    if(!gtisent) {
      serial1.write(0xba);
      serial1.write(0x02);
      serial1.write(0x31);
      serial1.write(0xba ^ 0x02 ^ 0x31);
      serial1.flush();
      gtisent = true;
      timesent = millis();
    } else {
      if(serial1.available()>0) {
        buffer[a] = serial1.read();
        a++;
     
      if(buffer[0] != 0xbd)
        = 0;
     
      if(a>2) {
        byte length = buffer[1];
        if(a >= length + 2)
        {
          byte chk = 0x00; //checksum checking below
          for(int i=0;i < length+1; i++)
            chk ^= buffer[a];
          if(chk != buffer[length+1]) {
            gtisent = false; //checksum error
          } else {
            if(buffer[3] != 0x00) { //failed (0x00 success)
              gtisent = false;
            } else {
              for(int i=0;i<8;i++) {
                uid[i] = buffer[11-i]; //copy uid uid array
              }
          if(memcmp_p(uid,olduid,8 == 0)) { //uid comparison thingy try , prevent duplicates
           memset(uid,0,sizeof(uid));
           memset(olduid,0,sizeof(olduid));
           memset(buffer,0,sizeof(buffer));// many memsets here , below may not needed, gettind desperate :/
          return;
          }
        if(uid[0] == 0xe0 && uid[1] == 0x04) //check specific card uid beginning
        {
          printhex8(uid,8); //print uid
          serial.println();
          memcpy(olduid,uid,sizeof(uid));
          memset(buffer,0,sizeof(buffer));
          memset(uid,0,sizeof(uid));
        }
        delay(1500);
       }
      }
     }
    }
   } else {
       if(millis() - timesent >500) {         // if last command sent more 500ms ago, send new one
         gtisent = false;
         serial.println("failed");
       }
        }
       }
      }
    else // if no tag in range
    {
     card = 0;
     gtisent = false;
    }
   }

code: [select]
if(memcmp_p(uid,olduid,8 == 0)) { //uid comparison thingy try , prevent duplicates
the wtf light exploded.

8 not equal 0.


Arduino Forum > Using Arduino > Programming Questions > RFID buffer keeping old UID a few times before printing the new one


arduino

Comments