detach servo after reached position

Hi all, below i have made this code for my model railway track were i use a relais with a pull up signal sending to the arduino to switch a servo position from 85 to 95 deg to change track. However I want to detach the servo after reaching position. Can anyone help me were and how to put in the detach command without loosing the independent control of the six servo’s? ( i deleted 3 of 6 servo’s for example)

I searched the web but I didn’t find any working solution.

#include <VarSpeedServo.h>
// Copyright2015 - SERVO SWITCH CONTROL - SSC - PaulDMV
// a maximum of seven servo objects can be installed on the nano
// a maximum of eight servo objects can be created
// only change ** lines (speedx, range_x_low and range_x_high)
// install the VarSpeedServo.h Library before use!


VarSpeedServo myservo1; // create servo object to control a servo, connected to pin 7 
int pos1 = 0; // variable to store the servo position 
int button1 = 1; // The button will be on Pin 1 
int speed1= 2; // **variable to store the servo speed
int delay1= 50; // sets the delay time to reach position
int range_1_low= 85; // **sets the lower limit
int range_1_high= 95 ; // **sets the uper limit
int range_1_diff= range_1_high - range_1_low; //diffence to go to position

VarSpeedServo myservo2; // create servo object to control a servo, connected to pin 8 
int pos2 = 0; // variable to store the servo position 
int button2 = 2; // The button will be on Pin 2 
int speed2= 2; // **variable to store the servo speed
int delay2= 50; // sets the delay time to reach position
int range_2_low= 85; // **sets the lower limit
int range_2_high= 95; //**sets the uper limit
int range_2_diff= range_2_high - range_2_low; //diffence to go to position

VarSpeedServo myservo3; // create servo object to control a servo, connected to pin 9 
int pos3 = 0; // variable to store the servo position 
int button3 = 3; // The button will be on Pin 3 
int speed3= 2; // **variable to store the servo speed
int delay3= 50; // **sets the delay time to reach position
int range_3_low= 85; // **sets the lower limit
int range_3_high= 95; //**sets the uper limit
int range_3_diff= range_3_high - range_3_low; //diffence to go to position



void setup() 
{ 
myservo1.attach(7); // attaches the servo on pin 7 to the servo object
pinMode(pos1, OUTPUT);
pinMode(button1, INPUT); 
digitalWrite (button1, LOW);

myservo2.attach(8); // attaches the servo on pin 8 to the servo object
pinMode(pos2, OUTPUT);
pinMode(button2, INPUT); 
digitalWrite (button2, LOW);

myservo3.attach(9); // attaches the servo on pin 9 to the servo object
pinMode(pos3, OUTPUT);
pinMode(button3, INPUT); 
digitalWrite (button3, LOW);




} 
void loop()
{
if (digitalRead(button0) == LOW)
for(pos0 = range_0_low; pos0 < range_0_high; pos0 += range_0_diff) // goes from 85 degrees to 95 degrees in steps of 1 degree 
{
myservo0.slowmove(pos0,speed0); // tell servo to go to position in variable 'pos' 
delay(delay0); // waits x ms for the servo to reach the position
} 
if (digitalRead(button0) == HIGH) 
for(pos0 = range_0_high; pos0>=range_0_high; pos0-=range_0_diff) // goes from 95 degrees to 85 degrees in steps of 1 degree
{ 
myservo0.slowmove(pos0,speed0); // tell servo to go to position in variable 'pos' 
delay(delay0); // waits x ms for the servo to reach the position 
}
{ 
if (digitalRead(button1) == LOW)
for(pos1 = range_1_low; pos1 < range_1_high; pos1 += range_1_diff) // goes from 85 degrees to 95 degrees in steps of 1 degree 
{
myservo1.slowmove(pos1,speed1); // tell servo to go to position in variable 'pos' 
delay(delay1); // waits x ms for the servo to reach the position
} 
if (digitalRead(button1) == HIGH) 
for(pos1 = range_1_high; pos1>=range_1_high; pos1-=range_1_diff) // goes from 95 degrees to 85 degrees in steps of 1 degree
{ 
myservo1.slowmove(pos1,speed1); // tell servo to go to position in variable 'pos' 
delay(delay1); // waits x ms for the servo to reach the position 
}
{ 
if (digitalRead(button2) == LOW)
for(pos2 = range_2_low; pos2 < range_2_high; pos2 += range_2_diff) // goes from 85 degrees to 95 degrees in steps of 1 degree 
{
myservo2.slowmove(pos2,speed2); // tell servo to go to position in variable 'pos' 
delay(delay2); // waits x ms for the servo to reach the position
} 
if (digitalRead(button2) == HIGH) 
for(pos2 = range_2_high; pos2>=range_2_high; pos2-=range_2_diff) // goes from 95 degrees to 85 degrees in steps of 1 degree
{ myservo2.slowmove(pos2,speed2); // tell servo to go to position in variable 'pos' 
delay(delay2); // waits x ms for the servo to reach the position 
}
{ 
if (digitalRead(button3) == LOW)
for(pos3 = range_3_low; pos3 < range_3_high; pos3 += range_3_diff) // goes from 85 degrees to 95 degrees in steps of 1 degree 
{ 
myservo3.slowmove(pos3,speed3); // tell servo to go to position in variable 'pos' 
delay(delay3); // waits x ms for the servo to reach the position
} 
if (digitalRead(button3) == HIGH) 
for(pos3 = range_3_high; pos3>=range_3_high; pos3-=range_3_diff) // goes from 95 degrees to 85 degrees in steps of 1 degree
{ 
myservo3.slowmove(pos3,speed3); // tell servo to go to position in variable 'pos' 
delay(delay3); // waits x ms for the servo to reach the position 
}


}}} // 3x close bracket, one for each servo

}

If you feel strongly enough to bother detaching, then I guess the place to put it is after those delay(delayx); that you have where you wait for the servo to get to its destination.

But, have to ask… why?

I share @JimboZA's "why?"

Also, if you use detach() you also need to include attach() when you next want to move the signal.

...R

    for (pos0 = range_0_high; pos0 >= range_0_high; pos0 -= range_0_diff) // goes from 95 degrees to 85 degrees in steps of 1 degree
    {
      myservo0.slowmove(pos0, speed0); // tell servo to go to position in variable 'pos'
      delay(delay0); // waits x ms for the servo to reach the position
    }

Do you need the for loop and delay() when you are using the VarSpeedServo library ? It looks like the code was originally used with the normal Servo library and has been converted.

Question : where is delay0, and speed0 for that matter, defined ?

The comment is confusing too because the code is not doing what it says. That may not matter now but when you look at it in the future it could be misleading.

sorry you can remove the delay0 and other …0, I deleted 4 of the 7 servo’s because the code was to long to post here on the forum.

" Why? " Well, i have about 70 servo’s running at the same time, all day long and some are making a bit of noise because there is a tiny bit of tension on the arm, when the servo is not perfectly trimmed in the range needed to shift slips.

so it would be great to detach after reaching position.

originally the code was indeed made for the original library, if i can remove code than it would be great.

#include <VarSpeedServo.h>
// Copyright2015 - SERVO SWITCH CONTROL - SSC - PaulDMV
// a maximum of seven servo objects can be installed on the nano
// a maximum of eight servo objects can be created
// only change ** lines (speedx, range_x_low and range_x_high)
// install the VarSpeedServo.h Library before use!


VarSpeedServo myservo1; // create servo object to control a servo, connected to pin 7 
int pos1 = 0; // variable to store the servo position 
int button1 = 1; // The button will be on Pin 1 
int speed1= 2; // **variable to store the servo speed
int delay1= 50; // sets the delay time to reach position
int range_1_low= 85; // **sets the lower limit
int range_1_high= 95 ; // **sets the uper limit
int range_1_diff= range_1_high - range_1_low; //diffence to go to position

VarSpeedServo myservo2; // create servo object to control a servo, connected to pin 8 
int pos2 = 0; // variable to store the servo position 
int button2 = 2; // The button will be on Pin 2 
int speed2= 2; // **variable to store the servo speed
int delay2= 50; // sets the delay time to reach position
int range_2_low= 85; // **sets the lower limit
int range_2_high= 95; //**sets the uper limit
int range_2_diff= range_2_high - range_2_low; //diffence to go to position

VarSpeedServo myservo3; // create servo object to control a servo, connected to pin 9 
int pos3 = 0; // variable to store the servo position 
int button3 = 3; // The button will be on Pin 3 
int speed3= 2; // **variable to store the servo speed
int delay3= 50; // **sets the delay time to reach position
int range_3_low= 85; // **sets the lower limit
int range_3_high= 95; //**sets the uper limit
int range_3_diff= range_3_high - range_3_low; //diffence to go to position



void setup() 
{ 
myservo1.attach(7); // attaches the servo on pin 7 to the servo object
pinMode(pos1, OUTPUT);
pinMode(button1, INPUT); 
digitalWrite (button1, LOW);

myservo2.attach(8); // attaches the servo on pin 8 to the servo object
pinMode(pos2, OUTPUT);
pinMode(button2, INPUT); 
digitalWrite (button2, LOW);

myservo3.attach(9); // attaches the servo on pin 9 to the servo object
pinMode(pos3, OUTPUT);
pinMode(button3, INPUT); 
digitalWrite (button3, LOW);




} 
void loop()

{ 
if (digitalRead(button1) == LOW)
for(pos1 = range_1_low; pos1 < range_1_high; pos1 += range_1_diff) // goes from 85 degrees to 95 degrees in steps of 1 degree 
{
myservo1.slowmove(pos1,speed1); // tell servo to go to position in variable 'pos' 
delay(delay1); // waits x ms for the servo to reach the position
} 
if (digitalRead(button1) == HIGH) 
for(pos1 = range_1_high; pos1>=range_1_high; pos1-=range_1_diff) // goes from 95 degrees to 85 degrees in steps of 1 degree
{ 
myservo1.slowmove(pos1,speed1); // tell servo to go to position in variable 'pos' 
delay(delay1); // waits x ms for the servo to reach the position 
}
{ 
if (digitalRead(button2) == LOW)
for(pos2 = range_2_low; pos2 < range_2_high; pos2 += range_2_diff) // goes from 85 degrees to 95 degrees in steps of 1 degree 
{
myservo2.slowmove(pos2,speed2); // tell servo to go to position in variable 'pos' 
delay(delay2); // waits x ms for the servo to reach the position
} 
if (digitalRead(button2) == HIGH) 
for(pos2 = range_2_high; pos2>=range_2_high; pos2-=range_2_diff) // goes from 95 degrees to 85 degrees in steps of 1 degree
{ myservo2.slowmove(pos2,speed2); // tell servo to go to position in variable 'pos' 
delay(delay2); // waits x ms for the servo to reach the position 
}
{ 
if (digitalRead(button3) == LOW)
for(pos3 = range_3_low; pos3 < range_3_high; pos3 += range_3_diff) // goes from 85 degrees to 95 degrees in steps of 1 degree 
{ 
myservo3.slowmove(pos3,speed3); // tell servo to go to position in variable 'pos' 
delay(delay3); // waits x ms for the servo to reach the position
} 
if (digitalRead(button3) == HIGH) 
for(pos3 = range_3_high; pos3>=range_3_high; pos3-=range_3_diff) // goes from 95 degrees to 85 degrees in steps of 1 degree
{ 
myservo3.slowmove(pos3,speed3); // tell servo to go to position in variable 'pos' 
delay(delay3); // waits x ms for the servo to reach the position 
}


}}} // 3x close bracket, one for each servo

Paul_DMV: because there is a tiny bit of tension on the arm,

In that case the arm will move out of position as soon as you detach()

...R

no not really, because overshoot is less then 1 deg, only tension will go off but it will stay in position when i turn of the power.

Should I put the attach in the loop and detach after each slowmove command? like below? (havent tried yet)

{ 
if (digitalRead(button1) == LOW)
for(pos1 = range_1_low; pos1 < range_1_high; pos1 += range_1_diff) // goes from 85 degrees to 95 degrees in steps of 1 degree 
{
myservo1.attach(7); // attaches the servo on pin 7 to the servo object  
myservo1.slowmove(pos1,speed1); // tell servo to go to position in variable 'pos' 
delay(delay1); // waits x ms for the servo to reach the position
myservo1.detach(); // attaches the servo on pin 7 to the servo object

if (digitalRead(button1) == HIGH) 
for(pos1 = range_1_high; pos1>=range_1_high; pos1-=range_1_diff) // goes from 95 degrees to 85 degrees in steps of 1 degree
{ 
myservo1.attach(7); // attaches the servo on pin 7 to the servo object
myservo1.slowmove(pos1,speed1); // tell servo to go to position in variable 'pos' 
delay(delay1); // waits x ms for the servo to reach the position 
myservo1.detach(); // attaches the servo on pin 7 to the servo object
}

Hi Paul -

Consider using arrays for all your variables. It will make it a whole lot easier to call out each variable, as then you simply put the array in a for loop, and use the loop counter to call out each value in the array.

Next step would be to put your repeated code in functions (subroutines), then call the(se) subroutine(s) from within that same for loop. The for loop is of course placed within you loop(), but the functions are placed outside the loop() at the bottom of your code.

This way you save on code usage and makes your code a lot easier to read and overlook.

Now for your original question : It will become quite easy to detach - and attach - the servos, once you have converted your code into function calls, as then you only need to have these commands once in your code.

I may help you with the initial reworking of your code, if you want to.

for (pos1 = range_1_high; pos1 >= range_1_high; pos1 -= range_1_diff) // goes from 95 degrees to 85 degrees in steps of 1 degree
{
  myservo1.attach(7); // attaches the servo on pin 7 to the servo object
  myservo1.slowmove(pos1, speed1); // tell servo to go to position in variable 'pos'
  delay(delay1); // waits x ms for the servo to reach the position
  myservo1.detach(); // attaches the servo on pin 7 to the servo object
}

I see that you still have the for loop, the delay() and the misleading comments. If you are going to attach and detach the servo then attach it before it moves and detach it it after it has finished moving not each time it moves.

myservo1.attach(7); // attaches the servo on pin 7 to the servo object
myservo1.slowmove(range_1_high, speed1); //move the servo slowly to the target position
myservo1.detach(); // attaches the servo on pin 7 to the servo object

may work but may also detach the sevo too soon. Try it and see.

Does the VarSpeedServo library support the equivalent of writeMicroseconds() from the standard library ? It may allow you to position the servos more accurately and avoid detaching them

Anders53: I may help you with the initial reworking of your code, if you want to.

hi Anders, every help would be highly appriciated, I had some help before with a similair solution but it didn't work, it worked with one servo, but not with multiple servo's that should be able to switch independent, with each having its own pull up input signal. I'm not really a programmer but a mechanical engineer, so i understand a lot but not really experienced with programming.

Here is my suggestion to make a simpler code.
Try first if it will compile on your Arduino environment.
Then download and test it out.

Please note the various changes I have introduced in my comments.

/*
 Copyright2015 - SERVO SWITCH CONTROL - SSC - PaulDMV
 Modified 2015 - using arrays - Anders53
 A maximum of 11 servo objects can be installed on the nano
 A maximum of eight servo objects can be created from library
 Only change constants (speed, range_low and range_high)
 install the VarSpeedServo.h Library before use!
*/

#include <VarSpeedServo.h>

// create servo objects using the included library
VarSpeedServo myservo1;         // create servo object to control a servo, connected to pin 4
VarSpeedServo myservo2;         // create servo object to control a servo, connected to pin 5
VarSpeedServo myservo3;         // create servo object to control a servo, connected to pin 6
VarSpeedServo myservo4;         // create servo object to control a servo, connected to pin 7
VarSpeedServo myservo5;         // create servo object to control a servo, connected to pin 8
VarSpeedServo myservo6;         // create servo object to control a servo, connected to pin 9
VarSpeedServo myservo7;         // create servo object to control a servo, connected to pin 10
VarSpeedServo myservo8;         // create servo object to control a servo, connected to pin 11

/* 
 Define Arduino pins, which are constants
 Note that Arduino analogue pins can be defined as digtial pins
 making for the maximum of 8 servo objects in the library
 Arduino pins 0 & 1 are here reserved for any serial communication
 i.e. for debugging or future inter comm between arduino units
 The pin numbers are those found printed on the Arduino board itself
*/

const int button1Pin = A0;      // A button will be on Pin A0
const int button2Pin = A1;      // A button will be on Pin A1
const int button3Pin = A2;      // A button will be on Pin A2
const int button4Pin = A3;      // A button will be on Pin A3
const int button5Pin = A4;      // A button will be on Pin A4
const int button6Pin = A5;      // A button will be on Pin A5
const int button7Pin = 2;       // A button will be on Pin 2
const int button8Pin = 3;       // A button will be on Pin 3
const int servo1Pin = 4;        // A servo will be on Pin 4
const int servo2Pin = 5;        // A servo will be on Pin 5
const int servo3Pin = 6;        // A servo will be on Pin 6
const int servo4Pin = 7;        // A servo will be on Pin 7
const int servo5Pin = 8;        // A servo will be on Pin 8
const int servo6Pin = 9;        // A servo will be on Pin 9
const int servo7Pin = 10;       // A servo will be on Pin 10
const int servo8Pin = 11;       // A servo will be on Pin 11

// define constant values, only changed at compile time
const int delayServo = 50;      // sets the delay time to reach servo position
const int range_low = 85;       // **sets the lower limit of servo travel
const int range_high = 95;      // **sets the upper limit of servo travel
const int travelSpeed = 2;      // **sets the servo working speed
const int numberServos = 8;     // the total number of servos

// variables used for controlling
int range_diff = range_high - range_low; // servo travel distance
int pos1 = 0;                   // variable to store servo 1 position 
int pos2 = 0;                   // variable to store servo 2 position
int pos3 = 0;                   // variable to store servo 3 position
int pos4 = 0;                   // variable to store servo 4 position
int pos5 = 0;                   // variable to store servo 5 position
int pos6 = 0;                   // variable to store servo 6 position
int pos7 = 0;                   // variable to store servo 7 position
int pos8 = 0;                   // variable to store servo 8 position
int i = 0;                      // loop counter

// arrays holding all indexable constants & variables
VarSpeedServo myservos[] = {myservo1, myservo2, myservo3, myservo4, myservo5, myservo6, myservo7, myservo8};
int buttonPins[] = {button1Pin, button2Pin, button3Pin, button4Pin, button5Pin, button6Pin, button7Pin, button8Pin};
int servoPins[] = {servo1Pin, servo2Pin, servo3Pin, servo4Pin, servo5Pin, servo6Pin, servo7Pin, servo8Pin};
int servoPos[] = {pos1, pos2, pos3, pos4, pos5, pos6, pos7, pos8};

void setup() 
{ 
  for (i = 0; i < numberServos; i++)
  {
    pinMode(servoPins[i], OUTPUT);
    pinMode(buttonPins[i], INPUT); 
    digitalWrite(buttonPins[i], LOW);
  }
}

void loop() // runs forever to detect control buttons and change switches accordingly
{ 
  for(i = 0; i < numberServos; i++)
  {
    // ******* check state of button and make neceassary switch position changes **********
    if (digitalRead(buttonPins[i]) == LOW)
    {
      for(servoPos[i] = range_low; servoPos[i] < range_high; servoPos[i] += range_diff) // goes from 85 degrees to 95 degrees in steps of 1 degree 
      {
        myservos[i].attach(servoPins[i]); // attaches the servos on output pins to their servo objects
        myservos[i].slowmove(servoPos[i],travelSpeed); // tell servo to go to position in variable 'pos' 
        delay(delayServo); // waits x ms for the servo to reach the position
        myservos[i].detach(); // detaches the servos on output pins to their servo objects
      } // end for
    } // end if
    if (digitalRead(buttonPins[i]) == HIGH) 
    {
      for(servoPos[i] = range_high; servoPos[i] >= range_low; servoPos[i] -= range_diff) // goes from 95 degrees to 85 degrees in steps of 1 degree
      { 
        myservos[i].attach(servoPins[i]); // attaches the servos on output pins to their servo objects
        myservos[i].slowmove(servoPos[i],travelSpeed); // tell servo to go to position in variable 'pos' 
        delay(delayServo); // waits x ms for the servo to reach the position
        myservos[i].detach(); // detaches the servos on output pins to their servo objects
      }
    }
  }
}

Thanks, Anders, I will try it once i have my arduino with me, have to wait two more days. LAT sucks :P

only thing is I still need the option to set each high and low value individually for each servo, because the mounting will never be exactly the same and some times I only need 8 or 6 degrees depending on the type or track.

Can that still be implemented to your way of programming?

No problem. We simply introduce a couple of more arrays, where we hold the constants needed to be defined for eavh switch application.

Hold on a few minutes :)

I don't have any programming advice. But I've installed a lot of model railway switch motors. It sounds like you have too much tension, not enough spring between the motor and the switch. The motors I'm using have an adjustable spring wire for that purpose. I'm sure the servos would stop cogging if they only had to hold the spring in position. They're designed to hold a certain position, not a certain torque.

Here is the revised code.

aarg has a point on the mechanical link from servo to the switch tongue.
The link should be a spring, which just exactly locks the switch into position, when the servo stops travel.
Thats also how ready made switches are made (at least the former Märklin switches I used to play with many years ago)

Still - detaching the servo should make for less mecahical noise, as the servos themselves may be prone to hunt a bit.
This will also save power, as any tension or hunting is released from the servo.

/*
 Copyright2015 - SERVO SWITCH CONTROL - SSC - PaulDMV
 Modified 2015 - using arrays - Anders53
 A maximum of 11 servo objects can be installed on the nano
 A maximum of eight servo objects can be created from library
 Only change constants (speed, range_low and range_high)
 install the VarSpeedServo.h Library before use!
*/

#include <VarSpeedServo.h>

// create servo objects using the included library
VarSpeedServo myservo1;         // create servo object to control a servo, connected to pin 4
VarSpeedServo myservo2;         // create servo object to control a servo, connected to pin 5
VarSpeedServo myservo3;         // create servo object to control a servo, connected to pin 6
VarSpeedServo myservo4;         // create servo object to control a servo, connected to pin 7
VarSpeedServo myservo5;         // create servo object to control a servo, connected to pin 8
VarSpeedServo myservo6;         // create servo object to control a servo, connected to pin 9
VarSpeedServo myservo7;         // create servo object to control a servo, connected to pin 10
VarSpeedServo myservo8;         // create servo object to control a servo, connected to pin 11

/* 
 Define arduino pin usage, which are constants
 Note that arduino anlogue pins can be defined as digtial pins
 making for the maximum of 8 servo objects in the library
 Arduino pins 0 & 1 are here reserved for any serial communication
 i.e. for debugging or future inter comm between arduino units
 The pin numbers are those found printed on the arduino board
 They should apply to all Arduino Uno, Nano & Micro varaints
 but can of course be re-arranged to suit the extra A6 & A7 pins
 found on later revisions of Uno & Nano derivatives.
*/

// the total number of servos
const int numberServos = 8;     

// Button input pins
const int button1Pin = A0;
const int button2Pin = A1;
const int button3Pin = A2;
const int button4Pin = A3;
const int button5Pin = A4;
const int button6Pin = A5;
const int button7Pin = 2; 
const int button8Pin = 3; 

// Servo output pins
const int servo1Pin = 4; 
const int servo2Pin = 5; 
const int servo3Pin = 6; 
const int servo4Pin = 7; 
const int servo5Pin = 8; 
const int servo6Pin = 9; 
const int servo7Pin = 10;
const int servo8Pin = 11;

/*
  Define constant values only changed at compile time
  These values can be changed to trim the behaviour of each switch
*/

// sets the delay time to reach servo position
const int delay_1_Servo = 50;
const int delay_2_Servo = 50;
const int delay_3_Servo = 50;
const int delay_4_Servo = 50;
const int delay_5_Servo = 50;
const int delay_6_Servo = 50;
const int delay_7_Servo = 50;
const int delay_8_Servo = 50;

// **sets the lower limit of servo travel
const int range_1_low = 85;
const int range_2_low = 85;
const int range_3_low = 85;
const int range_4_low = 85;
const int range_5_low = 85;
const int range_6_low = 85;
const int range_7_low = 85;
const int range_8_low = 85;

// **sets the upper limit of servo travel
const int range_1_high = 95;
const int range_2_high = 95;
const int range_3_high = 95;
const int range_4_high = 95;
const int range_5_high = 95;
const int range_6_high = 95;
const int range_7_high = 95;
const int range_8_high = 95;

// **sets the servo traversing speed
const int travel_1_Speed = 2;
const int travel_2_Speed = 2;
const int travel_3_Speed = 2;
const int travel_4_Speed = 2;
const int travel_5_Speed = 2;
const int travel_6_Speed = 2;
const int travel_7_Speed = 2;
const int travel_8_Speed = 2;

// working switch variables
int range_1_diff = range_1_high - range_1_low; 
int range_2_diff = range_2_high - range_2_low; 
int range_3_diff = range_3_high - range_3_low; 
int range_4_diff = range_4_high - range_4_low; 
int range_5_diff = range_5_high - range_5_low; 
int range_6_diff = range_6_high - range_6_low; 
int range_7_diff = range_7_high - range_7_low; 
int range_8_diff = range_8_high - range_8_low; 

int pos1 = 0;                   // variable to store servo 1 position 
int pos2 = 0;                   // variable to store servo 2 position
int pos3 = 0;                   // variable to store servo 3 position
int pos4 = 0;                   // variable to store servo 4 position
int pos5 = 0;                   // variable to store servo 5 position
int pos6 = 0;                   // variable to store servo 6 position
int pos7 = 0;                   // variable to store servo 7 position
int pos8 = 0;                   // variable to store servo 8 position

// other control variables
int i = 0;                      // loop counter

// arrays holding all indexable constants & variables
VarSpeedServo myservos[] = {myservo1, myservo2, myservo3, myservo4, myservo5, myservo6, myservo7, myservo8};
int buttonPins[] = {button1Pin, button2Pin, button3Pin, button4Pin, button5Pin, button6Pin, button7Pin, button8Pin};
int servoPins[] = {servo1Pin, servo2Pin, servo3Pin, servo4Pin, servo5Pin, servo6Pin, servo7Pin, servo8Pin};
int servoPos[] = {pos1, pos2, pos3, pos4, pos5, pos6, pos7, pos8};
int delayServos[] = {delay_1_Servo, delay_2_Servo, delay_3_Servo, delay_4_Servo, delay_5_Servo, delay_6_Servo, delay_7_Servo, delay_8_Servo};
int rangeLow[] = {range_1_low, range_2_low, range_3_low, range_4_low, range_5_low, range_6_low, range_7_low, range_8_low};
int rangeHigh[] = {range_1_high, range_2_high, range_3_high, range_4_high, range_5_high, range_6_high, range_7_high, range_8_high};
int travelSpeed[] = {travel_1_Speed, travel_2_Speed, travel_3_Speed, travel_4_Speed, travel_5_Speed, travel_6_Speed, travel_7_Speed, travel_8_Speed};
int rangeDiff[] = {range_1_diff, range_2_diff, range_3_diff, range_4_diff, range_5_diff, range_6_diff, range_7_diff, range_8_diff};

void setup() 
{ 
  for (i = 0; i < numberServos; i++)
  {
    pinMode(servoPins[i], OUTPUT);
    pinMode(buttonPins[i], INPUT); 
    digitalWrite(buttonPins[i], LOW);
  }
}

void loop() // runs forever to detect control buttons and change switches accordingly
{ 
  for(i = 0; i < numberServos; i++)
  {
    // ******* check state of button and make neceassary switch position changes **********
    if (digitalRead(buttonPins[i]) == LOW)
    {
      for(servoPos[i] = rangeLow[i]; servoPos[i] >= rangeHigh[i]; servoPos[i] += rangeDiff[i]) // goes from 85 degrees to 95 degrees in steps of 1 degree 
      {
        myservos[i].attach(servoPins[i]); // attaches the servos on output pins to their servo objects
        myservos[i].slowmove(servoPos[i],travelSpeed[i]); // tell servo to go to position in variable 'pos' 
        delay(delayServos[i]); // waits x ms for the servo to reach the position
        myservos[i].detach(); // detaches the servos on output pins to their servo objects
      } // end for
    } // end if
    if (digitalRead(buttonPins[i]) == HIGH) 
    {
      for(servoPos[i] = rangeHigh[i]; servoPos[i] <= rangeLow[i]; servoPos[i] -= rangeDiff[i]) // goes from 95 degrees to 85 degrees in steps of 1 degree
      { 
        myservos[i].attach(servoPins[i]); // attaches the servos on output pins to their servo objects
        myservos[i].slowmove(servoPos[i],travelSpeed[i]); // tell servo to go to position in variable 'pos' 
        delay(delayServos[i]); // waits x ms for the servo to reach the position
        myservos[i].detach(); // detaches the servos on output pins to their servo objects
      }
    }
  }
}

I’m interested in doing this myself, because the servos are smaller and cheaper than the motors I used before. But I would never allow a servo to drive the points directly, because the switch parts are delicate (at least, on the ones I like to use) and a switch is a terrible thing to have to replace once it’s glued down in ballast.

Use some kind of tension relaxing link like a tube-in-tube link found on R/C planes rudder controls, together with the spring. That also makes for easy made long servo links, placing the servos closer to the edge of your layout.

Nylon tubing has low friction, but still takes up some torque from the servo.

BTW : next update on the code, would be to read the buttons, and only traverse the servosettings if button state has changed.

@ aarg : would that be a contribution from you ? :wink:
Then we truly have a cooperative servo application, aimed at our modeltrain enthusiasts.

Anders53: Use some kind of tension relaxing link like a tube-in-tube link found on R/C planes rudder controls, together with the spring. That also makes for easy made long servo links, placing the servos closer to the edge of your layout.

Nylon tubing has low friction, but still takes up some torque from the servo.

That's another thing I like about the servo idea, that the servo location is flexible. Most (good) layouts are a maze of carpentry underneath the tracks. Also the parts are easily available and relatively standardized.

Anders53: BTW : next update on the code, would be to read the buttons, and only traverse the servosettings if button state has changed.

@ aarg : would that be a contribution from you ? ;) Then we truly have a cooperative servo application, aimed at our modeltrain enthusiasts.

I would certainly make it public domain. The time consuming part is to make it work with DCC, digital control. Actually, I have seen this from a company called ANE, out of Taiwan. But I'd rather do it myself. I'm hoping to work on it this coming summer.