Could someone pleases tell me how to slow the servos down in the attached sketch,
Thanks,
Wally.
#include <Servo.h>
Servo pointServo[6];
byte servoPin[] = { 2, 3, 4, 5, 6, 7, }; // pin numbers for servo signals
byte switchPin[] = { 8, 9, 10, 11, 12, 13 }; // pin numbers for switch connections
// Uno analog pins A5 A4 A3 A2 A1 A0
byte servoLowPos[] = { 50, 50, 50, 50, 50, 50, }; // degrees for low servo position
byte servoHighPos[] = {150, 150, 150, 150, 150, 150,}; // degrees for high servo position
byte servoPos[6];
void setup() {
setupServos();
setupSwitches();
}
void loop() {
for (byte n = 0; n < 6; n++) {
boolean servoMove = false;
byte sw = digitalRead(switchPin[n]);
if (sw == HIGH) {
if (servoPos[n] != servoHighPos[n]) { // check if the position has changed
servoMove = true;
servoPos[n] = servoHighPos[n];
}
}
else { // if sw == LOW
if (servoPos[n] != servoLowPos[n]) { // check if the position has changed
servoMove = true;
servoPos[n] = servoLowPos[n];
}
}
if (servoMove) { // only move the servo if the switch has changed
pointServo[n].write(servoPos[n]);
}
}
}
void setupServos() {
for (byte n = 0; n < 6; n++) {
pointServo[n].attach(servoPin[n]);
servoPos[n] = servoLowPos[n]; // this is just a starting value and may be over-ridden immediately by a switch
}
}
void setupSwitches() {
for (byte n = 0; n < 6; n++) {
pinMode(switchPin[n], INPUT_PULLUP);
}
}
The classic way to do what you want would be to use a for loop to move the servo in steps with a short delay between the moves
I use the word delay with caution because it has implications depending on your requirements. For instance, if a turnout was to take 1 second to move from one extreme to the other would it be acceptable that nothing else could happen in the sketch during that 1 second ?
If so then use the delay() function. However, if the sketch must remain responsive during the 1 second then a different, non blocking, timing method is required
To slow the movement down, you need to move the servo positions by small steps, and do that in a timed way. Right now they move from 50 to 150 in a single step, so the servo moves at maximum speed. You need to move from 50 to 51 or 55, then 52 or 60 etc, and wait a short time between each step. For the waiting, ideally you should use millis() but you could try using a short delay() like delay(100). This is unlikely to miss any button presses.
the key difference is that instead of immediately moving the servo to the specified end position, the servo position is inc/decremented to that position until it is reached