RE: Using 10 servos to operate a remote turning target

I wonder if some one could help me please with an ongoing project.
I'm trying to control 10 servos for a turning target.

I currently use an adapted version of the servo 'test' sketch which sequences through the servos one at a time making them turn 90º, hold for 3 seconds then turn back 90º, but what I'd like to introduce is a random element to the sequence.
So ideally a target turns but is then marked so as not to be selected again until all 10 servos have activated then the sequence stops.

Is that possible?

C_Eaton:
servo6.attach(6);
servo7.attach(7);
servo8.attach(8);
servo9.attach(9);
servo10.attach(10);

I know emoji make your code look cool but it does not make it readable. Please read How to use the forum on how to post code.

Questions to understand the problem:

C_Eaton:
which sequences through the servos one at a time making them turn 90º, hold for 3 seconds then turn back 90º,

That's not what I see in code. I see that you turn all servo's in sequence. But that turn from 0 to 90 is a slow and done in 4,5 minute. After which you leave that servo at 90 and move to the next servo.

Do you really want that slow servo movement?

Do you randomly want to select a servo, move that to 90, wait 3 seconds, move it a back to 0 and start this over?

Do you want the random to exclude the ones that already moved (until you've moved all ten in random order)?

And a tip, use arrays. That way you don't have to repeat everything for every servo, it's scaleable and it's easy to numerically pick a servo.

Thanks for the quick reply!
The smiley wasn’t intentional, it seems that the forum inserted it automatically when it saw (8) in the code.

That’s not what I see in code. I see that you turn all servo’s in sequence. But that turn from 0 to 90 is a slow and done in 4,5 minute. After which you leave that servo at 90 and move to the next servo.
Do you really want that slow servo movement?

No, ideally the servo movement should be within 15m/s or so. I’ll have a look at the timing.

Do you randomly want to select a servo, move that to 90, wait 3 seconds, move it a back to 0 and start this over?

Yes, exactly this.

Do you want the random to exclude the ones that already moved (until you’ve moved all ten in random order)?

Again yes, so that each servo only get activated once in each 10 action sequence.

And a tip, use arrays. That way you don’t have to repeat everything for every servo, it’s scaleable and it’s easy to numerically pick a servo.

I’ve been looking at arrays but can’t find anything specific to servo control, it all seems to be LED orientated, is there a resource for this please?

//Creating for loop

//Add the servo library. This library is standart library
#include <Servo.h>

//Define our servos
Servo servo1;
Servo servo2;
Servo servo3;
Servo servo4;
Servo servo5;
Servo servo6;
Servo servo7;
Servo servo8;
Servo servo9;
Servo servo10;

//servo position in degrees
int servoPos = 0;

void setup()
{
  //Define serco signal inputs (Digital PWM 3-5-6-9-11)
  servo1.attach(1);
  servo2.attach(2);
  servo3.attach(3);
  servo4.attach(4);
  servo5.attach(5);
  servo6.attach(6);
  servo7.attach(7);
  servo8.attach(8);
  servo9.attach(9);
  servo10.attach(10);
}
void loop()
}   
  //scan from 0 to 90 degrees
for(servoPos = 0; servoPos < 90; servoPos++)
{
servo1.write(servoPos);
delay(3000);
}
  //scan from 0 to 90 degrees
for(servoPos = 0; servoPos < 90; servoPos++)
{
servo2.write(servoPos);
delay(3000);
}
  //scan from 0 to 90 degrees
for(servoPos = 0; servoPos < 90; servoPos++)
{
servo3.write(servoPos);
delay(3000);
}
  //scan from 0 to 90 degrees
for(servoPos = 0; servoPos < 90; servoPos++)
{
servo4.write(servoPos);
delay(3000);
}
  //scan from 0 to 90 degrees
for(servoPos = 0; servoPos < 90; servoPos++)
{
servo5.write(servoPos);
delay(3000);
}
  //scan from 0 to 90 degrees
for(servoPos = 0; servoPos < 90; servoPos++)
{
servo6.write(servoPos);
delay(3000);
}
  //scan from 0 to 90 degrees
for(servoPos = 0; servoPos < 90; servoPos++)
{
servo7.write(servoPos);
delay(3000);
}
  //scan from 0 to 90 degrees
for(servoPos = 0; servoPos < 90; servoPos++)
{
servo8.write(servoPos);
delay(3000);
}
  //scan from 0 to 90 degrees
for(servoPos = 0; servoPos < 90; servoPos++)
{
servo9.write(servoPos);
delay(3000);
}
  //scan from 0 to 90 degrees
for(servoPos = 0; servoPos < 90; servoPos++)
{
servo10.write(servoPos);
delay(3000);
}
}
  servo0.detach();
  servo1.detach();
  servo2.detach();
  servo3.detach();
  servo4.detach();
  servo5.detach();
  servo6.detach();
  servo7.detach();
  servo8.detach();
  servo9.detach();     
}

C_Eaton:
No, ideally the servo movement should be within 15m/s or so. I’ll have a look at the timing.

Aka as fast as possible :slight_smile: For that, the delay is in the wrong place. Also, no need for a for-loop. Just servo.write(90) it

C_Eaton:
Yes, exactly this.

Okay, really look into array’s, that will make this part easy.

C_Eaton:
Again yes, so that each servo only get activated once in each 10 action sequence.

Okay, this will be the hard part. Not really because of code but the logic behind it. I would suggest you first try to make a sketch that picks one of the 10 servo’s at random, make it do flip-flop and start over. From there we can work it out.

C_Eaton:
I’ve been looking at arrays but can’t find anything specific to servo control, it all seems to be LED orientated, is there a resource for this please?

Not that I know of. But doesn’t really mater. They may use leds to give examples, but the array does not apply to leds, it applies to variables. Writing

Servo targetServos[10];
//is also perfectly vallid and gives you
targetServos[0];
//to
targetServo[9];
//The benefit of that is that instead of a fixed number you can also use another variable
for(byte i = 0; i < 10; i++){
  targetServos[i];
}
//loops over all servos for example

Excellent thank you, starting to get some where.

I'll look into the random function.

This illustrates setting up an array of servos and attaching them using an array of pins and for loops.

#include <Servo.h>

const byte numServos = 4;
const byte servoPins[numServos] = {3,4,5,6};

Servo targetServos[numServos];

void setup()
{
   Serial.begin(115200);
   
   for(int n = 0; n < numServos; n++)
   {
      targetServos[n].write(90);  // or desired initial position
      targetServos[n].attach(servoPins[n]);
      delay(1000); // so they don't all move at once
   }   
}

void loop()
{
   for(int n = 0; n < numServos; n++)
   {
      targetServos[n].write(10);
      delay(2000);
      targetServos[n].write(90);
      delay(500);
   }
}

To get rid of the delays, use millis() for timing. See the beginner’s guide to millis(). and several things at a time.