compare string and const char

String ch;
if(Serial.available()>0)
{
ch=Serial.read();
if(ch=="t"||ch=="temperature")
{
Serial.println(" OK ");

}
}

if i try to run above code i am not getting the desired answer. Plz help

ch=Serial.read();
     if(ch=="t"||ch=="temperature")

Point to the key on your computer’s keyboard marked “temperature”.

ch=Serial.read();
     if(ch=='t')

Should work fine (for some values of fine)

Let's see the whole program and the whole error message.
One thing is certain and that is ch will never equal "temperature" because Serial.read() will only read one character.

AWOL:
<…>
Point to the key on your computer’s keyboard marked “temperature”.
<…>

:o

… coffee spew !

Ray

The Serial.readString() method will read a complete String (for whatever definition of complete you provide). Then, you can compare the String read to other strings.

Thankyou PaulS Serial.readString() worked

String ch;
void setup() {
Serial.begin(9600);

}

void loop() {
if(Serial.available()>0)
{
ch=Serial.read();
if(ch=="t"||ch=="temperature")
{
Serial.println(" OK ");

}
}
}
was the original code. It compiled successfully but when i entered t or temperature nothing was printed.
But when i replaced Serial.read() with Serial.readString(), Problem was solved.

AWOL:

ch=Serial.read();

if(ch=="t"||ch=="temperature")




[...]
Should work fine (for some values of fine)

The OP defines ch as type String, which I guess is an object.

Serial.read() returns a char, right?

Why do people use String, really? I mean, I've worked 20 years with Java, and still think (char *) is much more intuitive than String in C/C++, because it's an explicit reference to a block of memory reserved for a spesific use.

char *buf=malloc(50);  // 50 bytes = 49 string characters + '\0' - no other overhead

String objects are great in Java/C#, but that's only 'cause you don't care about consuming megabytes of data, plus you have automatic garbage collection. Transferring this way of working with strings to the Arduino, you will quickly gobble up the whopping 2048 bytes of RAM (Uno).

:slight_smile:

The OP defines ch as type String, which I guess is an object.

String is a class, which makes ch an object.

Serial.read() returns a char, right?

Yes.

Why do people use String, really?

Because it is easy. Why use delay()? We know that is bad, too.

I mean, I’ve worked 20 years with Java, and still think (char *) is much more intuitive than String in C/C++.

You’re weird, then. 8)

Strings are easy to use. The class takes care of all the work. C style strings require engaging the brain.

PaulS:
String is a class, which makes ch an object.
Yes.
Because it is easy. Why use delay()? We know that is bad, too.
You're weird, then. 8)

Strings are easy to use. The class takes care of all the work. C style strings require engaging the brain.

Possibly (char *) is more intuitive because I know it from when I programmed C for a living, some 20 years ago (felles like closer to 100).

Another issue in Java/C# is that strings are not sequences of one-byte-per-character.

Doing unicode on (char *) strings is probably not much fun.

I agree with the other posters: Using the String class is a crutch; it's used because it's easy. The String version below:

//String ch;
char ch[12];

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

void loop() {
  int charsRead;

  if (Serial.available() > 0)
  {
//    ch = Serial.readString();

    charsRead = Serial.readBytesUntil('\n', ch, sizeof(ch) - 1);
    ch[charsRead] = '\0';                   // Now it's a string

//    if (ch == "t" || ch == "temperature")

    if (strcmp(ch, "t") == 0 || strcmp(ch, "temperature") == 0)
    {
      Serial.println(" OK ");
    }
  }
}

uses 4050 bytes of memory, whereas the C string (lower-case 's') uses 2392 bytes. Simply stated, using the crutch of the String class does not come without its own costs. There are still some hiccups in the code (e.g., what if they enter 'T' instead of 't').

OP: Please read Nick Gammon's post at the top of this Forum on the proper way to post source code using code tags.

Possibly (char *) is more intuitive because I know it from when I programmed C for a living, some 20 years ago (felles like closer to 100).

It's more intuitive because you learned to do stuff right first.

Doing unicode on (char *) strings is probably not much fun.

That's why there are wchar and wchar * types.