random servo programming

One tiny problem…that code doesn’t use a PCA9685 board. So either you want to use one of those or you don’t. Which is it? The code will be very different in the two cases. The standard Servo library can run servos at full speed so “servos too slow” is a complete red herring.

Oh and its pointless posting code that you have never tried. You can’t compile and load code without saving it so unless you deliberately went back and deleted your original code it’s still there somewhere. Have a look under File/Open recent.

Steve

I was using the own board as it seems to move the servos smoother. But if I can do it just through the nano then I'd be happy with that.

Bit I would rather use the pwm

juss1972:
So now I need to add a random setting for when and how many servos are activated.

Can you describe what you want the resulting 'random' movement to look like? Do the servos always sweep the same amount? Is the 'when' part just a random delay between sweeps and the chosen set of servos sweep together?

Yes all the servos will move from 0 to 90 degrees in one complete move. Wait around five seconds then go back to 0. With different lengths of time between the start of the sweep and a different length of time before the next sweep and to randomly use any servo for that movement. And also to pick whether 1,2,3 or four servos do the sweep. So basically as long as they go from 0 to 90 and have a five second delay at that 90 it's totally random what servo moves and when it does it

your loop() function will have to look something like this

void loop() {
  unsigned long startDelay = random(5000, 10000);  // wait to start 5-10 seconds

  unsigned long pauseDelay = random(4000, 6000); // pause between 4-6 seconds
  int servoCount = 1 + random(4); // 1-4 servos to use this time


  bool enabled1 = random(100) > 50;
  bool enabled2 = random(100) > 50;
  bool enabled3 = random(100) > 50;
  bool enabled4 = random(100) > 50;

  delay(startDelay);

  for (int pos = 0; pos <= 100; pos += 3) {
    if ( enabled1 ) myservo1.write(pos);
    if ( enabled2 ) myservo2.write(pos);
    if ( enabled3 ) myservo3.write(pos);
    if ( enabled4 ) myservo4.write(pos);
    delay(15);                       // waits 15ms for the servo to reach the position
  }

  delay(pauseDelay);

  for (int pos = 100; pos >= 0; pos -= 3) {
    if ( enabled1 ) myservo1.write(pos);
    if ( enabled2 ) myservo2.write(pos);
    if ( enabled3 ) myservo3.write(pos);
    if ( enabled4 ) myservo4.write(pos);
    delay(15);                       // waits 15ms for the servo to reach the position
  }
}

You could also put your servos (and then enabled) into an array

1 Like

Thank I'll give it a go and see what it does

I've tried that sketch and it's not accepting it. I will have a mess a post tomorrow with any problems

juss1972:
I've tried that sketch and it's not accepting it. I will have a mess a post tomorrow with any problems

That was not a sketch. It contains no setup() function, nor the necessary #include or declaration of the servo objects. It was just an example of what loop() would have to look like.

You will have to make it a complete sketch.

I'm trying to add the setup for the four servos but I'm still struggling. Like I said I've no experience at all in this

I think I've done it it's complied the sketch so just to try it now

Hey thanks it's working. The only thing I need to change on it is the initial starting position when I power it up. The servo go to the open position of what I'm using them for and they need to be at the opposite angle... Closed. What do I change in the sketch or add to it to do this. But thanks again

juss1972:
Hey thanks it's working. The only thing I need to change on it is the initial starting position when I power it up. The servo go to the open position of what I'm using them for and they need to be at the opposite angle... Closed. What do I change in the sketch or add to it to do this. But thanks again

If you do a servo.write(position); BEFORE servo.attach(pin); then the servo will move to that position rather than the default position of 90°.
To avoid the case of no servos moving for a cycle I would use this method of picking servos to move:

int pattern = random(1, 16);  // Pick a number from 1 to 15
  bool enabled1 = pattern & 1;  // Bit 0
  bool enabled2 = pattern & 2;  // Bit 1
  bool enabled3 = pattern & 4;  // Bit 2
  bool enabled4 = pattern & 8;  // Bit 3

johnwasser:
If you do a servo.write(position); BEFORE servo.attach(pin); then the servo will move to that position rather than the default position of 90°.
To avoid the case of no servos moving for a cycle I would use this method of picking servos to move:

int pattern = random(1, 16);  // Pick a number from 1 to 15

bool enabled1 = pattern & 1;  // Bit 0
  bool enabled2 = pattern & 2;  // Bit 1
  bool enabled3 = pattern & 4;  // Bit 2
  bool enabled4 = pattern & 8;  // Bit 3

Good improvement. Good to have at least one rather than a flip of a coin 4 times.

The longer they're shut the better to be honest. But can it mess it up if none move on a cycle.