#include <Servo.h> // Use Servo library, included with IDE
Servo servoX; // Create Servo object to control the servo
Servo servoY;
int laser = 2;
int potpinX = A4;
int potpinY = A5;
const int servoPause = 1;
void setup()
{
servoX.attach(9); // Servo x is connected to digital pin 9
servoY.attach(11);
pinMode(laser, OUTPUT);
pinMode(potpinX, INPUT);
pinMode(potpinY, INPUT);
Serial.begin(9600);
digitalWrite(laser, HIGH);
}
void loop()
{
const int XCenterVal = 512, YCenterVal = 512; // Values of pot input when centered
const int XpotSpeed = 200, YpotSpeed = 20; // Scale value for controlling speed of movement (higher=slower)
int XpotVal = analogRead(potpinX) - XCenterVal;
int YpotVal = analogRead(potpinY) - YCenterVal;
int servoAngleX = constrain(servoAngleX + (XpotVal/XpotSpeed), 180, 0);
int servoAngleY = constrain(servoAngleY + (YpotVal/YpotSpeed), 180, 0);
servoX.write(servoAngleX);
servoY.write(servoAngleY);
Serial.print(XpotVal);
Serial.print(" ");
Serial.println(YpotVal);
delay(servoPause);
}
Well lets begin with the constrain- why constrain? I'm not looking to limit values, i still want to use the full range of motion of the servos as well as the pot. That is why I used the map function in my original code. Also, while running the code in my head I noticed that if the pot read less that 512 the int X/YpotVal would be a negative number, isn't this undesirable and non-functional?
And why the difference in scale values? I assumed the difference was simply a typo, but did also compile it with these values in the interest of running the code as provided.
What would have proved more helpful is a description along with the code you provided. I assume the code was to be applied as such, and it did compile correctly.