Comparing senderNumber[20] to integer

Hey guys,

I am attempting to compare a received number and send a message according to the number it was, but the problem persists.

if (senderNumber[20] < 40000000) {
  x=1;
  fixer = 1;

  }

Thank you for your help guys - I appreciate it.

Akash

Try 40000000UL , where UL - unsigned long

To compare a char array with a integer, you must cast your char array to a integer type. You can do it with this function:

int atoi (const char * str);

For example:

char myCharNumber[20] = "2000";
int myIntNumber = atoi(myCharNumber);

I have tried both answers. Although it is compiling, it doesn't seem to make the right comparison. I might not have structured the code out right, but have tried putting different variables where the comparison occurs.

char myCharNumber[20] = "+12223333";
int myIntNumber = atoi(myCharNumber);


    Serial.println("\nEND OF MESSAGE");
  if (senderNumber[20] < myCharNumber[20]) {
  x=1;
  fixer = 1;
  Serial.println("X=1");
  
  }

Thank you all for the help

Akash

You are comparing two array of characters. If you want to compare two numbers mathematically, you should compare two integer variables, in other words, you should cast myCharNumber and senderNumber to integer with atoi function. If you want to compare two array for know if they are exactly equal (with letters and numbers), you should use strcmp(array1, array2).

Comparison of two numbers mathematically:

  char myCharNumber[20] = "+12223333";
  char senderNumber[20] = "+656543344";
  int myIntNumber = atoi(myCharNumber);
  int myIntSenderNumber = atoi(senderNumber);
  
  if(myIntSenderNumber < myIntNumber)
  {
    Serial.println("SenderNumber is less than myCharNumber");
  }
  else
  {
    Serial.println("SenderNumber is greater than myCharNumber");
  }

Comparison of two arrays (character by character):

  Serial.begin(9600);
  char myCharNumber[20] = "+656543344";
  char senderNumber[20] = "+656543344";

  if(strcmp(senderNumber, myCharNumber) == 0)
  {
    Serial.println("Equal");
  }
  else
  {
    Serial.println("Not equal");
  }

Thank you guys for help!

David - I have a question for you regarding a topic relating to this- I have seen your posts about this:

if (received.compareTo("On")) {

  lightlight = 1;
    
  Serial.println("LED ON");
}
else if (received.compareTo("Off")) {
  
  lightlight = 0;
  

}

I have put the lightlight variable in the void loop as:

boolean lightlight;

  • The LED turns on, but does not turn off after it has been texted the word "Off"

I thank you guys for the help, I appreciate it.

Akash

Hi,

Try with this code:

if(received.compareTo("On") == 0) {

  lightlight = 1;
    
  Serial.println("LED ON");
}
else if (received.compareTo("Off") == 0) {
  
  lightlight = 0;
  

}

Hey !

I've tried that, but it makes it all stop working. With the other code the LED turns on, but does not turn off.

If you have any ideas, please let me know.

Thank you Dave and everyone for helping.

Thank you

Are you using String object? or char array?. The code that I posted works in my Arduino UNO.

Read this:
If you use String, compareTo() - Arduino Reference
If you use char array, http://www.cplusplus.com/reference/cstring/strcmp/