Go Down

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

#### akashroy

##### May 18, 2013, 12:04 am
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

#1
##### May 18, 2013, 01:17 am
Try 40000000UL  , where UL - unsigned long

#### davidgoth

#2
##### May 20, 2013, 08:39 am
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

#3
##### May 21, 2013, 03:20 am
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

#4
##### May 21, 2013, 09:03 am
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

#5
##### Jun 01, 2013, 01:30 am
Thank you guys for help!

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

#6
##### Jun 03, 2013, 09:05 am
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

#7
##### Jun 08, 2013, 12:00 am
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 amLast 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.