Data type comparison

Hello! I have little problem about data comparing so i hope someone can help me.

Im using IComsat v1.1 module with SIMM900 on Arduino MEGA. I can send and receive messages, but i need make program so that Arduino understands individual sms characters.
I will try to show code simple as possible to resolve this problem and then if it works, make it more complex.
Basicly i need to compare received SMS characters to defined variables and if equals do something.

char sms_rx[120];	//Variable for saving received sms
char ON[3] = "On";      //char variable to compare received sms, including NULL character
char OFF[4] = "Off";  	//char variable to compare received sms, including NULL character

void setup() 
{
	//code for modem setup
}
void Check_SMS() 
{
	//Subroutine for checking received sms
}

void loop()
{
Check_SMS();	
if ( sms_rx == ON )
	{
	digitalWrite(13, HIGH);
	}
else if (sms_rx == OFF )
	{
	digitalWrite(13, LOW);
	}
}

This code compiles, but if i send to module sms like "On", LED doesnt turn on. So i guess theres problem in comparing.
I know there is SoftwareSerial interface using AT commands, it would be easier to use, but i cant managed to make work with that. If someone can suggest options on that, i would be appreciated. But for now it would be good to read individual sms characters. :slight_smile:

You are just comparing the addresses of the strings, not their content. You can use strcmp which returns zero if the strings are equal:

  if(strcmp(sms_rx,ON) == 0) {
    // the strings are equal - ON
  } else if (strcmp(sms_rx,OFF) {
    // the strings are equal - OFF
  } else {
    // Neither string is matched
  }

Make sure that sms_rx is null-terminated and also beware of sms_rx having carriage-return and/or linefeed on the end.

Pete

My guess is that the Check_SMS() function fills in the sms_rx[] character array. If that's the case, you need to compare strings, which won't work using sms_rx == ON. You'd have to do something like:

if (strcmp(sms_rx, "ON") == 0)  
{
	digitalWrite(13, HIGH);
} else {
    if (strcmp(sms_rx, "OFF") == 0)
   {
	digitalWrite(13, LOW);
   }
}

strcmp() returns 0 when there is a match, -1 if the match fails and the letter of the mismatch is lexicographically less, or 1 if greater. For example, an ASCII 'A' is 65 and ASCII 'B' is 66, so strcmp("A", "B") would return -1 while strcmp("B", "A") would return 1, and strcmp("A", "A") or strcmp("B", "B") would return 0.

ivars88:

char ON[3] = "On";      //char variable to compare received sms, including NULL character

char OFF[4] = "Off";  //char variable to compare received sms, including NULL character

You mean

const char * ON = "On" ;
const char * OFF = "Off" ;

That way you won't be creating 2 copies of each string I think - the string constant and
the array initialised from it.

That way you won't be creating 2 copies of each string I think - the string constant and
the array initialised from it.

The result is the same. There is a string constant, stored at some location in memory, pointed to by an array or by a pointer.

Thank you for responses, it helped. I tryed el_supremo recommended method and it worked, LED turns on and off depending of received sms.

Good luck! :wink: