Running multiple servos that randomly rotate

I built a 3 axis skull for Halloween, the skull can: nod, spin and tilt side to side using servos.

The sketch I made will sweep a servo a random number of degrees; it then will pause for a random amount of time and then sweep back a random numbers of degrees again. I define the allowable travel range (for example, between 45 and 135 degrees). I also control the speed of the servo to keep the movements looking realistic.

I am very happy with how the sketch works; the problem is that I cannot figure out how to run 3 servos at the same time. I am using a “for loop” to control the speed and I cannot figure out how to get 3 random movements at the same time (e.g. Servo 1 moves 35 degrees while Servo 2 moves 95 degrees). I also need to be able to set the allowable range for each servo independently (e.g. Servo 1 can move from 45 to 135 degrees and Servo 2 can move from 25 to 155 degrees).

This is the most complicated sketch I have made to date to give you a understanding of my skill (or lack thereof) level.

#include <Servo.h>

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

int target; // where servo arm is to move
int last = 90; // servo are starting point
int now = 90;  // current servo are location

void setup() {
  nServo.attach(9);
  nServo.write(now);
}

void loop() {
  delay(random(1500, 3500)); //sets time between movements
  target = random(25, 155); //sets value to move servo arm to and define allowable range

  if (now <= target) {  // if-else decides what direction arm needs to move

    for (now = last; now <= target; now++) {  // for loop moves servo 1 degree at a time and controls speed
      nServo.write(now);
      delay(20);
    }
  }
  else {
    for  (now = last; now > target; now--) {
      nServo.write(now);
      delay(20);
    }
  }
  last = target;  // resets starting point of servo arm
}

The for loop style of coding needs to be ditched, unfortunately.

This thread may give you some ideas: apart from not having the randomness you require it seems it does what you want for two servos sweeping independently.

You'll need to think about how to throw some randomness into the mix ;). But Halloween's still a while...

To make the servos move asynchronously, you can't use loops or delay statements. The thread posted by meltDown is similar to what you want to do- pay close attention to the loop() function in the last post.

Here is another post that may help.

Thanks for the advice, by using the timing strategy in "several things at one time" instead of delays I was able to make things work. Because of the delays in my original code I was going to run the jaw (synced to a sound level meter) off of a separate Arduino. Now that I'm not using delays I can just use 1 Arduino for both functions (move 3 axis's randomly and sync the jaw to speech). :slight_smile:

Just a update (and thanks to those who offered advice).

Still have some more tweaks:

Very good, Karma++. :slight_smile: