Pages: [1]   Go Down
Author Topic: How to write servo direction correctly  (Read 303 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 21
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

0
Offline Offline
God Member
*****
Karma: 39
Posts: 988
Get Bitlash: http://bitlash.net
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

-br
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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().
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 21
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 21
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!
Logged

Indiana, US
Offline Offline
Full Member
***
Karma: 12
Posts: 204
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi warboyzzzz,

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

Pat.
Logged

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

Johannesburg. UTC+2
Offline Offline
Faraday Member
**
Karma: 82
Posts: 3846
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


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...
Logged

The Elders of the Internet know who I am.....
Get me on LinkedIn: za.linkedin.com/in/jimbrownza/

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 21
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi warboyzzzz,

Its doing exactly what you programmed it to do smiley-cool 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!  smiley
Logged

Pages: [1]   Go Up
Jump to: