Hello all,
I've built a simple robotic arm and am having trouble with the servos jerking to maintain a position if external forces move their position a negligible amount. I'm attempting to solve this by detaching a servo when it is not being changed by the potentiometer, but I need it to attach back to the pin when the pot changes. This is what I have so far:
#include <Servo.h>
Servo kservo; // create servo object to control a servo
Servo aservo;
Servo fservo;
int kpot = 0; //pot pin value
int kval; //variable to store pot position value
int newkval; //secondary variable to store pot position value
int apot = 2;
int aval;
int newaval;
int fpot = 4;
int fval;
int newfval;
int t = 10; //time interval constant
int v = 10; //pot value deviation constant
void setup()
{
kservo.attach(13); //initial servo attachment to a pin
aservo.attach(11);
fservo.attach(9);
kval = analogRead(kpot); //read initial pot value
aval = analogRead(apot);
fval = analogRead(fpot);
kval = map(kval, 0, 1023, 0, 179); //move each servo to the inita postion
kservo.write(kval);
delay(t);
aval = map(aval, 0, 1023, 0, 179);
aservo.write(aval);
delay(t);
fval = map(fval, 0, 1023, 0, 179);
fservo.write(fval);
delay(t);
}
void loop()
{
newkval = analogRead(kpot); //chech the current postition of the pot
if (kval - v < newkval < kval + v){ //check to see if the new value is within an acceptable range
kservo.detach(); //if it is within the acceptable range then detach the servo
}
else{ //or else move the servo to the new position of the pot
kservo.attach(13);
kval = analogRead(kpot);
kval = map(kval, 0, 1023, 0, 179);
kservo.write(kval);
delay(t);
}
newaval = analogRead(apot);
if(aval - v < newaval < aval + v){
aservo.detach();
}
else{
aval = analogRead(apot);
aval = map(aval, 0, 1023, 0, 179);
aservo.write(aval);
delay(t);
}
newfval = analogRead(fpot);
if (fval - v < newfval < fval + v){
fservo.detach();
}
else{
fservo.attach(9);
fval = analogRead(fpot);
fval = map(fval, 0, 1023, 0, 179);
fservo.write(fval);
delay(t);
}
}
Thank you,
Zeb