Arduino Multiple Turnout Control Using Momentary Contact Buttons

Arduino Multiple Turnout Control Using Momentary Contact Buttons LEDs On Control Panel And Each Leg Of The Turnout

This is a very useful sketch that you can use for 1 or more turnouts. If you use it to control a single turnout, you can use an arduino nano as your Arduino board.

 

The Sketch

[code]
/*One of more turnouts with 2 leds on control panel and 2 on each leg of the turnout
Control is via momentary contact button
Wiring diagram and video at https://steamtraininfo.com/turnout-control
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;
int ledMain1 = 2;
int ledDivergent1 = 3;
int main1green = 4;
int main1red = 5;
int divergent1green = 7;
int divergent1red = 8;

#include <Servo.h>

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

pinMode(ledMain1, OUTPUT);
pinMode(ledDivergent1, OUTPUT);
pinMode(main1green, OUTPUT);
pinMode(main1red, OUTPUT);
pinMode(divergent1green, OUTPUT);
pinMode(divergent1red, OUTPUT);

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;
digitalWrite(ledMain1, HIGH);
digitalWrite(ledDivergent1, LOW);
digitalWrite(main1green, HIGH);
digitalWrite(main1red, LOW);
digitalWrite(divergent1green, LOW);
digitalWrite(divergent1red, HIGH);
}
}

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
digitalWrite(ledDivergent1, HIGH);
digitalWrite(ledMain1, LOW);
digitalWrite(main1green, LOW);
digitalWrite(main1red, HIGH);
digitalWrite(divergent1green, HIGH);
digitalWrite(divergent1red, LOW);
}
}
void switchstraight(int buttonRead) {
for (pos = stop1; pos >= start1; pos -= 1) {
myservo.write(pos);
delay(15);
switchState = ST_OFF1; //chages to ST_OFF1
}
}
[/code]

 

 


We hope that you found the information in this page useful. The internet is all about getting and giving. And giving back is the best. Please donate $1 or more to aid us in our continuing quest to add to and polish the information in our site.


Scroll to top