Ok, so I finally produced a code that works (nearly) as intended.
I say nearly because upon start-up the servos both write to 0 and need to be pushed out of the work envelope (to the other side seemingly) before they will move incrementally. However, they both will operate as intended once they are un-stuck. ![]()
Yes, I did make numerous attempts to correct this, none successful. Some of these include writing both servos to 90 (center) in setup. Nope. Another was to constrain the possible written values to inside 0 -180, hoping it would not try to move outside of the servo's range. Again, no dice. I will continue to seek solutions to this issue.
I ended up using small bits from all of our ideas. I did make the servo angles global, and I also used miriths code for producing new angles.
I also used the map function over constrain because I simply could not get constrain to produce the desired result. I mapped the pot values to 13 and -15 to keep the numbers small and thereby keeping the speed of movement down, and they are different to allow for the slight inaccuracy of the resting state of the potentiometer. Yes they are negative. That is of no concern, the issue is when the number being written to the servos is negative- the servos can be moved to anywhere in 0 - 180. Negative values may indeed work, but I prefer to keep them inside the stated range.
I am greatly pleased with the superb increase in accuracy with the incremental code over the absolute positioning code!
Thanks again, mirith and johnwasser. Hopefully this thread will answer others' questions on this project and topic!
#include <Servo.h>Â Â
Servo servoX;Â Â Â
Servo servoY;
int laser = 2;
int potpinX = A4;
int potpinY = A5;
const int servoPause = 10;
int servoAngleX, servoAngleY; //Thanks johnwasser
void setup()
{
 servoX.attach(9);Â
 servoY.attach(11);
 pinMode(laser, OUTPUT);
 pinMode(potpinX, INPUT);Â
 pinMode(potpinY, INPUT);
Â
 digitalWrite(laser, HIGH);
 Serial.begin(9600);
Â
 //servoX.write(90); //An attempt to move both to center on start-up,
 //servoY.write(90); //doesn't seem to work, hence ignored.
}
void loop()
{
 int XpotVal = map(analogRead(potpinX), 0, 1023, 13, -15); //13 and -15 to account for joystick center error,
 int YpotVal = map(analogRead(potpinY), 0, 1023, 13, -15); //and to slow movement of servos. Center should be near 0
Â
 servoAngleX += XpotVal; //Thanks mirith
 servoAngleY += YpotVal; //These can be negative...
Â
 servoX.write(servoAngleX); //These can't, or at least shouldn't IMO
 servoY.write(servoAngleY);
Â
 Serial.print(analogRead(potpinX)); //To monitor pot values
 Serial.print(" ");
 Serial.println(analogRead(potpinY));
Â
 delay(servoPause);
Â
}