Program RFID Starter Kit Problem ID-12

hi! :smiley:

i change program by http://forum.arduino.cc/index.php?action=profile;u=62243
i want to display "Your Card is False "to serial once with a false card,
but the "Your Card is False" display many times at Serial Monitor.
anyone can help ? :~

#include <SoftwareSerial.h>

const int SerInToArdu=7; //Defines pin data passes
//to Arduino over from RFID reader
const int SerOutFrmArdu=3; //Not used, but
//"fills" a parameter in the set up of
//mySerialPort
String FullCode;

SoftwareSerial mySerialPort(SerInToArdu,SerOutFrmArdu);
// Creates serial port for RFID reader to be attached to.
// Using pins 0 and 1 is problematic, as they are also
// connecting the development PC to the Arduino for
// programming, and for the output sent to the serial
// monitor.

void setup()
{
Serial.begin(9600);//For access to serial monitor channel
Serial.println("Please Scan Card");
mySerialPort.begin(9600);
delay(1000);
};

void loop()
{
int incomingByte=0;//create and initialize a local variable

//The "then" part of the following will only execute
// after the RFID reader has sent something to the Arduino
if (mySerialPort.available() > 0) {

// read the incoming byte from the serial buffer
incomingByte = mySerialPort.read();
// show what was received
//Serial.print(incomingByte, DEC);
//Serial.print(' ');
FullCode = FullCode + " " + incomingByte;
if (FullCode == 124){
Serial.println();
Serial.print("The Full Code is - ");
Serial.print(FullCode);
Serial.println();
if (FullCode == FullCode)
{
Serial.println("Your Card Is True");
FullCode = "";
}
}
if(FullCode != 124);
{
Serial.println("Your Card Is False");
FullCode = "";
}
FullCode = "";
}
}
}

you are comparing a string value to a numeric value and you are doing it everytime that the code loops through (per byte read)

so, what should i do ? :frowning:

so, what should i do ?

Compare things that are the same type.
Only compare things when the complete packet has been received.

None of this is rocket science.

thanks y all for the reply,
i change program into this, but "your Card is false" didn't come out at my serial monitor :drooling_face:
and what should i change the number of "124" to string? because that is a unique header that i have in tag rfid

#include <SoftwareSerial.h>

const int SerInToArdu=7; //Defines pin data passes
//to Arduino over from RFID reader
const int SerOutFrmArdu=3; //Not used, but
//"fills" a parameter in the set up of
//mySerialPort
String FullCode;

SoftwareSerial mySerialPort(SerInToArdu,SerOutFrmArdu);
// Creates serial port for RFID reader to be attached to.
// Using pins 0 and 1 is problematic, as they are also
// connecting the development PC to the Arduino for
// programming, and for the output sent to the serial
// monitor.

void setup()
{
Serial.begin(9600);//For access to serial monitor channel
Serial.println("Please Scan Card");
mySerialPort.begin(9600);
delay(1000);
};

void loop()
{
int incomingByte=0;//create and initialize a local variable

//The "then" part of the following will only execute
// after the RFID reader has sent something to the Arduino
if (mySerialPort.available() > 0) {

// read the incoming byte from the serial buffer
incomingByte = mySerialPort.read();
// show what was received
//Serial.print(incomingByte, DEC);
//Serial.print(' ');
FullCode = FullCode + " " + incomingByte;
if (FullCode == 124){ // the header of tag card
Serial.println();
Serial.print("The Full Code is - ");
Serial.print(FullCode);
Serial.println();
if (FullCode == FullCode)
{
Serial.println("Your Card Is True");
FullCode = "";
}
if(FullCode != 124);
{
Serial.println("Your Card Is False");
FullCode = "";
}
FullCode = "";
}

}
}
}

      // show what was received
      //Serial.print(incomingByte, DEC);

So, just for giggles, why don't you?

                if (FullCode == 124){       // the header of tag card

While you are at it, why don't you print out the value in FullCode AFTER you have received a full packet of data from the card?

I know the answer to that last question. It's because you have no clue when the last byte in the packet arrives, do you?

You MUST write code to read from unknown devices a step at a time. First, see what you are getting. What marks the beginning of a packet? What marks the end of a packet? Until you know the answers to these questions, you have no way of reading and storing a whole packet of data.

When you can pick a packet out of the stream, you'll have a much better idea what part of the packet is relevant. It may turn out to be all of it. It may be that security isn't all that important, so you might elect to ignore a checksum, for instance. Or, you might not.

Figure out where a packet starts and ends, first.

i do that but the diplay at serial monitor is "Your card is false"

this is the program, can you fix the program?

#include <SoftwareSerial.h>
//#include <LiquidCrystal.h>
//LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

const int SerInToArdu=7; //Defines pin data passes
//to Arduino over from RFID reader
const int SerOutFrmArdu=3; //Not used, but
//"fills" a parameter in the set up of
//mySerialPort
String FullCode;

SoftwareSerial mySerialPort(SerInToArdu,SerOutFrmArdu);
// Creates serial port for RFID reader to be attached to.
// Using pins 0 and 1 is problematic, as they are also
// connecting the development PC to the Arduino for
// programming, and for the output sent to the serial
// monitor.

void setup()
{
Serial.begin(9600);//For access to serial monitor channel
Serial.println("Please Scan Card");
//lcd.begin(16, 2);
//lcd.print("Please Scan Card");
mySerialPort.begin(9600);
delay(1000);
//lcd.clear();
};

void loop()
{
int incomingByte=0;//create and initialize a local variable

//The "then" part of the following will only execute
// after the RFID reader has sent something to the Arduino
if (mySerialPort.available() > 0) {

// read the incoming byte from the serial buffer
incomingByte = mySerialPort.read();
// show what was received
Serial.print(incomingByte, DEC);
Serial.print(' ');
FullCode = FullCode + " " + incomingByte;
if (incomingByte==240){
Serial.println();
Serial.print("The Full Code is - ");
Serial.print(FullCode);
Serial.println();
if (FullCode == "112 255 248 0 0 224 14 224 248 254 24 14 206 224 14 240")
{
Serial.println("Your Card Is ");
Serial.print(" true");
FullCode = "";
}

if (FullCode != "112 255 248 0 0 224 14 224 248 254 24 14 206 224 14 240"){
Serial.println("Your Card Is ");
Serial.print(" false");
FullCode = "";
}
//delay(1000);
// lcd.clear();
FullCode = "";
}

}
}

int incomingByte=0;//create and initialize a local variable

Serial.read() returns an int, so it can accommodate fools that don't bother to check for data first. You do, so, int is the wrong type.

this is the program, can you fix the program?

Without your card reader and cards, no. But, here is a clue:

      Serial.print(incomingByte, DEC);
                Serial.print(' ');
                FullCode = FullCode + " " + incomingByte;

Using the second argument to the Serial.print() function affects how the value is shown to you. You then assume, completely incorrectly, that the same transformation is applied when you add the integer to the end of the String.

If incomingByte were the type that its name suggests, and you were not using the second argument when printing the value, you'd see different output. If incomingByte were the correct type, and had a name that reflected its actual type, you'd see different output. In either case, it would be obvious why the String never matches.

Silly question but how many digits are shown on your rfid tag?

void checkRFID()
{
  // Check the serial port for data from the RFID module.
  if (Serial1.available() > 0)
  {
    char d = Serial1.read();
    switch (d)
    {
    case 0x02: // Start of transmission
      clearSettings();
      break;
    case 0x03: // End of transmission
      // Turn off the RFID module.
      digitalWrite(PIN_RFID_RESET, LOW);
      readTag();
      ReadyforRFID();
      
      
      break;
    default: // Add to buffer.
      currentLine += d;

    }
  }
}


void readTag() // Parse the data from a card.

{
currentLine = currentLine.substring(4,currentLine.length()-2);      
char rfidchar[6];
currentLine.toCharArray(rfidchar, 25);
rfidTag = strtoul(rfidchar, 0, 16);
currentLine = String(rfidTag);
  
  Serial.print("Card Read:");
  Serial.print(currentLine);
  Serial.println("!");
  readClientFile();
    // Make a beep
  
}

I like to read in the card and display it the same way it is shown on the card ( i strip the zeros out)

PaulS:

int incomingByte=0;//create and initialize a local variable

Serial.read() returns an int, so it can accommodate fools that don't bother to check for data first. You do, so, int is the wrong type.

this is the program, can you fix the program?

Without your card reader and cards, no. But, here is a clue:

      Serial.print(incomingByte, DEC);

Serial.print(' ');
                FullCode = FullCode + " " + incomingByte;



Using the second argument to the Serial.print() function affects how the value is shown to you. You then assume, completely incorrectly, that the same transformation is applied when you add the integer to the end of the String.

If incomingByte were the type that its name suggests, and you were not using the second argument when printing the value, you'd see different output. If incomingByte were the correct type, and had a name that reflected its actual type, you'd see different output. In either case, it would be obvious why the String never matches.

so what should i choice for the type incomingByte ?
is it char ?

sorry for the last answer i didn't understand what you mean :~

jasit:
Silly question but how many digits are shown on your rfid tag?

my rfid tag shape is like button
button1 = 112 248 0 0 224 248 224 248 24 124 0 0 252
button2 = 112 248 0 0 224 14 224 248 254 24 14 206 224 14 240

and the last is like your rfid tag picture
112 248 24 128 224 248 28 224 248 28 12 224 224 252 12

i confused with my id.
why my rfid tag have long id. i searching in other website about arduino tutorial, and people just have 12 digits ID and the last 2 digits is checksum.

by the way,
i can't understand what the meaning of this line of program :

    switch (d)
    {
    case 0x02: // Start of transmission
      clearSettings();
      break;
    case 0x03: // End of transmission
      // Turn off the RFID module.
      digitalWrite(PIN_RFID_RESET, LOW);
      readTag();
      ReadyforRFID();
      
      
      break;
    default: // Add to buffer.
      currentLine += d;

    }
  }
}


void readTag() // Parse the data from a card.

{
currentLine = currentLine.substring(4,currentLine.length()-2);      
char rfidchar[6];
currentLine.toCharArray(rfidchar, 25);
rfidTag = strtoul(rfidchar, 0, 16);
currentLine = String(rfidTag);
  
  Serial.print("Card Read:");
  Serial.print(currentLine);
  Serial.println("!");
  readClientFile();
    // Make a beep
  
}

so what should i choice for the type incomingByte ?

Probably not float. Probable not long.

Give me a break. What type are you already using in the name?

byte incomingByte;

Serial.print(incomingByte); // No second argument!

PaulS:
Give me a break. What type are you already using in the name?

byte incomingByte;

Serial.print(incomingByte); // No second argument!

so, i should choose byte ?
i'll try then

i used int for incomingByte

if you take a look at the code I am using char and d as the name. As I read the characters in, if the character is 0x02 then it is the start of the RFID tag. if it is 0x03 it is the end of the RFID tag. so everything else is the middle of the tag.
so if i get a 0x02 then clear the settings and prepare to read a new card.

the line currentLine =+ d; appends the data to currentLine. So once I get a 0x03 then we know the reading of the code is done. so goto readtag();

the readtag strips all of the data I don't want so I can use just the tagnumber. So if you do a Serial.println(currentLine); you should see just the rfid tag number.

if (Serial1.available() > 0)
  {
    char d = Serial1.read();
    switch (d)
    {
    case 0x02: // Start of transmission
      clearSettings();
      break;
    case 0x03: // End of transmission
      // Turn off the RFID module.
      digitalWrite(PIN_RFID_RESET, LOW);
      readTag();
      ReadyforRFID();
      
      
      break;
    default: // Add to buffer.
      currentLine += d;

    }
  }
}

jasit:

if (Serial1.available() > 0)

{
    char d = Serial1.read();
    switch (d)
    {
    case 0x02: // Start of transmission
      clearSettings();
      break;
    case 0x03: // End of transmission
      // Turn off the RFID module.
      digitalWrite(PIN_RFID_RESET, LOW);
      readTag();
      ReadyforRFID();
     
     
      break;
    default: // Add to buffer.
      currentLine += d;

}
  }
}

by the way, this program really works for you?
are you using ID-12 rfid ?

switch the Serial1. to Serial, I am using a Mega with port 19

to PauIS and jasit

thank you very much for your help and answer :slight_smile: