Go Down

Topic: can you independently control 3 servos? (Read 1 time) previous topic - next topic

budsiskos

Jun 15, 2009, 02:37 am Last Edit: Jun 15, 2009, 02:37 am by budsiskos Reason: 1
is it possible to use an arduino duemilanove to control 3 servos independently?

thanks

Mike Mc

#1
Jun 15, 2009, 03:05 am Last Edit: Jun 15, 2009, 03:06 am by zooto68 Reason: 1
With the Mega Servo Library you can control 12 servos (48 if you have a Mega)

With the Software Servo library you can control 20 Servos at the same time.

Take a look at the Servo section in the Playground.

afap

well i spent about an hour looking where you told me to, and here is what i came up with. Can you please look over it and tell me if its right? I have lots of wires to hook up and i need the code to be right.  Thank you!

Code: [Select]
#include <Servo.h>

Servo myservo1;  // create servo object to control a servo
Servo myservo2;
Servo myservo3;  // create servo object to control a servo
Servo myservo4;

int potpin1 = 0;  // analog pin used to connect the potentiometer
int potpin2 = 1;
int potpin3 = 2;  // analog pin used to connect the potentiometer
int potpin4 = 3;
int val;    // variable to read the value from the analog pin

void setup()
{
 myservo1.attach(9);  // attaches the servo on pin 9 to the servo object
 myservo2.attach(10);  // attaches the servo on pin 10 to the servo object
 myservo3.attach(5);  // attaches the servo on pin 5 to the servo object
 myservo4.attach(6);  // attaches the servo on pin 6 to the servo object
}

void loop()
{
 val = analogRead(potpin1);            // reads the value of the potentiometer (value between 0 and 1023)
 val = map(val, 0, 1023, 179, 0);     // scale it to use it with the servo (value between 0 and 180)
 myservo1.write(val);                  // sets the servo position according to the scaled value
 val = analogRead(potpin2);            // reads the value of the potentiometer (value between 0 and 1023)
 val = map(val, 0, 1023, 0, 179);     // scale it to use it with the servo (value between 0 and 180)
 myservo2.write(val);
 val = analogRead(potpin3);            // reads the value of the potentiometer (value between 0 and 1023)
 val = map(val, 0, 1023, 0, 179);     // scale it to use it with the servo (value between 0 and 180)
 myservo3.write(val);
 val = analogRead(potpin4);            // reads the value of the potentiometer (value between 0 and 1023)
 val = map(val, 0, 1023, 179, 0);     // scale it to use it with the servo (value between 0 and 180)
 myservo4.write(val);                  // sets the servo position according to the scaled value
                   // sets the servo position according to the scaled value
 delay(15);                           // waits for the servo to get there
}

Groove

#3
Jul 07, 2009, 08:14 am Last Edit: Jul 07, 2009, 09:24 am by GrooveFlotilla Reason: 1
looks reasonable to me, apart from the first line.
The standard servo library only controls two servos.

Code: [Select]

#include <MegaServo.h>

#define N_SERVOS 4
Servo myservo [N_SERVOS];  // create servo object to control a servo
const byte servoPin [N_SERVOS] = {9, 10, 5, 6};
const byte potPin [N_SERVOS] = {0, 1, 2, 3};
int i;

void setup()
{
 for (i = 0; i < N_SERVOS; i++) {
   myservo [i].attach(servoPin[i]);
 }
}

void loop()
{
 for (i = 0; i < N_SERVOS; i++) {
   myservo[i].write(map(analogRead(potPin [i]), 0, 1023, 179, 0));
 }
 delay(15);
}
Per Arduino ad Astra

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy