Pages: [1]   Go Down
Author Topic: Comparing senderNumber[20] to integer  (Read 2138 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 22
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
if (senderNumber[20] < 40000000) {
  x=1;
  fixer = 1;

  } 

Thank you for your help guys - I appreciate it.

Akash
Logged

Montreal
Offline Offline
Edison Member
*
Karma: 23
Posts: 2485
Per aspera ad astra.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Try 40000000UL  , where UL - unsigned long
Logged

Madrid (Spain)
Offline Offline
Full Member
***
Karma: 0
Posts: 103
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
char myCharNumber[20] = "2000";
int myIntNumber = atoi(myCharNumber);
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 22
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:

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
Logged

Madrid (Spain)
Offline Offline
Full Member
***
Karma: 0
Posts: 103
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
  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:
  Serial.begin(9600);
  char myCharNumber[20] = "+656543344";
  char senderNumber[20] = "+656543344";

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

Offline Offline
Newbie
*
Karma: 0
Posts: 22
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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
Logged

Madrid (Spain)
Offline Offline
Full Member
***
Karma: 0
Posts: 103
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,

Try with this code:
Code:
if(received.compareTo("On") == 0) {

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

}
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 22
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Madrid (Spain)
Offline Offline
Full Member
***
Karma: 0
Posts: 103
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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/
« Last Edit: June 10, 2013, 05:42:59 am by davidgoth » Logged

Pages: [1]   Go Up
Jump to: