Two inputs for one servo.

Hey all, I’m stuck with my limited knowledge of this problem.

I have two potentiometers and a push button that control two servos (3 inputs for each). The sketch runs as of now but there is a lot of noise and servo jitters because of all the conflicting input from the multiple map functions to a single servo (I think). I was able to drop the jitters from the push button by detaching the servo from the potentiometer inputs with if/else statement. Is there a way to do this with the map function as well? Hope this is explained correctly and clear enough. Any other ideas are more than welcome. Cheers.

#include <Servo.h>

//Eye movments
 Servo lr;                                                  //servo moving eye left to right       
 Servo ud;                                                  //servo moving eye up to down         
 const int X = 13;                                          //left and right eye control
 const int Y = 12;                                          //up and down right eye control
 const int browControl = 15;
 const int OC = 14;
 int servoVal;

 Servo upperLid3;
 Servo lowerLid3;
 int button1 = 4;                                           //pushbutton Pin 4
 int press1 = 0;

//Lid tracking
 Servo upperLid;                                            //servo for upper lid                    
 Servo lowerLid;                                            //servo for lower lid                    
 int servoVal1;
 int servoVal2;
 int servoVal3;

//Eye open and close
 Servo upperLid2;
 Servo lowerLid2;
 int servoVal4;
 int servoVal5;

void setup() 
//Eye movments

 pinMode(button1, INPUT);
 digitalWrite(4, HIGH);               //enable pullups to make pin high

//Lid tracking                         //May not need
 lowerLid.attach (46);
 upperLid.attach (47);

// eye open and close                  //May not need
 lowerLid2.attach (46);
 upperLid2.attach (47);

Serial.println ("READY!");


void loop() 
//Eye movments and lid tracking
 servoVal = analogRead(Y); 
 servoVal = map(servoVal, 0, 1023, 70, 50);
 ud.write(servoVal);                                  //up and down eye movement

 servoVal1 = analogRead(X);
 servoVal1 = map(servoVal1, 0, 1023, 100, 80);
 lr.write(servoVal1);                                 //left and right eye movment

 servoVal2 = analogRead(Y); 
 servoVal2 = map(servoVal2, 0, 1023, 110, 70);
 upperLid.write(servoVal2);                           //upper lid tracking
 servoVal3 = analogRead(Y);
 servoVal3 = map(servoVal3, 0, 1023, 70, 120);
 lowerLid.write (servoVal3);                         //lower lid tracking
// Blink
 press1 = digitalRead(button1);
  if (press1 == LOW){
   upperLid3.write (120);
   lowerLid3.write (120);
  else {
   upperLid.attach (47);
   lowerLid.attach (46);
   upperLid2.attach (47);
   lowerLid2.attach (46);

//Eye open and close

 servoVal4 = analogRead(OC); 
 servoVal4 = map(servoVal4, 0, 1023, 130, 50);
 servoVal5 = analogRead(OC); 
 servoVal5 = map(servoVal5, 0, 1023, 140, 50);

eyeTest1.ino (2.79 KB)

I find your code very difficult to follow because you have servo.write() all over the place. Why not have a function something like

void updateServos() {
   lowerLid.write (lowerLidVal);
   lowerLid2.write (lowerLid2Val);
   upperLid.write (upperLidVal);
   upperLid2.write (upperLid2Val);

and then all the rest of your program has to worry about is putting the correct values into the position variables.

You don't seem to be writing to the same servo from two different sources so I find it hard to understand your analysis of the problem.

Maybe you just need a deadband for the potentiometers so that the servo position only changes if there is a definite movement of the pot?



Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

What are you trying to accomplish, what is the application?

Have you got a separate powr supply for the servos, and have the ervo supply gnd connected to the Arduino gnd.

What model Arduino are you using?
What servos are you using?

Thanks.. Tom.. :slight_smile:

Impossible to work out what is going on. You define 8 Servo objects. Six of those are apparently attached to the same 2 pins so three Servo objects on each pin, randomly attached and detached. That sort of implies that you have 3 servos physically connected to each pin. Or do you only have one servo per pin but your program addresses them via 3 different Servo objects? Why?


Apologies for the messy code. This is my first project and I am learning as I go. The application is for a pair of eyes with eye lids. My goal is to have up/down, left/right movement from the eyes (pot), blinking from the eye (push button) lids, tracking of the eye lids with the up/down movement (indirect pot), and independent opening and closing of the eye lids (pot) separate from the blink.

There are 4 Servos (lr(A), ud(B), lowerLid(2,3)(C), upperLid(2,3)(D)), 3 pots (X, Y, OC), 1 push button.

pot X: controls the left/right eye movement (1 servoA).
pot Y: up/down eye movement (1 servoB) and up/down "tracking" of servoB for upper and lower lids (2 servosC/D)
pot OC: opening and close of upper and lower lids (2 servosC/D)
pushbutton(button1): controls upper and lower lid to blink (2 servosC/D)
I need them to all work together and not just one at a time to achieve fluid eye and lid movement.

I found that if I used the if/else statement to attach and detach the upperLid and lowerLid input (servos) it stopped the jittering when the push button was pressed and held. (This is good and works for me).

I'm indicating the upperLid,2,3 and lowerLid,2,3 as separate servos to not cause confusion with the 2 different map functions and push button input to each servo.

Im using a ATmega2560 with a servo shield, both individually powered by an external PC bench supply (Everything is getting more than enough power).

Robin2: I'm a bit clueless on what I would take out to replace with your suggestion.

Robin2: I'm a bit clueless on what I would take out to replace with your suggestion.

Take out all the references to servo.write() in loop() and as the last thing in loop() put this