Go Down

Topic: Controlling 2 servos with 1 potentiometer (Read 537 times) previous topic - next topic

andrewa

Hi, I'm new to electronics and I couldn't figure out how to control 2 servos with 1 potentiometer.  I tried the servo KNOB code and I tried changing it but the servos just start moving and I have no control. Someone help!

wildbill

Post the code you have so far - hard to diagnose the issue without. Also, how have you wired the servos up?

andrewa

Here is my code:

#include <Servo.h>

Servo myservo;  // create servo object to control a servo
Servo myservo1; // create another servo object to control a servo

int potpin = 0;  // analog pin used to connect the potentiometer
int val;    // variable to read the value from the analog pin

void setup()
{
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
myservo.attach(10);
}

void loop()
{
  val = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and 1023)
  val = map(val, 0, 1023, 0, 179);     // scale it to use it with the servo (value between 0 and 180)
  myservo.write(val);                  // sets the servo position according to the scaled value       
  val = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and 1023)
  val = map(val, 0, 1023, 0, 179);     // scale it to use it with the servo (value between 0 and 180)
  myservo1.write(val);                  // sets the servo position according to the scaled value
  delay(15);                           // waits for the servo to get there
}

I have the servos power to Vin the signals to 9 for 1 servo and 10 for the other, and - to -

AWOL

How are the servos powered?
(Why are you reading the pot twice?)
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

andrewa

I was reading the pots twice , once for each servo (in my code it has a small mistake with the myservo.write(val)the second one should be myservo1.write(val)


AWOL

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.


John_S

Maybe you forgot to attach the other servo?

  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
  myservo1.attach(10);
http://jsrintervalometers.blogspot.ca

jwatte


powered by the Arduino


Even small hobby servos can pull 1 amp each when encountering resistance. The Arduino 5V rail should really only power up to 500 mA. It may be that the power goes out and the Arduino resets when both servos get moving. Try powering them directly from a 6V power supply of some sort.

Also, reading the analog value twice, "once for each servo," seems unnecessary. The potentiometer won't have moved much between the first and the second reading, so you'll just get back the same value you got the first time, anyway.

Go Up