Need help on the IF-statement

Hi WildBill,

thanks for your comments. :slight_smile:
First...hehehe...newbie question: with brackets you mean { or ( ?

And I do need both because when the sensor is out of range (being further away then 3 to 4 meters) it doesn't "see"anything and writes down a "0". So that's why I need both conditions to be checked. Under no condition the servo's may react when the count is zero.

So here's what my code looks like after your help...is this what you are trying to tell me? :wink:

void loop() 
{
  delay(100);                                        // Wait 100ms between pings. 29ms should be the shortest delay between pings.
  unsigned int uS = sonar.ping();                    // Send ping, get ping time in microseconds (uS).
  Serial.print("Ping: ");
  Serial.print(uS / US_ROUNDTRIP_CM);                // Convert ping time to distance in cm and print result (0 = outside set distance range)
  Serial.println("cm");
}
{
 if ((uS / US_ROUNDTRIP_CM) != 0) && ((uS / US_ROUNDTRIP_CM) > 50)
  {for(pos = 0; pos < 180; pos += 1)      // goes from 0 degrees to 180 degrees 
   {                                      // in steps of 1 degree 
     myservo.write(pos);                  // tell servo to go to position in variable 'pos' 
     delay(15);                           // waits 15ms for the servo to reach the position 
   }
  }
}
{
 if ((uS / US_ROUNDTRIP_CM) != 0) && ((uS / US_ROUNDTRIP_CM) < 50)
  {for(pos = 180; pos>=1; pos-=1)         // goes from 180 degrees to 0 degrees 
   {                                      // in steps of 1 degree 
     myservo.write(pos);                  // tell servo to go to position in variable 'pos' 
     delay(15);                           // waits 15ms for the servo to reach the position 
   }
  }
}