If statement does not work?

Hello people,

I have the followong code:

#include <AltSoftSerial.h>
#include <FileIO.h>
#include <Process.h>

AltSoftSerial altSerial;

// Incoming data
char     sReceived[19];     // The valid string can contain 18 bytes and a terminating null

void setup() {
  Bridge.begin();	// Initialize the Bridge
  Serial.begin(9600);	// Initialize the Serial
  FileSystem.begin();
  // Wait until a Serial Monitor is connected.
  while(!Serial);

  altSerial.begin(4800);
}

void loop(){
  char   cByte     = -1;
  char   cStatus   = -1;
  String sHexChar  = "";
  
  if (altSerial.available()) {
    // Create a string array with the complete data
    cByte = altSerial.read(); 
    AddToReceived(cByte);
    
    // See if the string matches a valid string (yet)
    if ( MatchPattern() ) {
      cStatus = getStatus();
      
      sHexChar = CharToHex(cStatus);
      Serial.println("Status Hex = " + sHexChar );
      Serial.println("Status Chr = " + CharToHex(cStatus) );
      
// Do something with status 00	  
//      if (cStatus = '\0' )
//      if (CharToHex(cStatus) = "00" ){
      if (sHexChar = "00" ){
        Serial.println("-----> Status is '00'");
        StatusMessage(cStatus);
      }
      
// Do something with status 21	  
//      if (cStatus = '\21') 
//      if (CharToHex(cStatus) = "21"){ 
      if (sHexChar = "21"){ 
        Serial.println("-----> Status is '21'");
        StatusMessage(cStatus); 
      }
    }  
  }
}


//-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
void StatusMessage(char cChar){
	Serial.println("Status is: '" + (String)cChar +  "'"); // For now, just print it
}

//-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
char getStatus(){
  return sReceived[12];
}

//-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
void AddToReceived(char cByte)
{ // sReceived is a global variable
  // We add information ad the end, discarding each first character when we reached a full buffer
  //                       0   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18 
  // Example sReceived = [  ][  ][  ][  ][  ][  ][  ][  ][  ][  ][  ][  ][  ][  ][  ][  ][01][02][03]
  // becomes sReceived = [  ][  ][  ][  ][  ][  ][  ][  ][  ][  ][  ][  ][  ][  ][  ][01][02][03][04]
  // and
  // Example sReceived = [01][02][03][04][05][06][07][08][09][0a][0b][0c][0d][0e][0f][10][11][12][13]
  // becomes sReceived = [02][03][04][05][06][07][08][09][0a][0b][0c][0d][0e][0f][10][11][12][13][14]

  // Shift all characters one position to the left and add the new character to the new position
  for (int i=0; i <= 17; i++){
    sReceived[i] = sReceived[i+1];
  }
  sReceived[18] = cByte;  
}

//-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
String CharToHex (char cByte)
{ String sHex = "";
  // Get the character received and return the same information converted to it's hexadecimal form.
  sHex  = "0" +String(cByte,HEX);            // Put a zero in front of each value, making '0' into '00' and '11' into '011' and 'ffa3' into '0ffa3'
  sHex  = sHex.substring(sHex.length()-2);   // Get the last two characters, leaving '00' at '00' and turning '011' to '11' and even '0ffa3' into 'a3'
  return sHex;  
}

//-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
boolean MatchPattern()
{
  if (sReceived[17] == '\3')              // STX byte (Start of transmission) 
    if (sReceived[ 1] == '\2')            // ETX byte (End   of transmission)
      return true;                        // Yup.... This is the one!

  // No match.... So return false.
  return false;
}

The part that is giving me headaches is this:

// Do something with status 00	  
//      if (cStatus = '\0' )
//      if (CharToHex(cStatus) = "00" ){
      if (sHexChar = "00" ){
        Serial.println("-----> Status is '00'");
        StatusMessage(cStatus);
      }
      
// Do something with status 21	  
//      if (cStatus = '\21') 
//      if (CharToHex(cStatus) = "21"){ 
      if (sHexChar = "21"){ 
        Serial.println("-----> Status is '21'");
        StatusMessage(cStatus); 
      }

Why?
Well… see my output in the Serial Monitor:

Status Hex = 00
Status Chr = 00
-----> Status is '00'
[StatusMessage] Status is: ' '
-----> Status is '21'
[StatusMessage] Status is: ' '

I have tries every form of comparing I can imagine.
(Some of them are still in the comments)
Both if-statements will produce results and I don’t get it.
How come?
Do I test the wrong variables?
Do I need an other way of comparing?

I just don’t see the problem, and being more and more creative did not help me.
Please tell me what is wrong with my ways of comparing

      if (sHexChar = "00" ){

This is an assignment statement, not an equality test.

= != ==

if (cStatus = '\21') --- this statement sets the value of cStatus to '\21' and then checks to see of cStatus is non-zero which it now is.

What you want is

if (cStatus == '\21')

Bites us all one time or another.

 if (sHexChar == "21")

won't work either, you compare strings with strcmp, not ==

you compare strings with strcmp, not ==

But, sHexChar isn't a string. It's a String, with a piss poor name.

MarkT: if (sHexChar == "21")

won't work either, you compare strings with strcmp, not ==

PaulS: But, sHexChar isn't a string. It's a String, with a piss poor name.

sHexChar ........ My vision in naming the variable was: It's a String, hence 's'. It's a HEX notation of the char variable, hence HexChar. So..... that's how I came to sHexChar. But I'm allways open to suggestion to better myself. :)

The if statement.....It's a newbie error, I acknowledge that. :-) And if (sHexChar == "21") works. (BTW: I did try the '==' before I posted and got an error that I was mixing C and C++ commands........) Thanks for the assistance. The correct message it send at the correct time now.. :)

However..... I get the feeling from you comments, there is a better way to work with stings. Can you direct met to some reading material, or just explain me what to do better?

Don't get too concerned about other people's opinions. If the variable means something to you, it's good.

If the variable means something to you, it's good.

Unless is only means something today.

I've seen crap like this: int myFloat; float myInt; int someFloats[] = {5, 6, 7};

While the names might have meant something when the code was created, a week later, they wont.

To me, char in a name implies ONE character. chars implies more than one. sHexChars is would have cringed, but passed on. sHexValue I would have accepted. sHexString is a better description of what the variable contains, and would have been acceptable to me Except for using the useless String class in the first place.

I see. Maybe sCharInHex wouldv'e been more descriptive, if not a bit long. Keep in mind though, it is one char, just in hex notation, which takes two digits to represent one byte (char).