Good Morning again,
I shortly discribe what i want to programm. I have a RFID reader which sends The ID from a 125khz RFID Card Serial to my Arduino. I want my Arduino to notice when there comes the excactly ID from my Card. The Code ist finished (very simple,but should work). But The Char Comparsion doesn´t work. Do you have any Idea what´s wrong? thank you very much, Yours Ruediger
int incomingByte = 0;
char String1[30];
char String2[30];
void setup() {
Serial.begin(9600);
}
void loop() {
// send data only when you receive data:
if (Serial.available() > 0) {
incomingByte = Serial.read();
itoa(incomingByte,String1,10);
strcat(String2,String1);
Serial.print("ID: ");
Serial.print(incomingByte, DEC);
Serial.print(" ");
Serial.println(String2);
if (String2 == "25052")
{Serial.println('erkannt');
}
}
}
Ps: If i scan my Card,these stands in my Serial Monitor:
ID: 2 2
ID: 50 250
ID: 52 25052
ID: 48 2505248
only that you know,that the Char is sometimes truely 25052
Single quotes <'> won't give you what you want - you must use double-quotes <">
One other thing - I think your code is relying on the "strings" being terminated by the zeroes initialised into the arrays.
This will bite you on the bottom at some time in the future.
Get used to to terminating the strings yourself.
Every time you read a character into the array, you should write a zero into the next array element (watch out for array overflow)/
@ AWOL:
Thanks, works perfectly!
Greetings, Ruediger
Ps: Thank you for the tip with the Overflow. I first want that the Programm work, than i want to fix the bugs. But you are right, i only can scan my card the first time,after that i get the overflow. I will fix it. Thanks again, Ruediger
Pps: My English isn´t very good so i´m not really shure what you mean with terminating by themselfes, but i think google will help me
When you define an array at global scope, like String1 and String2, they are automatically filled with zeroes just after the processor resets (unless you specify some other value).
A string in 'C' is a sequence on non-zero values, with a zero byte as the last element. This is the only way that functions like "strcmp" and "strcat" can work, otherwise they don't know where the string ends.
If you start filling a global array that hasn't been used before, it will automatically be terminated by the zeroes that were there to begin with as you add characters, but a shorter string filled in later won't be terminated correctly, it will still have some of the original string left over.
One final point. My code to read the RFID data consistently failed, with random amounts of data read, if I used any Serial.print statements in the loop reading the data. It worked perfectly if all I did was collect the data, and wait to print it out at the end.