Instructables

Can someone help me write a code for an Arduino to power a servo?

Hey I'm looking at doing a project and I need 2 servos to sweep their arms alternately from about 140  to 180 degrees and use a push button to turn it on and off. Can someone please help me with this? Thanks!

robo103025 months ago

how much of a delay do you want between each time the servo stops and the other servo starts?

TELLERMAN4 (author)  robo103025 months ago

Hey the sketch didn't work the servo plugged into 9 was unable to complete it's full rotation. I hate to ask again, but could you write another code but only for one servo to move from about 180 to 160 with no delay between movements? Thanks for the help by the way.

Sure no prob here :-

// Sweep

// by BARRAGAN <http://barraganstudio.com>

// This example code is in the public domain.

#include <Servo.h>

Servo myservo; // create servo object to control a servo

// a maximum of eight servo objects can be created

int pos = 0; // variable to store the servo position

void setup()

{

myservo.attach(9); // attaches the servo on pin 9 to the servo object

}

void loop()

{

for(pos = 160; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees

{ // in steps of 1 degree

myservo.write(pos); // tell servo to go to position in variable 'pos'

delay(15); // waits 15ms for the servo to reach the position

}

for(pos = 180; pos>=160; pos-=1) // goes from 180 degrees to 0 degrees

{

myservo.write(pos); // tell servo to go to position in variable 'pos'

delay(15); // waits 15ms for the servo to reach the position

}

}

tell me if it works

TELLERMAN4 (author)  robo103025 months ago

Well I think the code is what i want but the angles are wrong how do i change the angle in the code for the servo to move?

Ok thats not a problem you just need to change the number values in these sentences. for(pos = 140; pos < 180; pos += 1) change the 140 and 180 to the angle u want the numbers refer to the angles. So this for(pos = 140; pos < 180; pos += 1) says take the servo to angle of 140 and then increment it to 180.

Hope this helps :) by the way what are you making?

TELLERMAN4 (author)  robo103025 months ago

Yea that helps tremendously thank you I got the code to work the way I needed it too thanks again and I'm making a paintball gun sentry turret and I needed something to pull the trigger besides my finger obviously and I had a couple servos around and so I'm here haha thanks again

Happy to hear that would love to see some photos once your done.

Good luck on that :)

robo103025 months ago

hope this helps

// Sweep

// by BARRAGAN <http://barraganstudio.com>

// This example code is in the public domain.

#include <Servo.h>

Servo myservo; // create servo object to control a servo

Servo myservo1; // a maximum of eight servo objects can be created

int pos = 0; // variable to store the servo position

void setup()

{

myservo.attach(9);

myservo1.attach(10); // attaches the servo on pin 9 to the servo object

}

void loop()

{

for(pos = 140; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees

{ // in steps of 1 degree

myservo.write(pos); // tell servo to go to position in variable 'pos'

delay(15); // waits 15ms for the servo to reach the position

}

for(pos = 180; pos>=140; pos-=1) // goes from 180 degrees to 0 degrees

{

myservo.write(pos); // tell servo to go to position in variable 'pos'

delay(15); // waits 15ms for the servo to reach the position

delay(1000);

}

{

for(pos = 140; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees

{ // in steps of 1 degree

myservo.write(pos); // tell servo to go to position in variable 'pos'

delay(15); // waits 15ms for the servo to reach the position

}

for(pos = 180; pos>=140; pos-=1) // goes from 180 degrees to 0 degrees

{

myservo.write(pos); // tell servo to go to position in variable 'pos'

delay(15);

delay(1000); // waits 15ms for the servo to reach the position

}

}