Pages: [1]   Go Down
Author Topic: Controlling 2 servos with 1 potentiometer  (Read 452 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!
Logged

New Jersey
Offline Offline
Faraday Member
**
Karma: 65
Posts: 3638
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 -
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25786
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

How are the servos powered?
(Why are you reading the pot twice?)
Logged

"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.

Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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)
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

but its still no working
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25786
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

How are the servos powered?
Logged

"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.

Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

powered by the Arduino
Logged

Canada
Offline Offline
Sr. Member
****
Karma: 0
Posts: 318
Sometimes teaching, always learning,
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Maybe you forgot to attach the other servo?

  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
  myservo1.attach(10);
Logged


Offline Offline
God Member
*****
Karma: 4
Posts: 813
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Pages: [1]   Go Up
Jump to: