Pages: [1]   Go Down
Author Topic: Multiple variables for an if statement  (Read 132 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Does anyone know how to put more than one variable,(or  number in my case of a serial monitor) into an if statement. I'm trying to have a between value like x<1000 but >800 in one if statement. Here's how my code is setup so it can be put into context.
Code:
void loop(){

  float lightLevel = readTSL230(TSL230_samples);
  Serial.println(lightLevel);

if(lightLevel<1000)
  {
    myservo1.attach(5);
    myservo1.writeMicroseconds(1300);delay(1000); 

}
if(lightLevel>4000)
{
  myservo1.attach(5);
  myservo1.writeMicroseconds(1700);delay(1000);
}
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
have a between value like x<1000 but >800
Code:
if(x > 800 && x < 1000)
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 211
Posts: 13471
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 if not sure of the precedence of the operators you can always use extra ()
Code:
if ( (x > 800) && (x < 1000) )
{

Technically this is equal to
Code:
if ( x > 800)
{
  if ( x < 1000)
  {
    ....
  }
}

The compiler creates short circuit evaluation of compound if statements.
This means that with a statement in the form 
- IF (A && B)   the expression B is not evaluated if  A is false.
- IF (A || B)  the expression B is not evaluated if A is true

Logged

Rob Tillaart

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

Pages: [1]   Go Up
Jump to: