Reversing the servo code? (Ping sensor controlled servo)

My code works fine but every time I change the else value with if value it freaks out and the ping sensor seems to stop working, can anyone help with this?

The code:

#define trigPin 12
#define echoPin 11
#include <Servo.h>

Servo myservo;

void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
myservo.attach(9);

}

void loop() {
int duration, distance,pos=0,i;
digitalWrite(trigPin, LOW);
delayMicroseconds(4);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
Serial.print(distance);
Serial.println(" cm");

if(distance<50)
{
myservo.write(90);
delay(10000);
}
else{
myservo.write(10);
}

delay(100);

}

every time I change the else value with if value

What does that mean?

If you are attempting to power the servo from the Arduino, don't. Use a separate power supply and connect the grounds.

Hi,

Can you please post a copy of your sketch, using code tags?
They are made with the </> icon in the reply Menu.
See section 7 http://forum.arduino.cc/index.php/topic,148850.0.html

Thanks. Tom… :slight_smile: