Go Down

Topic: Multiple variables for an if statement (Read 267 times) previous topic - next topic

djm049

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: [Select]
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);
}

PaulS

Quote
have a between value like x<1000 but >800

Code: [Select]
if(x > 800 && x < 1000)

robtillaart

and if not sure of the precedence of the operators you can always use extra ()
Code: [Select]

if ( (x > 800) && (x < 1000) )
{


Technically this is equal to
Code: [Select]

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

Rob Tillaart

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

Go Up