detach() and attach() in a loop

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

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.

Detaching the servo will stop it's attempt to hold a position. I can't see how that will prove useful.

This is what I have so far:

Does it work? If not, what does it do? How does that differ from what you want?

The arm has the mechanical ability to hold its position so it does not need the servo to hold the position. The trouble I'm having is that the servos don't do anything when I run the program and move the pots, the arm just sits there with my current code.

  if (kval - v < newkval < kval + v){    //check to see if the new value is within an acceptable range

Does this actually do what the comment says ?

UKHeliBob,

This line of code may not work, I guess I should try and set up some feedback to check my values. Do you have any suggestions on how I can receive this feedback?

Zeb

Serial.begin(), Serial.print(), and Serial.println() and open the Serial Monitor.

And, no that code does not do what the comment says. Algebra class is over. You are now dealing with stupid computers that can only determine is one value is less than, equal, or greater than another. If you want to determine if newkval (poor name) is less than a value and greater than another value, you need two distinct comparisons.

PaulS,

Thanks for the info, I'll give it a try. Why do you think my variable name choice is poor?

Zeb

Why do you think my variable name choice is poor?

digitalRead()
analogRead()
digitalWrite()

Function names and variable names are supposed to use camel case. Individual words are capitalized, except the first word, in function names.