SERVO MOTOR POSITIONING

I am trying to rotate a servo motor for 45 degrees and hold it there for 2 seconds and bring it back to 0 degrees again… but the motor moves to an extent and then stops…and only sound keeps coming…this is my code

#include <Servo.h>
Servo servoMain; // Define our Servo

void setup()
{
servoMain.attach(10); // servo on digital pin 10
}

void loop()
{
servoMain.write(45); // Turn Servo Left to 45 degrees
delay(1000); // Wait 1 second
servoMain.write(0); // Turn Servo Left to 0 degrees
delay(1000); // Wait 1 second
servoMain.write(90); // Turn Servo back to center position (90 degrees)
delay(1000); // Wait 1 second
servoMain.write(135); // Turn Servo Right to 135 degrees
delay(1000); // Wait 1 second
servoMain.write(180); // Turn Servo Right to 180 degrees
delay(1000); // Wait 1 second
servoMain.write(90); // Turn Servo back to center position (90 degrees)
delay(1000); // Wait 1 second
}

How is your servo powered?

..and how is it wired?

And read the two links in my signature -

Duane B

its a 5 volt servo..so i hve connected it directly to the arduino uno board and powering through usb..and weird as in it rotates in the beginning then gradually reduces and moves like very very very slowly which cant be seen by the naked eye but the moving sound keeps coming...i need this motor to go upto 45 degree and come back to 0 degree after a 2 sec delay

Have you got a common ground?

so i hve connected it directly to the arduino uno board and powering through usb

When you power the servo correctly, it will behave correctly.

Did you read the two links in my signature -

yes, I read and I understand that power supply is the problem, But can anybody check my code that i have posted and tell me if its correct

I am trying to rotate a servo motor for 45 degrees and hold it there for 2 seconds and bring it back to 0 degrees again.

Well the code you have certainly doesn't do that. But other than that it looks like it would move a servo around ok. You may need to adjust the limit cases (0 and 180) a bit to suit your particular servo - it may not be able to make it that far.

wildbill, thanks but neglect the 180 and 135 degree parts. just servomain.write(45) then a delay(2000) and then servomain.write(0)…this will do the job right?

Duane B thanks a lot for your help with those links. so i power the arduino board with a 9 volt battery and power the servos separelty with a 6 volt aa pack ..all this after loading the program through arduino..thats it right? can i keep the board connected to the usb instead of using the 9 volt battery?

so i power the arduino board with a 9 volt battery and power the servos separelty with a 6 volt aa pack ..all this after loading the program through arduino..thats it right? can i keep the board connected to the usb instead of using the 9 volt battery?

Yes and yes.

servomain.write(45) then a delay(2000) and then servomain.write(0)..this will do the job right?

You'll learn more if you just try it.

I have got the servo to rotate..it goes to 45 degrees and comes back to 0. how do i know make it rotate to -45 degrees that is 45 degrees in the opposite direction from 0?

You could post your code.

Hey guys, I have made a servo motor rotate from 0 to 45 and back to 0. now i need to rotate in the opposite direction of 45 degrees from 0. does setting the angle to -45 in servomain.write() work or is there any other way?

#include <Servo.h>
Servo servoMain; // Define our Servo

void setup()
{
servoMain.attach(10); // servo on digital pin 10
}

void loop()
{
servoMain.write(45);
delay(1000);
// Wait 1 second
servoMain.write(0);
delay(1000);
servoMain.write(-45);
delay(1000);

}

Does the servo write method take negative parameters? I don't think so.

Do not cross post.

then how do i make it rotate to 45 degrees in the other direction?

Offset your turn by 45 degrees.