Multiple Servo control

Hi,

I did this code so that I can control two servos at the same time, but it doesn’t work and the potentiometers interfer with each other, and with the two servos and potentiometers hooked up, the servos go wild. I also noticed that the arduino disconnect itself when the potentiometer is turned to fast. I would like to know how to control different servos with one potentiometer per servo, using the pwm outputs.
Here’s the code:

#include <Servo.h> 
 
Servo myservo1;
Servo myservo2;
 
int potpin1 = 0;
int potpin2 = 1;
int val1;  
int val2;
 
void setup() 
{ 
  myservo1.attach(9);  
  myservo2.attach(10);

 
void loop() 
{ 
  val1 = analogRead(potpin1);            
  val1 = map(val1, 0, 1023, 0, 179);      
  myservo1.write(val1);                 
  delay(15);                             
  val2 = analogRead(potpin2);           
  val2 = map(val2, 0, 1023, 0, 179);     
  myservo2.write(val2);                  
  delay(15);
}

Thanks!

Let me guess... you're powering the servos from the Arduino? Don't use pwm to drive servos.

That code won’t even compile - it’s missing a closing brace for setup().

In setup(), add a call to Serial.begin(). Add a call to Serial.println() to show that you are in setup().

In loop(), add calls to Serial.print() and Serial.println(). Print out the values of val1 and val2.

Some simple servo code for two servos you can use with the serial monitor to get your servos working.

// zoomkat 10-20-11 serial servo (2) test
// for writeMicroseconds, use a value like 1500
// for IDE 0022 and later
// Powering a servo from the arduino usually DOES NOT WORK.
// two servo setup with two servo commands
// send eight character string like 15001500 or 14501550

#include <Servo.h> 
String readString, servo1, servo2;
Servo myservo1;  // create servo object to control a servo 
Servo myservo2;

void setup() {
  Serial.begin(9600);
  myservo1.attach(6);  //the pin for the servo control 
  myservo2.attach(7);
  Serial.println("servo-test-22"); // so I can keep track of what is loaded
}

void loop() {

  while (Serial.available()) {
    delay(1);  
    if (Serial.available() >0) {
      char c = Serial.read();  //gets one byte from serial buffer
      readString += c; //makes the string readString
    } 
  }

  if (readString.length() >0) {
      Serial.println(readString); //see what was received
      
      // expect a string like 07002100 containing the two servo positions      
      servo1 = readString.substring(0, 4); //get the first four characters
      servo2 = readString.substring(4, 8); //get the next four characters 
      
      Serial.println(servo1);  //print to serial monitor to see results
      Serial.println(servo2);
      
      int n1; //declare as number  
      int n2;
      
      int n1 = servo1.toInt();
      int n2 = servo2.toInt();
      
      myservo1.writeMicroseconds(n1); //set servo position 
      myservo2.writeMicroseconds(n2);
    readString="";
  } 
}

PaulS: That code won't even compile - it's missing a closing brace for setup().

In setup(), add a call to Serial.begin(). Add a call to Serial.println() to show that you are in setup().

In loop(), add calls to Serial.print() and Serial.println(). Print out the values of val1 and val2.

Can you tell me how to do that please? I'm a beginner in the arduino world. :cold_sweat:

I'm a beginner in the arduino world.

Apparently you missed the first 6 chapters of the book, then. Serial output, for debugging purposes is covered long before servos. Did you even look at the reference material for Serial.begin() and Serial.print()?

Being a beginner is no excuse for not even trying.

Besides, it's all set out in zoomkat's example.

I know how to use the serial.print and the serial.begin , i just didn't understand how it would help me in my case, wanting to control the servos with potentiometers.

I also noticed that the arduino disconnect itself when the potentiometer is turned to fast.

Disconnects itself from one? Does some arm come out of the Arduino and start jerking out wires?

i just didn't understand how it would help me in my case, wanting to control the servos with potentiometers.

Well, for one thing, you KNOW if the Arduino was resetting. For another, you'd see what values were being read from the analog pins.

Let me guess... you're powering the servos from the Arduino?

Are you going to answer this question?

When i say disconnect itself, i mean resets itself. I am using a 6 volt batterie to power the servos.

The rate at which you turn the potentiometers should have no affect on the Arduino. The analogRead() function is affected by changing input voltages to the pin being measured, but there is no way that the changing voltage affects the Arduino, unless the voltage is out of range.

So, we can rule out a coding issue. If you are not powering the servos through the Arduino, then we can rule out the servos causing the Arduino to brown out.

That still leaves a multitude of possibilities, A schematic would be useful, or a Fritzing diagram at the least.

I changed the code a bit, and it’s actually working right now. I uses the SoftwareServo librairy avaible on the Arduino website. But there’s still a little problem. When everything is connected the servos tremble. Is that normal? Maybe I also need to add that the 2 potentiometer aren’t the same. I know one of them is 10K, but for the other one it’s not written on it, so I don’t know.

Here’s the code:

#include <SoftwareServo.h> 

SoftwareServo myservo1; 
SoftwareServo myservo2; 

int potpin1 = 0;  
int potpin2 = 1; 
int vala;  
int valb; 

void setup() 
{ 
  myservo1.attach(2);  
  myservo2.attach(1);
} 

void loop() 
{ 
  vala = analogRead(potpin1);           
  vala = map(vala, 0, 1023, 0, 179);     
  myservo1.write(vala);                  
  delay(15);                            

  valb = analogRead(potpin2);             
  valb = map(valb, 0, 1023, 0, 179);     
  myservo2.write(valb);                  
  delay(15);                        

  SoftwareServo::refresh();
}

And here’s the fritzing schematic:

http://imageshack.us/f/202/servobb.jpg/

I know one of them is 10K, but for the other one it's not written on it, so I don't know.

Test it. You do have a multimeter, don't you? If not, you need one. You know you do.

When everything is connected the servos tremble. Is that normal?

In your case, yes. The SoftwareServo::Refresh() function needs to be called more often than you are calling it - at least once every 20 milliseconds. The two 15 millisecond delays mean that you are calling the refresh function less than once every 30 milliseconds.

Why did you change to a software implementation of a hardware function?

PaulS:
You do have a multimeter, don’t you? If not, you need one. You know you do.

I need to buy a new one because i broke mine a few days ago so right now i don’t have any.

Why did you change to a software implementation of a hardware function?

I’m sorry i don’t understand your question .

I'm sorry i don't understand your question .

You were using the Servo class/library. That uses the hardware to refresh the servo instances at the right interval.

Now, you are using the SoftwareServo class/library. That relies on software to do what the hardware (the Arduino) can do.

If the hardware can do the job, it is generally better to let it.

So, the question is why aren't you.

I was just trying to find a way to make it work, so i tried with the SoftwareServo library to see if it worked. Do you advise me to go back to the Servo library?

Do you advise me to go back to the Servo library?

Yes, I do.

I tried with this code, it works, but the servos still tremble.

#include <Servo.h> 
 
Servo myservo1;
Servo myservo2; 
 
int potpin1 = 0;
int potpin2 = 1;
int vala;  
int valb;
 
void setup() 
{ 
  myservo1.attach(2);  
  myservo2.attach(1);  
} 
 
void loop() 
{ 
  vala = analogRead(potpin1);             
  vala = map(vala, 0, 1023, 0, 179);      
  myservo1.write(vala);                   
  delay(15);  
  
  valb = analogRead(potpin2);          
  valb = map(valb, 0, 1023, 0, 179);      
  myservo2.write(valb);                   
  delay(15);
}

I tried with this code, it works, but the servos still tremble.

If the servos move when the potentiometers are not being moved, it is not a software problem. I would guess that they need more current than you are supplying.

I tried to power them with a 5V 1A wall adaptater instead of a 6V batterie, but it doesn’t work, the servos get a cut signal, for exemple when I turn the potentiometer, i have to wait 0.5 seconds (approximately) for the servo starts turning, then it turns for 0.5 seconds, then it stops turning for 0.5 seconds, then it starts again for 0.5 seconds, and that until it reaches the right place.