Hey guys, I'm trying to get my Arduino to check for a code and then compare it to the ones i have already programmed.
I've tried the code with only a single compare: if (strcmp(tagcode, str1)==0) and it works, the LED lights up when the correct code is read but stays off when reading different codes. However, when i try to do the 'or' statements it fails. Can anyone see where i went wrong?
I don't think that is the problem, the precedence of == is higher than ||, though the () does make it clearer.
I notice in (with my comments):
if(Serial.read() == 'F')
{
tagcode[0]='F';
for(i = 1; i < 17; i++)
{ // put char into tagcode[1] to tagcode[16]
tagcode[i] = Serial.read();
}
tagcode[16]=0; // overwrite tagcode[16] again
// after it was added in the for loop
even though the strings are only 16 characters long, the for loop puts a character into tagcode*, where i is 16, and is hence character 17, and then puts 0 into the 17 location.* This would have the effect of getting out of synch with input, causing the code to skip characters until it finds another F. This is unfortunate because the strings have F in several places, so it can get badly out of synch; I can't see when it will get back into synch. I think you should change the for loop test to: for(i = 1; i < 16; i++) I'd suggest defining a constant: #define CODE_LENGTH (16) and base my calculations on using that to make the program a bit easier to follow. for(i = 1; i < CODE_LENGTH; i++) ... tagcode[CODE_LENGTH]=0; I'd also suggest you might want to declare the strings as: char str1[]="FF954003CAF55DF8"; especially if they are all going to be the same length. The use of 17 in their declarations may have distracted you when reading the code. To make communications easier to synchronise, can you use a character which isn't 0-9 or A-F in between codes? That way your loop can re-synchronise more easily if things go wrong. HTH GB-)