Arduino refuses to command refuses to command two servos.

I am working on a small two-motored kit for a class project. I am using an Arduino Uno with a motor shield attached to it, and two Analoge Servos attached to that. The problem is when I attempt to connect two servos in my code, the whole thing seems to immediately lock up and not allow me to write to the servos.

Here is code that demonstrates my dilemma:
#include <SoftwareServo.h>

SoftwareServo leftServo;
SoftwareServo rightServo;

void setup() {
pinMode(2, OUTPUT);
pinMode(4, OUTPUT);
leftServo.attach(2);
}

void loop() {
for (pos = 0; pos < 180; pos += 1) {
leftServo.write(pos);
delay(15);
}
rightServo.attach(4); //<— Output goes dead here
for (pos = 0; pos < 180; pos += 1) {
rightServo.write(pos);
delay(15);
}
}

I've never used SoftwareServo nor a shield to handle servos, but I suspect this is a power supply problem.

Where do the servos get their power from? Seems like as soon as the second one draws current, and the first one will still be getting its regular position updates (at least if software servo works like normal servo) the current draw is too high.

As a matter of interest why do you have one servo attach in setup() and the other in loop()?

The software servo implementations used to require a refresh method to be called regularly.
I don't know if that's the case, but it is worth checking

Hi,

  rightServo.attach(4); //<--- Output goes dead here

Shouldn’t that be in setup like the other attach?

Can you please post a copy of your sketch, using code tags?
They are made with the </> icon in the reply Menu.
See section 7 http://forum.arduino.cc/index.php/topic,148850.0.html

Thanks… Tom… :slight_smile:

This may work…

#include <Servo.h>
int pos;
Servo leftServo;
Servo rightServo;

void setup() {
  pinMode(2, OUTPUT);
  pinMode(4, OUTPUT);
  leftServo.attach(2);
  rightServo.attach(4);
}

void loop() {
  for (pos = 0; pos < 180; pos += 1) {
    leftServo.write(pos);
    delay(15);
  }
  for (pos = 0; pos < 180; pos += 1) {
    rightServo.write(pos);
    delay(15);
  }
}