I am having a problem trying to build an array from incoming data from the software serial. Whenever I print the array onto the serial monitor it is blank, like nothing exists in the array. Here is my code neglecting the setup:
Why would it not be true? It does enter that loop so it must be true. To print inData does it need to be in a for loop or should Serial.println(inData); work to print the entire array?
Global variable idx is only re-initialised to 0 in one branch of this if statement. Won't that sometimes cause problems the next time the function is called?
I changed a few things, like the while loop, and eventually you do get "they are equal". However the way you are doing it, is horrible. Your FOR loop runs constantly, and you will see when you try my code.
char inChar = -1;
char inData[20];
byte idx=0;
int conStat(char* This)
{
if(Serial.available()>0) { // changed to use the serial monitor instead of software serial.
if(idx < 19) {
inChar = Serial.read();
Serial.write(inChar);
inData[idx] = inChar;
idx=idx+1;
inData[idx] = '\0';
}
}
int i;
for(i=0;i<19;i++)
{
Serial.print(i);
Serial.print(": ");
Serial.println(inData[i]);
}
if(strcmp(inData,This) == 0) {
for(int i =0;i<19;i++) {
inData[i] = 0;
}
idx = 0;
return(0);
}
else {
return(1);
}
}
void setup()
{
Serial.begin(9600); // had to lower the baud rate from 115200(my default), to 9600 to catch the results.
}
void loop()
{
if(conStat("NO CARRIER") == 0)
{
Serial.println("They are equal");
}
}
Micah_kurtz:
Any ideas as to why inData would be empty? Thanks!
Because you haven't received anything?
Note that when the array becomes full there is no way out of that read loop, because you only read the character if there is space for it in the array. It would be more robust if you read the character and then either appended it to the array, or ditched it.