system
March 27, 2010, 5:02am
1
Hi all, I'm trying to compare two strings to each other. One that is read in using "Serial.Read" and another that I physically add into the code. I highlighted what i need compared.
To summarize the code: It reads a 16 bit number and it is supposed to compare to a code i previously entered into it. If the codes match up, the LED will light up (digitalWrite HIGH).
I tried the code below but keep getting an error:
request for member 'equals' in 'code', which is of non-class type 'char [17]
Any help would be great. Thank you in advance.
int incoming = 0;
char code[17];
int bytesread = 0;
int i;
const byte Pin2=2;
char str2[17]="[glow]FF954003F7E92592[/glow]";
void setup()
{
pinMode(Pin2,OUTPUT);
Serial.begin(9600);
}
void loop()
{
if(Serial.available() > 0)
{
delay(035);
if(Serial.read() == 'F')
{
code[0]='F';
for(i = 1; i < 17; i++)
{
code[i] = Serial.read();
}
Serial.print("Information Read: ");
Serial.println([glow]code[/glow]);
[glow] if (code.equals(str2) == true)
{
digitalWrite(Pin2, HIGH);
delay(5000);
digitalWrite(Pin2, LOW);
break;
}[/glow]
}
}
}
delay(035);
035 is an octal number. Was that your intention?
Your Sketch tries to read 17 characters. Change the for loop...
for(i = 1; i < [glow]16[/glow]; i++)
Once code is properly terminated...
code[0]='F';
for(i = 1; i < 17; i++)
{
code[i] = Serial.read();
}
[glow]code[16] = 0;[/glow]
Serial.print("Information Read: ");
You can use strcmp to make the comparison...
if ( strcmp( code, str2 ) == 0 )
system
March 27, 2010, 12:08pm
3
Your Sketch tries to read 17 characters. Change the for loop...
Array indices start at 0, not 1. You need to change the start value of the for loop, rather than the end value that Coding Badly highlighted.
That value is correct.
RaptorXP1 wants to "read a 16 bit number". I assume from the statement and Sketch that he / she meant to write "read a 16 digit hexadecimal number".
if(Serial.read() == 'F')
{
code[0]='F';
At this point code[0] is assigned (one character stored).
for(i = 1; i < 17; i++)
{
code[i] = Serial.read();
}
At this point these are assigned...
code[1] = Serial.read(); // two characters
code[2] = Serial.read(); // 3
code[3] = Serial.read(); // 4
code[4] = Serial.read(); // 5
code[5] = Serial.read(); // 6
code[6] = Serial.read(); // 7
code[7] = Serial.read(); // 8
code[8] = Serial.read(); // 9
code[9] = Serial.read(); // 10
code[10] = Serial.read(); // 11
code[11] = Serial.read(); // 12
code[12] = Serial.read(); // 13
code[13] = Serial.read(); // 14
code[14] = Serial.read(); // 15
code[15] = Serial.read(); // 16
code[16] = Serial.read(); // 17 characters stored in the array
As written, the Sketch stores 17 characters in the array with no storage left for the terminator.
If RaptorXP1 really does want to store a 16 character value then the 17 is wrong. If RaptorXP1 really does want to stored a 17 character value then code is too small.
system
March 27, 2010, 9:15pm
5
Thank you guys, it works now. I changed the value of code[] and used the strcmp . Appreciate the help.