In my naive, knows the square-root of diddly-squat about railway signalling way, I attempted to produce a simple framework to factor the code, presented below.
Do with it as you will.
(uncompiled, untested)
struct Signal
{
byte redLED;
byte yellowLED;
byte greenLED;
};
Signal signals [] = {{2, 3, 4},
{5, 6, 7},
{8, 9, 10}};
const byte inPins [] = {14, 15, 16};
const byte nSwitches = sizeof (inPins) / sizeof (inPins [0]);
const int LED_OFF = LOW; // Depeding on how the LEDs are wired, you may
const int LED_ON = HIGH; // need to swap these over.
void setup()
{
for (auto & sig : signals ) {
pinMode(sig.redLED, OUTPUT);
pinMode(sig.yellowLED, OUTPUT);
pinMode(sig.greenLED, OUTPUT);
setSignalState (sig, LED_OFF, LED_OFF, LED_ON);
}
for (auto & switchPin : inPins) {
pinMode (switchPin, INPUT_PULLUP);
}
}
void setSignalState (Signal& sig, byte redState, byte yellowState, byte greenState)
{
digitalWrite (sig.redLED, redState);
digitalWrite (sig.yellowLED, yellowState);
digitalWrite (sig.greenLED, greenState);
}
void loop()
{
int switchState [nSwitches];
for (int i = 0; i < nSwitches; i++) {
switchState [i] = digitalRead (inPins [i]);
}
// your code here
}