Arduino Forum upgrade scheduled for Monday, October 20th, 11am-4pm (CEST). Sorry for the inconvenience!
Pages: [1]   Go Down
Author Topic: please, compare two variables  (Read 637 times)
0 Members and 1 Guest are viewing this topic.
Brasil - SP
Offline Offline
Newbie
*
Karma: 0
Posts: 42
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

please doubts basic,

how to compare the value of two variables?

eg

var1 = 1
var2 = 2

if (var1 == var2) {
do something;
Else if} {
do something;
}

Can you help me?

thank you

Sergio
Logged

Idaho, US
Offline Offline
Edison Member
*
Karma: 23
Posts: 1280
Special User
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yes, the pseudo-code you posted is basically correct.

http://www.arduino.cc/en/Reference/If
http://arduino.cc/en/Reference/Else

Code:
byte var1 = 1;
byte var2 = 2;

if (var1 == var2) {
  // the variables are the same!
  }
else {
  // the variables were different!
  }
Logged

Brasil - SP
Offline Offline
Newbie
*
Karma: 0
Posts: 42
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

thanks for the help
but lacked information on my part.

have:

Vrsms char [13] = "011983000000" / / phone number
String VTRec / / phone number

I get a sms and store the phone number that sent the sms

Serial.println (String (info));
VNumData = String (String (info));
VTRec = (VNumData.substring (32.44));

and need to do:

if (Vrsms == VTRec) {
  SMS sends the response only to Vrsms
else if
  SMS sends the response only to Vrsms and VTRec
}

Can you help me?

Thank very you.
Logged

France
Offline Offline
Edison Member
*
Karma: 38
Posts: 1016
Scientia potentia est.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Use the function strcmp for string comparison. And I suggest you don't use the String class, use char arrays instead smiley-wink
Logged

Brasil - SP
Offline Offline
Newbie
*
Karma: 0
Posts: 42
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

thank you very much.
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 234
Posts: 14296
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

ANd when you are comparing floats there is another trick you should know

Code:
float var1 = 1.0;
float var2 = sin(355/113/2);
float threshold = 0.001; // to be defined

if ( abs(var1 - var2) < threshold )
{
  // the variables are considered the same!
}
else
{
  // the variables were different!
  }

with floats you always have to use < or > or <= or >=  in tests. The == fails quite often
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Idaho, US
Offline Offline
Edison Member
*
Karma: 23
Posts: 1280
Special User
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

with floats you always have to use < or > or <= or >=  in tests. The == fails quite often
Heck I like to use <= or >= for integers too. Helps cut down on infinite loops and other undesirable behavior when a variable reaches an unanticipated value.
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 234
Posts: 14296
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@ tylernt

Very true, especially if the integers are of the volatile kind used in an interrupt! They can be increased an unknown number of times before tested.
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Brasil - SP
Offline Offline
Newbie
*
Karma: 0
Posts: 42
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

thank you  very much
Logged

Pages: [1]   Go Up
Arduino Forum upgrade scheduled for Monday, October 20th, 11am-4pm (CEST). Sorry for the inconvenience!
Jump to: