Reading Serial input strings to execute IF

admittedly, its been quite some time since ive coded C, but tbh this should be very simple.

Im simply trying to read the serial input for a string, at which point X happens.

However, no matter how many examples I read or posts I reference, or variations I use. none work.

Ive inserted several println statements to help show where Im at.

The program will simply print After, meaning after my IF block, until something is input.

Its supposed to go into IF, print 2 meaning stage 2, print out a message etc.

But I cannot get into my IF.

CODE:


void setup() {
Serial.begin(115200); //initial the Serial
}

char inData[20]; // Allocate some space for the string
char inChar; // Where to store the character read
byte index = 0; // Index into array; where to store the character

void loop()
{
while(Serial.available() > 0)
{
if(index < 19) // One less than the size of the array
{
inChar = Serial.read(); // Read a character
inData[index] = inChar; // Store it
index++; // Increment where to write next
inData[index] = '\0'; // Null terminate the string
}
}

if (strcmp(inData, "Hello") == 0){ //If strings are Equal do X

Serial.print("2"); //step 2
Serial.write("Chamber1Fault"); //send what has been received if read returns fault, write fault
Serial.println();
Serial.print(inData); //print input string
Serial.println();
}

Serial.print("After"); // IF skipped
Serial.println();
Serial.print(inData); //print input

delay(1000); //wait
}


Using the serial mon to test it. The screenshot shows After After etc until I input Hello.

After which it prints After Hello After Hello confirming the string input is correct, but I never got into the IF

Which should give a 2, Chamber Fault, Hello etc

Thanks

Try this line in your code and it probably will work:

if (strncmp(inData, "Hello",5) == 0){ //If strings are Equal do X

Your string might include non-printable characters (newline, carriage returns, etc.) which are compared too but you don't see them.

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.

...R

Bingo!

That does it. It'd be nice if there was a decently quick to install debug tool so I can go through it..

How else besides knowing of strncmp do you figure that out..

I started installing eclipse etc etc but theres sooo many steps it like, its a simple IF condition I shouldnt need a 60min install to understand why my condition isnt being met.

Anyway,

Thanks alot.

Repped.

fullauto2009:
How else besides knowing of strncmp do you figure that out..

Forty years of messing with PCs :slight_smile:

...R

Why are you using Eclipse, instead of the Arduino IDE?

You just google "c string functions" and there is all the documentation of the standard C string functions. These are not specific to Arduino.

I am using the arduino ide. I was talking about a debug tool which the arduino ide doesnt have.

I was following guides to set it up for eclipse etc, but as I said, after about 30min+ of dloading and setting it up, I just got frustrated.

I shouldnt have to jump through a crazy amount of hoops for a simple if statement.

Anyway, yea I understand the normal C lib applies, but thats not gonna tell me oh its bc thier extra characters..

I am using the arduino ide. I was talking about a debug tool which the arduino ide doesnt have.

That's not a problem of the IDE but of the rather simple hardware the Arduino is. You cannot do step-by-step debugging with that hardware.

I see. Guess I'll just have to code bug free then :]