Model railroad servo based turnouts

Could someone please modify the attached sketch so the servos remain in the last position when I switch the power off and then on.

#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,  60,  60,  60,  60,  50,  }; // degrees for low servo position
byte servoHighPos[] = {160, 140, 160, 170, 150, 160,}; // 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) {
    while (servoPos[n] != servoLowPos[n]) {
         servoPos[n] -= 1;
         pointServo[n].write(servoPos[n]);
         delay(10);
     }
}

if (sw == LOW) {
    while (servoPos[n] != servoHighPos[n]) {
         servoPos[n] += 1;
         pointServo[n].write(servoPos[n]);
         delay(10);
     }
}
     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 code would need to store the position of each servo in EEPROM after each change

and then on startup, read the EEPROM and set the servo to the value that value

if you're using toggle switches, won't the points move to the position of the toggle switches?

maybe just delete the servo.write() in setup()

Thanks for your reply,

Yes I am using toggle switches, But every time I power up I have to move the toggle switches to get the servos to move otherwise they stay in a neutral position.

I will check servo .write in the setup.

~WRD0000.jpg

suggest you initialize servoPos [] basedon the switch position in startup

void setup() {
    Serial.begin (9600);

    for (byte n = 0; n < 6; n++) {
        pinMode(switchPin[n], INPUT_PULLUP);

        if (HIGH == digitalRead(switchPin[n]))
            servoPos[n] = servoLowPos[n];
        else
            servoPos[n] = servoHighPos[n];

        pointServo[n].attach(servoPin[n]);
        pointServo[n].write(servoPos[n]);
    }
}
#include <Servo.h>
Servo Srv[6];

const byte servoPin[] =     {  2,   3,   4,   5,   6,   7 }; // pin numbers for servo signals
const byte switchPin[] =    { 8,  9,  10,  11,  12, 13 }; // pin numbers for switch connections
const byte servoLowPos[] =  { 50,  60,  60,  60,  60,  50  }; // degrees for low servo position
const byte servoHighPos[] = {160, 140, 160, 170, 150, 160 }; // degrees for high servo position

void setup() {
  for (byte n = 0; n < 6; n++) {
    pinMode(switchPin[n], INPUT_PULLUP);
    Srv[n].attach(servoPin[n]);
  }
}

void loop() {
  for (byte n = 0; n < 6; n++) {
    if (digitalRead(switchPin[n]))Srv[n].write(servoLowPos[n]);
    else Srv[n].write(servoHighPos[n]);
  }
  delay(10);
}

Thank you for your suggestion, I modified my sketch as attached but received this message on line 17 (serv was not declared)
Could please check this and show me where I have gone wrong.
Tanks Wally.

 #include <Servo.h>

Servo Srv[6];

const byte servoPin[] =     {  2,   3,   4,   5,   6,   7, }; // pin numbers for servo signals
const byte switchPin[] =    { 8,  9,  10,  11,  12, 13 }; // pin numbers for switch connections
// Uno analog pins      A5   A4   A3   A2   A1   A0
const byte servoLowPos[] =  { 50,  60,  60,  60,  60,  50,  }; // degrees for low servo position
const byte servoHighPos[] = {160, 140, 160, 170, 150, 160,}; // degrees for high servo position

byte servoPos[6];


void setup() {
  Serial.begin (9600);

 for (byte n = 0; n < 6; n++) {
   pinMode (switchPin[n], INPUT_PULLUP);

   if (HIGH == digitalRead(switchPin[n]))
    servoPos [n] = servoLowPos [n];
   else
    servoPos[n]  = servoHighPos [n];

  pointServo [n] . attach(servoPin[n]);  
  pointServo [n] . write(servoPos[n];
 }
}

void loop() {
  for (byte n = 0; n < 6; n++) {
     if (digitalRead(switchPin[n])) Srv [n] . write(seservoLowPos[n] );
     els Serv[n] .write(servoHighPos [n]);
  }
         delay(10);
     
}

if (sw == LOW) {
    while (servoPos[n] != servoHighPos[n]) {
         servoPos[n] += 1;
         pointServo[n].write(servoPos[n]);
         delay(10);
     }
}
     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);
  }
}
 code here

No compilation error for "els"?

you should use proposed sketch entirely. you asking for help because your sketch was not working as needed, so why you try your own sketch again?
well, at least it was compilable, then you did change and now it not compile, so you take back made changes.

seems servo is not suitable for your need.

I want to add that EEPROMS wear out after ca 100000 writecycles
So if y code would write the new position
once every 5 seconds
The EEPROM becomes unuseable after
100000 / (60 / 5 times per Minute * 60 minutes / hour) = 138 hours of operation

In this case you should use an FRAM-chip to store the servo-positions
FRAMs have 10^12 writecycles (1 000 000 000 000)

So the question is how often do you change the servo-positions?
Would it be sufficient to drive the servos to a pre-defined position
or
do you have this situation:
the servo-positions change very often before power off
and shall remain in this position on power on again.

Edit: you asked for

So I assume that you have almost no knowledge about programming.
Hm - well - .. --
will this be the only change to the code or is it very likely that you want additional changes?

If you want additional changes I suggest that you start learning programming. The forum will support you with an unlimited number of answers as long as you show some own effort in learning.

But it is unlikely that a user will do all the work for you again and again and again.

best regards Stefan

Thank you for your suggestion, I modified my sketch as attached but received this message on line 17 (serv was not declared)
Could please check this and show me where I have gone wrong.
Tanks Wally.

#include <Servo.h>

Servo Srv[6];

const byte servoPin[] =     {  2,   3,   4,   5,   6,   7, }; // pin numbers for servo signals
const byte switchPin[] =    { 8,  9,  10,  11,  12, 13 }; // pin numbers for switch connections
// Uno analog pins      A5   A4   A3   A2   A1   A0
const byte servoLowPos[] =  { 50,  60,  60,  60,  60,  50,  }; // degrees for low servo position
const byte servoHighPos[] = {160, 140, 160, 170, 150, 160,}; // degrees for high servo position

byte servoPos[6];


void setup() {
 for (byte n = 0; n < 6; n++) {
   pinMode (switchPin[n], INPUT_PULLUP);
   serv[n].attach(servoPin[n]);
 }
}

void loop() {
  for (byte n = 0; n < 6; n++) {
     if (digitalRead(switchPin[n])) Srv [n] . write(seservoLowPos[n] );
     els Serv[n] . write(servoHighPos [n]);
  }
         delay(10);
     
}

if (sw == LOW) {
    while (servoPos[n] != servoHighPos[n]) {
         servoPos[n] += 1;
         pointServo[n].write(servoPos[n]);
         delay(10);
     }
}
     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);
  }
}

servo is not suitable for your need. your sketch is not suitable for your need. even if you rename variable correctly

could you please explain why servos are not suitable?
Do you have any additional information about @Greatnorthern 's project?

@Greatnorthern
could you give a normal worded description about your project as a whole and especially what the servos are doing in your project?
best regards Stefan

Tom.. :grinning: :+1: :coffee: :australia:

I am 80 years old and am a model railroader not a person who needs to learn how to program sophisticated projects. I only can make modifications to sketches with help.
Thank you.

his code did work
he asked if the switch positions could be maintained after power up
they can because he uses toggle switches, but his code always initialized the switches to one position

his code also moved the servos slowly, one step / 10 msec

the suggestion was to simply not initialize the servos in setup and allow the toggle to initialize the switch position if not already there (the toggle could have been thrown while off)

so his own sketch simply needed some simple modification

  1. usually servos going to 90° on power up
  2. current position of servo can't be read

I have 6 turnouts on a section of my model railroad layout these are operated by 6 servos using 6 toggle switches. The sketch that I have works fine with the exception that the servos do not move to the switch position on a power up.

I have to move each toggle switch in turn the get the sevos to the correct position.

Thanks Wally

~WRD0000.jpg

just like i done in my proposed sketch