Slow Moving Turnout

This arduino sketch is used to control slow moving turnouts using a toggle switch.

If you use a DPST toggle, you can add LEDs to and activate other animations from the other pole.

[code]
/*One of more turnouts
Control is via momentary contact button
Wiring diagram and video at
This sketch is a modified version of Basic Servio by DIY And Digital Railroad at https://www.youtube.com/watch?v=dodydRF7BBw
Modified by Gary Granai 11 April 2021
www.steamtraininfo.com
*/

 

const int button = A0;
int start1=0;
int stop1=180;

#include <Servo.h>

Servo myservo;
int pos = 0;
void setup () {

Serial.begin(9600);
myservo.attach(6);

}
enum SWITCHSTATES
{
ST_OFF1,
ST_OFF2,
ST_STRAIGHT,
ST_TURN,
};
SWITCHSTATES switchState = ST_OFF1;

void loop() {
int buttonRead = analogRead(button);
delay(200);
switch (switchState)
{
case ST_OFF1:
switchoff1(buttonRead);
break;
case ST_OFF2:
switchoff2(buttonRead);
break;
case ST_STRAIGHT:
switchstraight(buttonRead);
break;
case ST_TURN:
switchturn(buttonRead);
break;
}
}
void switchoff1(int buttonRead) {
myservo.write(start1);

if (buttonRead > 500) {
switchState = ST_TURN;
}
}

void switchturn(int buttonRead) {
for (pos = start1; pos <= stop1; pos += 1) {
myservo.write(pos);
delay(15);
}
switchState = ST_OFF2; //chages to ST_OFF2
}

void switchoff2(int buttonRead) {
myservo.write(stop1);

if (buttonRead < 500) {
switchState = ST_STRAIGHT; //switches to Straight
}
}
void switchstraight(int buttonRead) {
for (pos = stop1; pos >= start1; pos -= 1) {
myservo.write(pos);
delay(15);
switchState = ST_OFF1; //chages to ST_OFF1
}

}
[/code]

Tell others about this information by using one of these services. Thank you for the support.
Scroll to top