Pages: [1]   Go Down
Author Topic: Comparing 2 strings  (Read 18912 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 2
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,

The function below is not working properly, how can i compare 2 strings?
I send "temperat" to the arduino through the serial port, so the IF statement should return the analog value, only it doesnt work, it only prints back:
"Received: temperat"

How should i alter the IF statement?
I already tried comparing 2 char arrays
(received and command)

char command[8];
char received[8] = {'t','e','m','p','e','r','a','t'}'

  if (Serial.available() == smiley-cool
  {
    for (int i=0; i < 8; i++)
    {
      command = Serial.read();      
    }

    if (command == "temperat")
    {
        val = analogRead(0);
        Serial.print(val,DEC);
    }
    Serial.print("Received: ");
    Serial.print(command);
  }
Logged

London
Offline Offline
Tesla Member
***
Karma: 10
Posts: 6255
Have fun!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

There are a few functions you can call to compare strings. strcmp is the most basic, it takes the two strings to be compared as parameters and returns 0 if the strings are equal.

you would use it as follows:

  if (Serial.available() >= smiley-cool   // this was == 8, more than one command may be waiting
  {
    for (int i=0; i < 8; i++)
    {
     command = Serial.read();      
    }
 
    if (strcmp(command, "temperat")  == 0)  // test to see if the two strings are equal
    {
       val = analogRead(0);
       Serial.print(val,DEC);
    }
    Serial.print("Received: ");
    Serial.print(command);
  }

BTW, i changed  the test for serial available so that it will work if more than one command is received at the  serial port when the sketch checks to see if charactors are available
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 2
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
There are a few functions you can call to compare strings. strcmp is the most basic, it takes the two strings to be compared as parameters and returns 0 if the strings are equal.

BTW, i changed  the test for serial available so that it will work if more than one command is received at the  serial port when the sketch checks to see if charactors are available

Cool thanks for the fast response  ;D
Logged

Sussex UK / CT USA
Offline Offline
Edison Member
*
Karma: 0
Posts: 1028
Forums forever
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Would it be appropriate to add strcmp to...

http://www.arduino.cc/en/Reference/Extended

... and the keywords list?

Does any library need to be #included to use strcmp?

Is there a different place to find strcmp? Any other "hidden" keywords?
Logged

London
Offline Offline
Tesla Member
***
Karma: 10
Posts: 6255
Have fun!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

> Would it be appropriate to add strcmp to...  http://www.arduino.cc/en/Reference/Extended
The reference page on strings http://www.arduino.cc/en/Reference/String would be a good place to mention the comparison functions

>Does any library need to be #included to use strcmp?
String handling functions like strcmp are declared in string.h but the Arduino environment seems to include this automatically.

>Is there a different place to find strcmp? Any other "hidden" keywords?
A good book on C should list these library functions and you can find a references to C library functions by doing an online search for words like: c language reference
Logged

Pages: [1]   Go Up
Jump to: