Jerky servo!!!

I have made a robot with 2 gear motors, a led and a servo

I have written in my code that when I press 1,2,3,7,5,9 some functions go on like the robot moving forward etc

The 2 motors are used to move the robot and the servo and led are used as a gun turret.

When I give commands to my motors my servo goes jerky for a while and stops but some times it goes jerky and never stops unless I plug out the arduino.

Please help me to solve this problem

The code is :

#include <Servo.h>

int LedPin1 = 12;
int LedPin = 13;
int MotorPin1 = 9;
int MotorPin2 = 10;
int servoPin = 11;

Servo VishalServo;

void setup()
{
digitalWrite(LedPin, HIGH);
Serial.begin(9600);
pinMode(LedPin, OUTPUT);
pinMode(LedPin1, OUTPUT);
pinMode(MotorPin1, OUTPUT);
pinMode(MotorPin2, OUTPUT);
VishalServo.attach(servoPin);
VishalServo.writeMicroseconds(1500);
delay(2000);
}

void loop()
{
while (Serial.available() == 0);
int val = Serial.read() - '0';

int i = VishalServo.read();

if (val == 2)
{
Serial.println("Robot is On");
digitalWrite(MotorPin1, HIGH);
digitalWrite(MotorPin2, HIGH);
delay (1000);
digitalWrite(MotorPin1, LOW);
digitalWrite(MotorPin2, LOW);
}

if (val == 1)
{
Serial.println("Robot is turning left");
digitalWrite(MotorPin2, HIGH);
delay(1000);
digitalWrite(MotorPin2, LOW);
}

if (val == 3)
{
Serial.println("Robot is turning right");
digitalWrite(MotorPin1, HIGH);
delay(1000);
digitalWrite(MotorPin1, LOW);
}

if (val == 7)
{
Serial.println("Turning left");
VishalServo.write(i+30);
}

if (val == 9)
{
Serial.println("Turning right");
VishalServo.write(i-30);
}

if (val == 5)
{
Serial.println("Shoot");
digitalWrite(LedPin1, HIGH);
delay(25);
digitalWrite(LedPin1, LOW);
}

if (val == 8)
{
Serial.println("Centering");
VishalServo.writeMicroseconds(1500);
}

else
{
Serial.println("Invalid!");
}
}

Cross posted.

What does cross posted mean

It means you've been very very naughty and upset people by posting the same question in different parts of the forum.

This wastes time, so don't do it.

When I give commands to my motors my servo goes jerky for a while and stops but some times it goes jerky and never stops unless I plug out the arduino.

Probably inadequate power supply.

Ok AWOL

But could anyone please tell me how to solve this problem

I will give you a link to where I bought the servo:
http://cgi.ebay.com/2X-Micro-9g-Servo-Futaba-Hitec-HS-55-GWS-walkera-RC-/320669312249?pt=Radio_Control_Parts_Accessories&hash=item4aa96164f9
And the servo jerks even when I connect it to 3 V or 5 V on the arduino.

Please reply fast as I only have a couple more days to finish this project and submit it to my school for a science lesson.

Thanks

Im having the same issue, the servo shakes when stopping in a position.
And sometimes the program stops responding so i have to restart the circuit.
I also believe that it has something to do with power supply because i think a got rid of the unresponsiveness or lock of the program by adding some caps on the power rails to provide maybe a little bit more of juice when the servo needs it.
The Jerkiness and shaking was reduced a little and also giving instructions to the servo not to fast reduces it.

Try with some caps from VCC to GND, add several values in parallel for example 0.1uf, 1uf, 10uf and 100uf.

Regards.

zoomkat could you tell me how to make the power supply adequate the....
Please.....

Ok i'll try

But I do not have any capacitors, Ihve lost about 100 in a packet I had before.I'snt there any other way...

You don't need caps, just a separate supply with adequate capacity.

AWOL:
You don't need caps, just a separate supply with adequate capacity.

how do I make that have connected all the grounds together except the Servo's and the LED's as those two grounds have been connected to the arduino's ground inputs,but I have connected one of the arduino's ground pin to the rest of the grounds of the motors,batteries etc.

So could you please tell me how to send it a seperate supply with adequate capacity.

Thanks

Someone please give me instruction on what to do to get rid of these problems(detailed instructions if possible)

Thanks

Four AA cells should do the trick.

What do you mean... How and where am I supposed to connect those 4 AA batteries

To the servo - where else?
Don't forget to common the grounds.

When i give commands to the servo and/or the LED it works fine, but when I give commands to the motors it jitters and jerks, and the motors also go on only for half of a second or 1 and a half of a second sometimes when they are supposed to go on for 1 second.

How do I stop this happening

When I remove the servo then everything works fine but when I add the servo the above problem occurs...

Please help...

9V batteries are rubbish for powering high current devices like motors and servos.
Don't power anything other than LEDs off the Arduino.

But if I add 4 AA batteries then the robot will become too heavy and will not be able to move...So is there no other alternative.

maybe you should redesign your robot then? you could look for a flat lithium battery pack, it would be lighter and smaller, but you would need extra circuitry to charge it and prevent it from being discharged too much.