Problem with sketch controlling two servos

bigone5500

Apr 9, 2014
712
Joined
Apr 9, 2014
Messages
712
I have modified a sketch to attempt to control 2 servos using separate pots. The servos just jitter around and only one will move when both are connected. If I disconnect the one attached to A0, then they both move simultaneously.

Code:
// Controlling a servo position using a potentiometer (variable resistor)
// by Michal Rinott 

#include <Servo.h>
Servo myservo0;  // create servo object to control a servo
Servo myservo1;
int potpin0 = 0;  // analog pin used to connect the potentiometer
int val0;
int potpin1 = 1;
int val1;  // variable to read the value from the analog pin
void setup()
{
myservo0.attach(9);  // attaches the servo on pin 9 to the servo object
myservo1.attach(10);
}
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
val1 = analogRead(potpin1);
val1 = map(val1, 0, 1023, 0, 179);
myservo1.write(val1);
delay(15);
}
 

(*steve*)

¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd
Moderator
Jan 21, 2010
25,510
Joined
Jan 21, 2010
Messages
25,510
I have modified a sketch to attempt to control 2 servos using separate pots. The servos just jitter around and only one [servo] will move when both are connected. If I disconnect the [potentiomenter] attached to A0, then both [servos] move simultaneously.

Is this a reasonable translation of what you're seeing?
 

(*steve*)

¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd
Moderator
Jan 21, 2010
25,510
Joined
Jan 21, 2010
Messages
25,510
I thought that was the reasonable translation. You could have meant that if you disconnect one of the servos both servos move together.

What I suggest you do is to try with one pot disconnected and it's input (analog 0) tied to ground.

Then try with the other pot disconnected and it's input (analog 1) tied to ground.

What is the behaviour with both?

Are you powering the servos from the same power supply as the arduino?
 

bigone5500

Apr 9, 2014
712
Joined
Apr 9, 2014
Messages
712
After some in-depth troubleshooting, it turns out that one of my potentiometers is bad. I replaced it with a good one and the sketch works just fine...except for when you turn the pot all the way or turn it slowly, the servo that you do not want moving jitters. This is a step in the right direction I think.
 
Last edited:

(*steve*)

¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd
Moderator
Jan 21, 2010
25,510
Joined
Jan 21, 2010
Messages
25,510
I would freak out if that happened...LOL!

Yeah, but imagine if you had some sort if short between the two servos that somehow linked their control connection. The behaviour you see would be entirely consistent with that assuming the alternate reading of your text.

Whilst I considered that to be extremely unlikely, I wanted to rule it out. It's important that the problem I think you have is the same one that you actually have :D
After some in-depth troubleshooting, it turns out that one of my potentiometers is bad. I replaced it with a good one and the sketch works just fine...except for when you turn the pot all the way or turn it slowly, the servo that you do not want moving jitters. This is a step in the right direction I think.

Hahaha, it's sometimes the simplest of things. I wouldn't have suggested a faulty pot high up on my list of possibilities.

Congrats on getting the main problem resolved.

It sounds like it's still not behaving exactly like you expect though. Is that true?
 

bigone5500

Apr 9, 2014
712
Joined
Apr 9, 2014
Messages
712
Yeah, but imagine if you had some sort if short between the two servos that somehow linked their control connection. The behaviour you see would be entirely consistent with that assuming the alternate reading of your text.

Whilst I considered that to be extremely unlikely, I wanted to rule it out. It's important that the problem I think you have is the same one that you actually have :D


Hahaha, it's sometimes the simplest of things. I wouldn't have suggested a faulty pot high up on my list of possibilities.

Congrats on getting the main problem resolved.

It sounds like it's still not behaving exactly like you expect though. Is that true?
Yes, it is true. When the pots are not being turned, the servos do not move...which is great. But when you turn either one, be it slowly or fast, then the servo opposite the one you are controlling will have a bit of motion to it. Although it is just a slight motion of maybe 2 to 3 degrees in either direction. I am in the process of getting a video of the servos on here...stay tuned.
 

(*steve*)

¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd
Moderator
Jan 21, 2010
25,510
Joined
Jan 21, 2010
Messages
25,510
It sounds like it could be due to interference. Try placing 0.1uF caps across the power connections to the servos.
 

Harald Kapp

Moderator
Moderator
Nov 17, 2011
14,271
Joined
Nov 17, 2011
Messages
14,271
It says here:
Details and Caveats
The analogRead command will not work correctly if a pin has been previously set to an output, so if this is the case, set it back to an input before using analogRead. Similarly if the pin has been set to HIGH as an output, the pullup resistor will be set, when switched back to an input.
The Atmega datasheet also cautions against switching analog pins in close temporal proximity to making A/D readings (analogRead) on other analog pins. This can cause electrical noise and introduce jitter in the analog system. It may be desirable, after manipulating analog pins (in digital mode), to add a short delay before using analogRead() to read other analog pins.

Try changing your loop to:
Code:
void loop()
{
delay(7); // split 15ms delay into 2*7ms delay before each analog read
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(7); // split 15ms delay into 2*7ms delay before each analog read
val1 = analogRead(potpin1);
val1 = map(val1, 0, 1023, 0, 179);
myservo1.write(val1);
}

Another means of reducing noise on the analog measurement would be using some averaging. Your delay statements show that you have ample headroom for that. E.g. like this:
Code:
delay(7); // split 15ms delay into 2*7ms delay before each analog read
val0 = analogRead(potpin0);
val0 += analogRead(potpin0);
val0 += analogRead(potpin0);
val0 += analogRead(potpin0);
val0 = val0 >>2; // divide by 4 to average over 4 samples
 
Top