Go Down

Topic: please, compare two variables (Read 690 times) previous topic - next topic

sergiotux

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

tylernt

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

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

Code: [Select]
byte var1 = 1;
byte var2 = 2;

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

sergiotux

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.

guix

Use the function strcmp for string comparison. And I suggest you don't use the String class, use char arrays instead ;)

sergiotux


robtillaart

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

Code: [Select]

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
Rob Tillaart

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

tylernt


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.

robtillaart

@ 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.
Rob Tillaart

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

sergiotux


Go Up