IF statement

This day just keeps getting worse…

What am i doing wrong? ive been staring for the screen for hours.

I lost my project sketch and had to redo this whole part, i know for sure it was 99% the same code i used and it was working

Code:

void setup() 
{
Serial.begin(9600);
}

void loop()
{
char string[10];
char comando[5];
char codigo[5];
char *index;
int  charsRead;

while (true)
{
   if (Serial.available() > 0)
  {
    charsRead = Serial.readBytesUntil('\n', string, sizeof(string) -1);
    string[charsRead] = '\0';
 
    Serial.print("string = ");
    Serial.println(string);
   
    index = strchr( string, ';');
    *index = '\0';
    
    strcpy(comando, string);
    strcpy(codigo, index + 1);
    
    Serial.print("comando = ");
    Serial.println(comando);
    Serial.print("codigo = ");
    Serial.println(codigo);
    break;
   }
}
 
  if (comando == "hello")
    {
   Serial.print("hello is the string"); 
    }
    else
    {
   Serial.print("hello isn't the string"); 
    }
}

It reads a string i send, splits it in two parts, “comando” ( first half ) and “codigo” ( second half ). Everything’s fine until i try to do this simple IF statement.

IF comando = hello , print " hello is the string ", if its anything but hello, " hello isnt the string.

Thing is, when i send “hello”, it always prints “hello isn’t the string”, what am i doing wrong?

i’ve been working nonstop on visual basic for the past few days my head is hurting, im sure im missing something obvious…

Attachment below

fail.png

Google "strcmp"

AWOL:
Google “strcmp”

I know about the existance of strcmp, but before i lost my sketch i was doing it how i posted above, could you tell me what i am doing wrong now?

 if (comando == "hello")

This is comparing the pointer (or address) of the string to comando, so it is syntactically correct and will compile with no errors, although you should get a warning that you are mixing up data types. This will work in VB as a string is a supported native data type. Strings in C are arrays of characters. There is a String class that you can use, but this is generally considered to be buggy.

If you want to check is the strings are the same you need to use strcmp(), as has already been suggested.

but before i lost my sketch i was doing it how i posted above,

You may have been using the String class, but as posted above, it never worked.

Thank you, learning everyday!

adopted the suggested method, worked perfectly!