Model Railroad: control switch with servo and relay for frog polarisation

Hi all,

I am new here. Some time ago a downloaded a sketch foor controlling 8 turnouts for a modelrailroad. It works great. Only now I would like to control the polarisation of the frog of the turnout.

For non modelrailroaders: the frog is where the straith and siding rail meet.

I thought to modify the existing sketch but i get an error:

exit status 1
request for member ‘digitalWrite’ in ‘MyRelays[((int)i)]’, which is of non-class type ‘boolean {aka bool}’

I asssume i do something wrong in decalring the array for the relays. I do have tried various things, searched the net and the forum, but I do not have clue what I do wrong.

Many thanks, for advice

Where I got the original sketch from:

The code:

/*
 Copyright2015 - SERVO SWITCH CONTROL - SSC - PaulDMV
 Modified 2015 - using arrays - Anders53
 Modified 9-4-2015 - code reduced heavily - by Septillion

 Attempt to modify 27-02-2018: by yoeroen: goal is to switch a relay halfway in order to control polarisation of the frog of the turnout

 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.
 
 A digital testprobe is found on input pin 12.
 A high on this input will light up internal LED.
 Can only be used internally to the Arduinos 5V supply area.
 
*/

#include <VarSpeedServo.h>
const byte NumberServos        =     5;        // the total number of servos
const byte NumberRelays        =     5;       // the total number of relays, added JH
const unsigned long DetachTime =     8000;    // detach servo after 8 seconds
const byte LedLightPin         =     13;       // internal test LED is on pin 13
const byte TestProbePin        =     12;       // input pin as test probe for internal LED
byte i                         =     0;        // Loop counter
boolean HoldReadVal, TestProbe =     0;        // input read varaiables

// 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
boolean               MyRelays[NumberRelays];      // Relay objects, added JH

// Servo nr                    =    {0,  1,  2,  3,  4  }
const byte ButtonPins[]        =    {A0, A1, A2, A3, A4 };
const byte ServoPins[]         =    {2,  3,  4,  5,  6  };
const byte RelayPins[]         =    {7,  8,  9, 10, 11  }; // added JH
const byte RangeLow[]          =    {65, 45, 69, 65, 65 };
const byte RangeMid[]          =    {90, 90, 90, 90, 90 };
const byte RangeHigh[]         =    {115,115,130,120,115};
const byte TravelSpeed[]       =    {22, 22, 22, 22, 22 };

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 (RelayPins [i], OUTPUT);                // Define pin as output, added JH
    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 then
    {
      Myservos[i].write(RangeMid[i]);                 // , pre-set servo control accordingly halfway
      MyRelays[i].digitalWrite(RelayPins[i], HIGH);        // , and pre-set the relay accordingly, added JH
      Myservos[i].write(RangeHigh[i]);                // , pre-set servo control accordingly to end position
    }
    else                                              // Switch state is low
    {
      Myservos[i].write(RangeMid[i]);                 //, pre-set servo control accordingly halfway
      MyRelays[i].digitalWrite((RelayPins[i], LOW)         //, and pre-set the relay accordingly, added JH
      Myservos[i].write(RangeLow[i]);                 //, pre-set servo control accordingly to end position
    }
    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
{
  TestProbe = digitalRead(TestProbePin);                     // read digital testprobe input
  digitalWrite(LedLightPin, TestProbe);                      // set internal LED to testprobe input
  for (i = 0; i < NumberServos; i++)
  {
    HoldReadVal = digitalRead(ButtonPins[i]);                // Read the button state
    if (HoldReadVal != SwitchState[i])                       // *** Has the switch state changed by the button ?
    {
      SwitchState[i] = !SwitchState[i];                      // *** Yes the switch state has changed, save new switch state
      DetachMillis[i] = millis();                            // Initialise the detach timer
      Myservos[i].attach(ServoPins[i]);                      // Attaches the servo on output pin to its servo object
      if (SwitchState[i])                                    // Is the new switch state high (greater than 0) ?
      {
        Myservos[i].slowmove(RangeMid[i], TravelSpeed[i]);   // Yes, new switch state is high, move the switch accordingly halfway
        MyRelays[i].digitalWrite((RelayPins[i], HIGH);            // Yes, new switch state is high, control the relay accordingly
        Myservos[i].slowmove(RangeHigh[i], TravelSpeed[i]);  // Yes, new switch state is high, move the switch accordingly to end position
      }
      else
      {
        Myservos[i].slowmove(RangeMid[i], TravelSpeed[i]);   // No, new switch state is low, move the switch accordingly halfway
        MyRelays[i].digitalWrite((RelayPins[i], LOW);             // No, new switch state is low, control the relay accordingly
        Myservos[i].slowmove(RangeLow[i], TravelSpeed[i]);   // No, new switch state is low, move the switch accordingly to end position
      }
    }
    else if (Myservos[i].attached() && ((millis() - DetachMillis[i]) > DetachTime)) // *** No, the switch state has not changed, is it time to detach servo then ?
    {
      Myservos[i].detach();                                  // *** Yes, the time has elapsed, detach the switch servo for a more silent layout
    }
  }
}

Kind regards, Yoeroen

Compare the incorrect line 66 with the correct style in line 82.

I think line 66 (and similar lines) should be

digitalWrite(RelayPins[i], HIGH);

I don't know what is the purpose of the array MyRelays[] unless it is to keep track of the state of the relays. But it is not used like that anywhere.

...R

Robin,

Thank you. I modified like you suggested. The error has gone.

Being a (very) novice programmer, I assumed I needed to declare an array of relays similar to the servos.

I will put this on a test; this might take some days since my desk is a mess.

Yoeroen.

yoeroen: Being a (very) novice programmer, I assumed I needed to declare an array of relays similar to the servos.

Strictly speaking you are adding an array of instances of Servo objects derived from the Servo library.

There is nothing equivalent for relays as they are simple ON/OFF devices.

...R

I don't see anywhere in your code where you properly initialize the Myservos array.

PaulS:
I don’t see anywhere in your code where you properly initialize the Myservos array.

Well I not sure (since I copied the sketch) but I assume it is:

#include <VarSpeedServo.h>

const byte NumberServos     =     5;        // the total number of servos

VarSpeedServo     Myservos[NumberServos];      // Servo objects

const byte ServoPins[]         =    {2,  3,  4,  5,  6  };

Well I not sure (since I copied the sketch) but I assume it is:

There you declare an array that can hold 5 elements of type VarSpeedServo.

That's like having an empty shoe box that can hold 5 pairs of Nike's top of the line sneakers. Without anything IN the box, it's useless.

@PaulS, you are, once again, being cryptic.

Perhaps what you are looking for is on Line 75 in the code in the Original Post.

...R

PaulS: There you declare an array that can hold 5 elements of type VarSpeedServo.

And it will contain 5 VarSpeedServo objects created using the default constructor.

Isn't that how object arrays work?

Steve