Why wont my servos react to my potentiometers

Hello everyone, hope your day is better than mine.
I am trying to figure this out and to the best of my knowledge; I just can’t get this to work.
Everything is connected correctly.  Note, I am also using separate power to the breadboard.

The problem is, the servos adjust themselves when I apply power, but they will not turn when I increase or decrease the rotation of the potentiometers (I know all servos work and the pots work as well (trust me, I tested each using a single servo and pot sketch)

I am almost positive it is an oversight, and I have been staring at it so long that, well I am most likely am overlooking the problem.

I would really appreciate your assistance.
So here is the code:

#include <Servo.h>

Servo pot1V;  // create servo object to control a servo
Servo pot2V;  // create servo object to control a servo
Servo pot3H;  // create servo object to control a servo
Servo pot4H;  // create servo object to control a servo

int mypan1 = 0;  // analog pin used to connect the potentiometer
int val0;    // variable to read the value from the analog pin
int mypan2 = 1;  // analog pin used to connect the potentiometer
int val1;    // variable to read the value from the analog pin
int mytilt3 = 2;  // analog pin used to connect the potentiometer
int val2;    // variable to read the value from the analog pin
int mytilt4 = 3;  // analog pin used to connect the potentiometer
int val3;    // variable to read the value from the analog pin

void setup()
{
  pot1V.attach(3);  // attaches the servo on pin 9 to the servo object
  pot2V.attach(5);  // attaches the servo on pin 9 to the servo object
  pot3H.attach(6);  // attaches the servo on pin 9 to the servo object
  pot4H.attach(9);  // attaches the servo on pin 9 to the servo object   
}

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

  mypan2 = analogRead(1);            // reads the value of the potentiometer (value between 0 and 1023)
  mypan2 = map(mypan2, 0, 1023, 0, 179);     // scale it to use it with the servo (value between 0 and 180)
  pot2V.write(val1);                  // sets the servo position according to the scaled value
  delay(15);                           // waits for the servo to get there

  mytilt3 = analogRead(2);            // reads the value of the potentiometer (value between 0 and 1023)
  mytilt3 = map(mytilt3, 0, 1023, 0, 179);     // scale it to use it with the servo (value between 0 and 180)
  pot3H.write(val2);                  // sets the servo position according to the scaled value
  delay(15);                           // waits for the servo to get there

  mytilt4 = analogRead(3);            // reads the value of the potentiometer (value between 0 and 1023)
  mytilt4 = map(mytilt4, 0, 1023, 0, 179);     // scale it to use it with the servo (value between 0 and 180)
  pot4H.write(val3);                  // sets the servo position according to the scaled value
  delay(15);                           // waits for the servo to get there
}

Thank you so much in advance,
Anna :)

sort by: active | newest | oldest
sspence4 years ago
Here's a simpler pot controlled servo, jusy duplicate 3 more times: http://arduinotronics.blogspot.com/2012/03/fun-with-servos.html
caitlinsdad4 years ago
1. you need to set val0=map(......)
2. you may be missing the logic to actually tell the servo to move from it's relative position or from start...the servo is dumb. From the servo example code:
for(pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(5); // waits 15ms for the servo to reach the position
}

You told the servo to go to a position but not how to get there. Good luck.
Regarding #2: actually the servo is pretty smart -- a simple servo.write(position) statement will send the servo to that position. No need to loop over incremental steps to get it there (unless you want to control the speed at which it goes there).
Abrandonmi (author)  lewisb424 years ago
Lewisb42 -
I am NOT trying to get them to turn with one pot.
I will be using 4 pots, and 4 servos. If I turn pot1 which lets say is connected to servo 1, I don't want the other 3 servos to turn.
~Anna
Thanks. I jumped into using servos with the sweep example and never thought to just try the servo.write alone. I had two hand me down servos to play with and it was tricky to get them to move in sync as they might have had different gearing and response. More fun though.
IIRC I thought the same thing in the beginning. :)

Some servos have a screw that adjusts the response (I think it mainly deals with adjusting where the center point is relative to the input signal, if memory serves) -- if you're still playing with those hand-me-downs you might look into that to help with the response issues.
Abrandonmi (author)  caitlinsdad4 years ago
Hmm interesting..I will go back and look at a few things and report back..
Thank you :)
Anna
Abrandonmi (author) 4 years ago
Thanks caitlinsdad,
I guess I didn't explain this very well, I'm sorry.
here is the new code:
#include

Servo myservo0; // create servo object to control a servo
Servo myservo1; // i added
Servo myservo2; // i added
Servo myservo3; // i added

int potpin0 = 0; // analog pin used to connect the potentiometer (i added the zero to the potpin before the equal sign)
int val0; // variable to read the value from the analog pin


int potpin1 = 1; // i added
int val1; // i added

int potpin2 = 2; // i added
int val2; // i added

int potpin3 = 3; // i added
int val3; // i added


void setup()
{
myservo0.attach(3); // attaches the servo on pin 9 to the servo object
myservo1.attach(5); // i added
myservo2.attach(6); // i added
myservo3.attach(9); // i added
}

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

val1 = analogRead(potpin1); // i added
val1 = map(val1, 0, 1023, 0, 179); // i added
myservo1.write(val1); // i added
delay(15);

val2 = analogRead(potpin2); // i added
val2 = map(val2, 0, 1023, 0, 179); // i added
myservo2.write(val2); // i added
delay(15);

val3 = analogRead(potpin3); // i added
val3 = map(val3, 0, 1023, 0, 179); // i added
myservo3.write(val3); // i added
delay(15);
}

With one pot connected, and four servos connected to the breadboard and arduino as show in the code; all turn at the same time...cant figure that out.
When running a breadboard powersource, does each servo ground need to have a common ground with the arduino?

Anna
Servos and Arduino can be tricky to debug. I've seen some issues like this before. loop() runs extremely fast compared to the response time of the servos, so if you're sending a lot of writes to the servos with that frequency they can never catch up -- or even begin to take action.

Try adding a big delay (say, 1000 ms) at the end of loop() and see what happens. If it works then, start backing off the delay until you hit a sweet spot where things work and are responsive enough for your needs.
It is one single loop...it reads the pots and tells the servo to move, goes on to read the next pot and tells the servo to move... You need to add statements to see if the pot value has changed, otherwise don't make it move. I think in your loop, it will reread the pot and increment move the same value again if you did not change the pot.

Yes, all grounds should be connected somehow and to the arduino ground.