I am trying to control 2 servos independently with 2 potentiometers. the code i am using controls both servos in unison, which i do not want. can somebody explain to me what i have wrong in my code? Thanks
Try the below code which will print out to the serial monitor to see what is happening. I changed to analog input pins to match my pot connection and the servo pins to allow use of the serial monitor on pins 0 and 1 for debugging.
//zoomkat dual pot/servo test 12-2912
#include <Servo.h>
Servo myservo1;
Servo myservo2;
int potpin1 = 0;Â //my pot pin
int potpin2 = 1;
int newval1, oldval1;
int newval2, oldval2;
void setup()
{
 Serial.begin(9600);Â
 myservo1.attach(2);Â
 myservo2.attach(3);
 Serial.println("testing dual pot servo");Â
}
void loop()
{
 newval1 = analogRead(potpin1);    Â
 newval1 = map(newval1, 0, 1023, 0, 179);
 if (newval1 < (oldval1-2) || newval1 > (oldval1+2)){Â
  myservo1.write(newval1);
  Serial.print("1- ");
  Serial.println(newval1);
  oldval1=newval1;
 }
 newval2 = analogRead(potpin2);
 newval2 = map(newval2, 0, 1023, 0, 179);
 if (newval2 < (oldval2-2) || newval2 > (oldval2+2)){Â
  myservo2.write(newval2);
  Serial.print("2- "); Â
  Serial.println(newval2);
  oldval2=newval2;
 }
 delay(50);
}
I'm guessing it has something to do with having attached the servos to pins 1 and 2 not say 9 and 10.
But I also note you have no delay as recommended in the Knob example as shown arrowed below. I wonder if that's part of the problem?
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
 delay(15);             // waits for the servo to get there  <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
}
Incidentally I have code very similar to yours to drive two servos from a joystick (which is two pots in one mechanism) with the pots read on A4 and A5 and servos attached to pins 9 and 10..... works as expected.