Go Down

Topic: Comparing senderNumber[20] to integer (Read 2 times) previous topic - next topic

akashroy

Hey guys,

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

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

  } 


Thank you for your help guys - I appreciate it.

Akash

Magician

Try 40000000UL  , where UL - unsigned long

davidgoth

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:
Code: [Select]

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

akashroy

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.

Code: [Select]


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

davidgoth

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:
Code: [Select]

  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):
Code: [Select]

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

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

akashroy

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:

Code: [Select]
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

davidgoth

Hi,

Try with this code:
Code: [Select]

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

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

}


akashroy

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

davidgoth

#8
Jun 10, 2013, 10:03 am Last Edit: Jun 10, 2013, 12:42 pm by davidgoth Reason: 1
Are you using String object? or char array?. The code that I posted works in my Arduino UNO.

Read this:
If you use String, http://arduino.cc/en/Reference/StringCompareTo
If you use char array, http://www.cplusplus.com/reference/cstring/strcmp/

Go Up