Go Down

Topic: How to write servo direction correctly (Read 373 times) previous topic - next topic

warboyzzzz

hi, i want to ask how to write a servo code for arduino. i'm using a single servo attached to pin 10, 5v, and ground. I want the servo to turn first at 0 then to 179, from 179 back to 0 and stop (i want it turn full 180 twice, backwar forward)

#include <Servo.h>
Servo servoMain;

void setup()
{
   servoMain.attach(10);
}

void loop(){
servoMain.write(0);
delay(750);
servoMain.write(179);
delay(750);
}


i have try this code, it turn out backward and forward, but it wont stop. it keeps doing that. can anyone help me how? thanks

billroy

Move the code in loop() to setup() and it will only be executed once.

-br

PaulS

Quote
i'm using a single servo attached to pin 10, 5v, and ground.

Wrong. You need a separate power supply for the servo.

Quote
it keeps doing that.

Of course it does. That's what loop does.

If all that the Arduino is ever supposed to do is move the servo twice, move the code from loop() to setup().

warboyzzzz


Quote
i'm using a single servo attached to pin 10, 5v, and ground.

Wrong. You need a separate power supply for the servo.

Quote
it keeps doing that.

Of course it does. That's what loop does.

If all that the Arduino is ever supposed to do is move the servo twice, move the code from loop() to setup().


ok, i have try that and it work! thanks b4, i'm still new at this thing

warboyzzzz


Move the code in loop() to setup() and it will only be executed once.

-br



thanks so much, i'm still new so thanks for the support!

patduino

Hi warboyzzzz,

Its doing exactly what you programmed it to do 8) As billroy says, you have to move the code out of the 'loop' function.  Loop repeats forever...

Pat.
There are 10 types of people in the world, those that understand binary, and those that don't.

JimboZA



Wrong. You need a separate power supply for the servo.



In the OP's defence, even the tutorial on this very site shows a servo hooked up to an Arduino for its power...
Roy from ITCrowd: Have you tried turning it off an on again?
I'm on LinkedIn: http://www.linkedin.com/in/jimbrownza

PaulS

It is just about impossible to get anything on the Tutorial page fixed. It took an inordinate amount of time to get a resistor added to the blink page.

warboyzzzz


Hi warboyzzzz,

Its doing exactly what you programmed it to do 8) As billroy says, you have to move the code out of the 'loop' function.  Loop repeats forever...

Pat.

thanks a lot pal, simple thing but i wont budged haha. thanks again for the support!  :)

Go Up