looks like a model RR signal controller. not sure about your use of a button.
if the IR sensor is active, shouldn't it set the signal LEDs and ignore the buttons? in other words, only look at the button count if the IR sensor is not active
consier
#undef MyHW // my hardware
#ifdef MyHW
const int buttonPin = A1;
const int IRSensor = A2;
const int ledPinredTop = 13;
const int ledPinyellowTop = 12;
const int ledPingreenTop = 11;
const int ledPinredBtm = 10;
const int ledPingreenBtm = 9;
#else
const int buttonPin = 7;
const int ledPinredTop = 4;
const int ledPinyellowTop = 3;
const int ledPingreenTop = 2;
const int ledPingreenBtm = 5;
const int ledPinredBtm = 6;
const int IRSensor = 8;
#endif
// Variables will change:
int butCnt = 0;
int butState;
enum { Off = HIGH, On = LOW };
// -----------------------------------------------------------------------------
struct Sig {
byte pinRed;
byte pinYellow;
byte pinGreen;
};
#define PinNone 0
Sig sigTop = { ledPinredTop, ledPinyellowTop, ledPingreenTop };
Sig sigBtm = { ledPinredBtm, PinNone, ledPingreenBtm };
void
sig (
Sig &sig,
byte red,
byte yellow,
byte green )
{
digitalWrite (sig.pinRed, red);
digitalWrite (sig.pinGreen, green);
if (PinNone != sig.pinYellow)
digitalWrite (sig.pinYellow, yellow);
}
// -----------------------------------------------------------------------------
enum { Stop, Clear, AppDiv, App };
void loop()
{
byte but = digitalRead(buttonPin);
if (butState != but) {
butState = but;
delay(10);
if (LOW == but)
butCnt++;
}
if (On == digitalRead (IRSensor)) {
butCnt = 1;
}
switch (butCnt) {
case Stop: //STOP - RED OVER RED
sig (sigTop, On, Off, Off);
sig (sigBtm, On, Off, Off);
break;
case Clear: //CLEAR - GREEN OVER RED
sig (sigTop, Off, Off, On);
sig (sigBtm, On, Off, Off);
break;
case AppDiv: //APPROACH DIVERGING - YELLOW OVER GREEN
sig (sigTop, Off, On, Off);
sig (sigBtm, Off, Off, On);
break;
case App: //APPROACH - YELLOW OVER RED
sig (sigTop, Off, On, Off);
sig (sigBtm, On, Off, Off);
break;
default:
butCnt = Stop;
break;
}
}
// -----------------------------------------------------------------------------
void setup() {
pinMode(buttonPin, INPUT_PULLUP);
butState = digitalRead (buttonPin);
pinMode(IRSensor, INPUT_PULLUP);
// initialize the LED as an output:
pinMode(ledPinredTop, OUTPUT);
pinMode(ledPinyellowTop, OUTPUT);
pinMode(ledPingreenTop, OUTPUT);
pinMode(ledPingreenBtm, OUTPUT);
pinMode(ledPinredBtm, OUTPUT);
// initialize serial communication:
Serial.begin(9600);
}