Go Down

Topic: Simultaneous but different servo sweeps with four servos (Read 4 times) previous topic - next topic

johnwasser

OK... So one servo is moving.  If you show me the complete code I might be able to tell why the other servos are not moving.
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

PaulS

I don't think this code will work:
mythirdservo.attach(8); // attaches the servo on pin 8 to the servo object

Hint: There is a reason why there is a special button for posting code.

cloudsabio007

Code: [Select]
//QUADSweep
//edited from the original code by BARRAGAN

#include "Servo.h"

Servo myservo;  // create servo object to control a servo, a maximum of eight servo objects can be created
Servo mysecondservo;              // servo 2
Servo mythirdservo;              // servo3
Servo myfourthservo;              // servo4

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

void setup()
{
myservo.attach(9);  // attaches the servo on pin 9 to the servo object
mysecondservo.attach(10); // attaches the servo on pin 10 to the servo object
mythirdservo.attach(8); // attaches the servo on pin 8 to the servo object
myfourthservo.attach(11); // attaches the servo on pin 11 to the servo object
}


void loop()
{
unsigned long start = millis();
int sweepsNotFinished;

do
    {
    unsigned long now = millis() - start;
    sweepsNotFinished = 0;
    sweepsNotFinished += sweep(now, 500, 1500, 90, 0, myservo); // 1/2 second delay then 1 second sweep from 90 to 0
    sweepsNotFinished += sweep(now, 0, 2000, 180, 75, mysecondservo); // no delay and 2 second sweep from 180 to 75
    sweepsNotFinished += sweep(now, 250, 10000, 30, 100, mythirdservo);  // 1/4 second delay, then 9.75 second sweep from 30 to 100
   } while (sweepsNotFinished > 0);
}

int  sweep(unsigned long currentTime, unsigned long startTime, unsigned long endTime, int startPosition, int endPosition, Servo &servo)
{
if (currentTime < startTime)
    return 1;
if (currentTime > endTime)
    return 0;
servo.write( startPosition + (endPosition-startPosition) * (currentTime-startTime) / (endTime-startTime));
  return 1;
}


I understand how the delay works (I think?) but is that set of lines supposed to only apply to one servo? With that code I just have one servo which alternates between twitching and sweeping.

johnwasser

The code looks fine.  Which servo is moving? 

Are you sure you have all the servos connected correctly?  Others have found that their servos act very strangely if they accidentally swap any two of the three wires.

Are you supplying enough power to drive multiple servos?  Even one servo might be too much for USB power and four is probably too much to run off the Arduino 5V power line.  You should have a separate 5 or 6 volt power supply for the servos.  The Arduino ground should be connected to the servo's ground pin.  The Arduino data pins should connect to the servo's signal pin.
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

cloudsabio007

#9
Aug 04, 2011, 01:09 am Last Edit: Aug 04, 2011, 01:39 am by cloudsabio007 Reason: 1
Ok I've played with the numbers and I've got it much closer to where I need it to be (thankyouthankyou!). Here is my code now:

Code: [Select]
//QUADSweep
//edited from the original code by BARRAGAN

#include "Servo.h"

Servo myservo;  // create servo object to control a servo, a maximum of eight servo objects can be created
Servo mysecondservo;              // servo 2
Servo mythirdservo;              // servo3
Servo myfourthservo;              // servo4

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

void setup()
{
myservo.attach(9);  // attaches the servo on pin 9 to the servo object
mysecondservo.attach(10); // attaches the servo on pin 10 to the servo object
mythirdservo.attach(8); // attaches the servo on pin 8 to the servo object
myfourthservo.attach(11); // attaches the servo on pin 11 to the servo object
}


void loop()
{
unsigned long start = millis();
int sweepsNotFinished;

do
    {
    unsigned long now = millis() - start;
    sweepsNotFinished = 0;
    sweepsNotFinished += sweep(now, 250, 10000, 0, 90, myservo);  // 1/4 second delay, then 9.75 second sweep from 30 to 100
    sweepsNotFinished += sweep(now, 0, 1000, 0, 180, mysecondservo);  // 0 second delay, then 9.75 second sweep from 30 to 100
    sweepsNotFinished += sweep(now, 1000, 8000, 0, 180, mythirdservo);  // 1 second delay, then 9.75 second sweep from 30 to 100
    sweepsNotFinished += sweep(now, 500, 5000, 0, 90, myfourthservo);  // 1/2 second delay, then 9.75 second sweep from 30 to 100
   } while (sweepsNotFinished > 0);
}

int  sweep(unsigned long currentTime, unsigned long startTime, unsigned long endTime, int startPosition, int endPosition, Servo &servo)
{
if (currentTime < startTime)
    return 1;
if (currentTime > endTime)
    return 0;
servo.write( startPosition + (endPosition-startPosition) * (currentTime-startTime) / (endTime-startTime));
  return 1;
}



My question now is that right now I feel like I have good control of the second half of the sweep, but not the first (ie. what controls the speed of the first half of the sweep in the code?) Also, is there a way to make all the sweeps more continuous? Right now, all the servos wait until all other servos have completed their loops before restarting--is there a way that the servos can run continuously, irregardless to where the other servos are in their loops?

Go Up