servo is not working properly

HI ,

I am new guy in robotics . i have connected a servo with arduino and two IR leds RX ( receiver ) and TX ( transmitter ) . but the problem is the servo is moving on its own . its moving and coming back to its orginal position on its own .

this is the skech ..

#include <Servo.h>
Servo myservo;
int LedPin = 13;
int inputPin = 1;
int ir1 = 70;
int obs = 0;
int pos = 0;
int turn = 0;

void setup()
{
myservo.attach(9);
pinMode(LedPin, OUTPUT);
Serial.begin(9600);
}

void loop()
{
if (turn == 1)
{
Serial.print("\n");
Serial.print("I am turning! :/");
digitalWrite(LedPin, HIGH);
delay(1000);
digitalWrite(LedPin, LOW);
Serial.print("I am not turning anymore! sigh \n");

turn = 0;
myservo.write(-90);
delay(1500);
}

int ir2 = analogRead(inputPin);

if(ir1 < ir2-10 && obs != 1)
{
obs = 1;
ir1 = ir2;
if (turn != 1)
{
myservo.write(90);
delay(1500);
turn = 1;
}

}

if (ir1 > ir2+10 && obs == 1)
{
obs = 0;
ir1 = ir2;
}

Serial.print(obs);
Serial.print("\n");
}

while serial monitering it is showing that transmitter is working on its own .. guyz plsss help me out ... :frowning: :frowning: :frowning:

Hi Lucky,
First thing, can you go back an modify your post?
Highlight the sketch and then click the # button on the editor.
This puts the sketch into a code window that makes it easier to read.

Second, your angles for the servo.write need some help.
The angle range is 0-180, not -90 to 90
90 centers the servo.
0 is full left
180 is full right
Anything less that zero is treated as zero.

Also make sure you've got the grounds connected.

Also make sure you've got the grounds connected.

I don't think you
connected the
grounds, Dave.

I still chuckle every time I read this!