perhaps a little help with servo control??

Hello everyone,

Here’s my goal, its pretty simple - just a little prototype for a larger project…

I need to be able to control 9 servo’s from one board (Decimilia) independently. I’m a bit new at writing code however, so my approach was to write separate functions for each servo, and then call those functions in a main loop in whatever order was necessary to achieve the desired effect.

I have some code that simply runs all of them at once, (just a test to see if my power supply was sufficient) -
how could I modify this or re-write the code to get independent control and get to a choreographed sequence of actuations?

#define SERVOa 13
#define SERVOb 12
#define SERVOc 11
#define SERVOd 10
#define SERVOe 9
#define SERVOf 8
#define SERVOg 7
#define SERVOh 6
#define SERVOi 5


#define MIN_PULSE 600
#define MAX_PULSE 2400
#define DELTA 1800


int lastPulse = 0;
int refreshTime = 20;

long randNumber;

void setup()
{
  int i;
  pinMode(SERVOa, OUTPUT);
  pinMode(SERVOb, OUTPUT);
  pinMode(SERVOc, OUTPUT);
  pinMode(SERVOd, OUTPUT);
  pinMode(SERVOe, OUTPUT);
  pinMode(SERVOf, OUTPUT);
  pinMode(SERVOg, OUTPUT);
  pinMode(SERVOh, OUTPUT);
  pinMode(SERVOi, OUTPUT);
  
  digitalWrite(SERVOa, LOW);
  digitalWrite(SERVOb, LOW);
  digitalWrite(SERVOc, LOW);
  digitalWrite(SERVOd, LOW);
  digitalWrite(SERVOe, LOW);
  digitalWrite(SERVOf, LOW);
  digitalWrite(SERVOg, LOW);
  digitalWrite(SERVOh, LOW);
  digitalWrite(SERVOi, LOW);

  
  Serial.begin(9600);
  for (i=0; i<10; i++)
  {
    digitalWrite(SERVOa, HIGH);
    digitalWrite(SERVOb, HIGH);
    digitalWrite(SERVOc, HIGH);
    digitalWrite(SERVOd, HIGH);
    digitalWrite(SERVOe, HIGH);
    digitalWrite(SERVOf, HIGH);
    digitalWrite(SERVOg, HIGH);
    digitalWrite(SERVOh, HIGH);
    digitalWrite(SERVOi, HIGH);
    
    delayMicroseconds((MIN_PULSE + MAX_PULSE)/2);
    
    digitalWrite(SERVOa, LOW);
    digitalWrite(SERVOb, LOW);
    digitalWrite(SERVOc, LOW);
    digitalWrite(SERVOd, LOW);
    digitalWrite(SERVOe, LOW);
    digitalWrite(SERVOf, LOW);
    digitalWrite(SERVOg, LOW);
    digitalWrite(SERVOh, LOW);
    digitalWrite(SERVOi, LOW);
    
    delay(20);
  }
  Serial.println("servo should be at center");

  delay(20);
}

void loop()

{
  int i;
  static int pulse_width;
  
  randNumber = random(600, 2400);
  delay(3000);
  
  pulse_width = randNumber;
  Serial.println(pulse_width, DEC);
  
 /* 
  if (millis() - lastPulse >= refreshTime) {
    digitalWrite(SERVOa, HIGH);
    digitalWrite(SERVOb, HIGH);
    digitalWrite(SERVOc, HIGH);
    digitalWrite(SERVOd, HIGH);
    digitalWrite(SERVOe, HIGH);
    digitalWrite(SERVOf, HIGH);
    digitalWrite(SERVOg, HIGH);
    digitalWrite(SERVOh, HIGH);
    digitalWrite(SERVOi, HIGH);
    
    delayMicroseconds(pulse_width);
    
    digitalWrite(SERVOa, LOW);
    digitalWrite(SERVOb, LOW);
    digitalWrite(SERVOc, LOW);
    digitalWrite(SERVOd, LOW);
    digitalWrite(SERVOe, LOW);
    digitalWrite(SERVOf, LOW);
    digitalWrite(SERVOg, LOW);
    digitalWrite(SERVOh, LOW);
    digitalWrite(SERVOi, LOW);
    
    delay(20);
    
    lastPulse = millis();
  }
}
*/
  
    for (i=0; i<100; i++)
 {
      digitalWrite(SERVOa, HIGH);
      digitalWrite(SERVOb, HIGH);
      digitalWrite(SERVOc, HIGH);
      digitalWrite(SERVOd, HIGH);
      digitalWrite(SERVOe, HIGH);
      digitalWrite(SERVOf, HIGH);
      digitalWrite(SERVOg, HIGH);
        digitalWrite(SERVOh, HIGH);
        digitalWrite(SERVOi, HIGH);

      delayMicroseconds(pulse_width);

      digitalWrite(SERVOa, LOW);
      digitalWrite(SERVOb, LOW);
      digitalWrite(SERVOc, LOW);
      digitalWrite(SERVOd, LOW);
      digitalWrite(SERVOe, LOW);
      digitalWrite(SERVOf, LOW);
      digitalWrite(SERVOg, LOW);
        digitalWrite(SERVOh, LOW);
        digitalWrite(SERVOi, LOW);

      delay(20);
  }
}

thanks in advance !!

Q. Was the supply adequate to power all 9 servo's ?

Q. Was the supply adequate to power all 9 servo's ?

well .. somewhat ... ;)

I'm running a 9v 500Ma power supply - it pushes them all at once, however there is a slight lag time on some of them, and I could see that they're not running at full speed or power.

Leads me to believe that it could be two things - the refresh time in the code is too long for this amount of motors, or I need more amperage.

running that many servos on so little current is goign to spell trouble.
You need at least a couple amps to power 9 servos properly… they draw 1/4A easy.
They should be on a separate power supply too-- not on the Arduino supply.

running that many servos on so little current is goign to spell trouble. You need at least a couple amps to power 9 servos properly.... they draw 1/4A easy. They should be on a separate power supply too-- not on the Arduino supply.

ok thanks, I'll look for another power supply

yes try and run them separately... feeding the servos with a 6V battery would be perfect.