I'm using servos to throw my turnouts on my model railroad. I adjust the "servoLowPos" and "servoHighPos" to get the throw I need, but some of my servos twitch or hum. I trying to figure out how to "detach" them after being thrown. I had a lot of help to get this sketch so the servos run slow and now need help to get detach to work.
I modified this sketch to throw 22 servos on a mega.
Here is the sketch
#include <Servo.h>
#define SERVOS 8
Servo pointServo[ SERVOS ];
// 4 moves per second:
#define TIME2UPDATE 22
#define BYSOME 1
byte servoPin[] = { 1, 2, 3, 4, 5, 6, 7, 8}; // pin numbers for servo signals
byte switchPin[] = { 19, 18, 17, 16, 15, 14, 12, 11}; // pin numbers for switch connections
// Uno analog pins A5 A4 A3 A2 A1 A0
byte servoLowPos[] = {120, 120, 120, 120, 120, 60, 60, 60, 60}; // degrees for low servo position
byte servoHighPos[] = { 60, 60, 60, 60, 60, 120, 120, 120, 120}; // degrees for high servo position
byte servoPos[ SERVOS ];
byte servoCommanded[ SERVOS ];
unsigned int before, now;
void setup( ) {
setupServosAndSwitches( );
before = now = millis( );
} // setup( )
void loop( ) {
// this gets the time since the uc started running
now = millis( );
// this checks if TIME2UPDATE time has been expired,
// so we only update servos to get towards their final destination every TIME2UPDATE milliseconds
if ( now - before > TIME2UPDATE ) {
before = now; // set before to now, so we can check for another expiration next time
for ( byte n = 0; n < SERVOS; n++ ) {
updateServo( n, BYSOME );
} // for
} // if time to update
for ( byte n = 0; n < SERVOS; n++ ) {
byte sw = digitalRead( switchPin[n] );
if ( sw == HIGH ) {
servoCommanded[n] = servoHighPos[n]; // not moving the servo, only setting where it should travel towards
} else { // if sw == LOW
servoCommanded[n] = servoLowPos[n]; // not moving the servo, only setting where it should travel towards
} // if
} // for
} // loop( )
void setupServosAndSwitches( ) {
for ( byte n = 0; n < SERVOS; n++ ) {
pointServo[n].attach( servoPin[n] ); // say which pin this servo is running on
servoPos[n] = servoLowPos[n]; // this is just a starting value and may be over-ridden immediately by a switch
pinMode( switchPin[n], INPUT_PULLUP ); // make this pin an input with a pull up resistor
} // for
} // setupServosAndSwitches( )
void updateServo( byte servo, byte byHowMuch ) {
if ( servoCommanded[ servo ] > servoPos[ servo ] ) { // if position smaller, increase
servoPos[ servo ] = servoPos[ servo ] + byHowMuch;
if ( servoPos[ servo ] > servoCommanded[ servo ] ) { // don't overshoot!
servoPos[ servo ] = servoCommanded[ servo ];
} // if
pointServo[ servo ].write( servoPos[ servo ] );
} else {
if ( servoCommanded[ servo ] < servoPos[ servo ] ) { // if position bigger, reduce
servoPos[ servo ] = servoPos[ servo ] - byHowMuch;
if ( servoPos[ servo ] < servoCommanded[ servo ] ) { // don't undershoot!
servoPos[ servo ] = servoCommanded[ servo ] ;
} // if
pointServo[ servo ].write( servoPos[ servo ] ); // only place we write to a servo
} else {
// do nothing, since you are there!
} // if
} // if
} // updateServo( int )
// the end