Yes the first code runs perfect.
It has been tested, debugged and verified.
The latest code had a bug
I changed the placement of detecting the midpoint switch.
It is now within the loop where the servos are detached after 5 seconds (I reduced the wait timer).
If it works, the servo should go midpoint, if a low is held to pin 12.
Further after 5 seconds, the servos should detach and stop rattling.
It should work now, but my handicap is, that I have no working servos at the mo.
/*
Copyright2015 - SERVO SWITCH CONTROL - SSC - PaulDMV
Modified 2015 - using arrays - Anders53
Modified 09-04-2015 - code reduced heavily - by Septillion
Modified 13-04-2015 - added override to mid position - by 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)
include the VarSpeedServo.h Library before compiling !
Define arduino pin usage, which are constants :
Note that arduino analog 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.
An override toggle switch can be added to input pin 12.
A LOW (GND) level on this pin will set all servo channels to
mid position 90° for installation of the servos.
If the input is left open, the internal PULL_UP will set the
input high, which disables the mid position feature.
The internal led is set to blink every time loop() is
traversed, to indicate activity.
*/
#include <VarSpeedServo.h>
const byte NumberServos = 8; // the total number of servos
const unsigned long DetachTime = 5000; // detach servo after 10 seconds
const byte LedLightPin = 13; // internal activity LED is on pin 13
const byte TestProbePin = 12; // input pin for midpoint setting
byte i = 0; // Loop counter
boolean HoldReadVal, TestProbe = 0; // input read varaiables
boolean LedState = 0; // For toggling the LED
// arrays holding all indexable constants & variables
VarSpeedServo Myservos[NumberServos]; // Servo objects
unsigned long DetachMillis[NumberServos]; // Detach timer for each servo object
boolean SwitchState[NumberServos]; // Holds the status for each switch
// Servo nr = {0, 1, 2, 3, 4, 5, 6, 7 }
const byte ButtonPins[] = {A0, A1, A2, A3, A4, A5, 2, 3 };
const byte ServoPins[] = {4, 5, 6, 7, 8, 9, 10, 11};
const byte RangeLow[] = {85, 85, 85, 85, 85, 85, 85, 85};
const byte RangeHigh[] = {95, 95, 95, 95, 95, 95, 95, 95};
const byte TravelSpeed[] = {2, 2, 2, 2, 2, 2, 2, 2 };
void setup()
{
pinMode(LedLightPin, OUTPUT); // define internal LED output
pinMode(TestProbePin, INPUT_PULLUP); // define digital test probe input
for (i = 0; i < NumberServos; i++)
{
pinMode(ServoPins[i], OUTPUT); // Define digital pin as output
pinMode(ButtonPins[i], INPUT_PULLUP); // Define digital input pin with pull up resistor for noise reduction
SwitchState[i] = digitalRead(ButtonPins[i]); // Read the current switch state from the track layout
if(SwitchState[i]) // If switch state is high, pre-set servo control accordingly
{
Myservos[i].write(RangeHigh[i]);
}
else // Switch state is low, pre-set servo control accordingly
{
Myservos[i].write(RangeLow[i]);
}
Myservos[i].attach(ServoPins[i]); // Only after pre-setting the position control, it's safe to attach the servo
} // The servo will detach in the loop automatically after 10 seconds if no activity
}
void loop() // runs forever to detect control buttons and change switches accordingly
{
digitalWrite(LedLightPin, LedState); // Code activity LED
LedState = !LedState; // Change LED state
for (i = 0; i < NumberServos; i++)
{
TestProbe = digitalRead(TestProbePin); // Read midpoint setting switch input
HoldReadVal = digitalRead(ButtonPins[i]); // Read the button state
if ((HoldReadVal != SwitchState[i]) || !TestProbe) // Midpoint state or switch state changed by the button ?
{ // Yes, attach servo and start detach timer
DetachMillis[i] = millis(); // Initialise the detach timer
Myservos[i].attach(ServoPins[i]); // Attaches the servo on output pin to its servo object
}
if (!TestProbe) // ### Is midpoint switch active LOW ?
{ // ### Yes, midpoint active, set servos to midpoint
Myservos[i].slowmove(90, 5);
} // ### No, midpoint idle, control the servos
if ((HoldReadVal != SwitchState[i]) && TestProbe) // *** Has the switch state changed by the button ?
{
SwitchState[i] = !SwitchState[i]; // *** Yes the switch state has changed, save new switch state
if (SwitchState[i]) // Is the new switch state high (greater than 0) ?
{
Myservos[i].slowmove(RangeHigh[i], TravelSpeed[i]); // Yes, new switch state is high, move the switch accordingly
}
else
{
Myservos[i].slowmove(RangeLow[i], TravelSpeed[i]); // No, new switch state is low, move the switch accordingly
}
} // *** No, the switch state has not changed, is it time to detach servo then ?
if (Myservos[i].attached() && ((millis() - DetachMillis[i]) > DetachTime))
{
Myservos[i].detach(); // *** Yes, the time has elapsed, detach the switch servo for a more silent layout
}
}
}