Here is an example using millis() for timing. Note that my switches are wired to ground and use the internal pullup resistors as is common practice. Also I use the state change detection method to sense the start switch. You can see the state change detection method explained and the switch wiring in my state change detection for active low inputs tutorial. Also I replaced the Pedal Photointerrupter sensors with push button switches for development. This code uses the state machine approach. Another state machine tutorial.
The yellow lights are simultaneous for each lane so I represent them with one set of 3. There are green and red LEDs for each lane. A false start while any yellow is lit results in a red light for that lane. The first pedal button after the last yellow goes out will light that lanes green and lock the other out. The green lights come on right after the last yellow goes out. Restart by pressing the start button (no need to reset the processor).
const byte y1ledPin = 4;
const byte y2ledPin = 5;
const byte y3ledPin = 6;
const byte gLeftLedPin = 7;
const byte rLeftLedPin = 8;
const byte gRightLedPin = 9;
const byte rRightLedPin = 10;
const byte lPedalButtonPin = 11;
const byte rPedalButtonPin = 12;
const byte startButtonPin = A0;
unsigned long yellowInterval = 1000;
void setup()
{
Serial.begin(115200);
Serial.println("christmas tree starting");
pinMode(y1ledPin, OUTPUT);
pinMode(y2ledPin, OUTPUT);
pinMode(y3ledPin, OUTPUT);
pinMode(gLeftLedPin, OUTPUT);
pinMode(rLeftLedPin, OUTPUT);
pinMode(gRightLedPin, OUTPUT);
pinMode(rRightLedPin, OUTPUT);
pinMode(lPedalButtonPin, INPUT_PULLUP);
pinMode(rPedalButtonPin, INPUT_PULLUP);
pinMode(startButtonPin, INPUT_PULLUP);
}
void loop()
{
static byte mode = 0;
static unsigned long timer = 0;
static bool lastStartButtonState = HIGH;
if (mode == 0)
{
bool startButtonState = digitalRead(startButtonPin);
if (startButtonState != lastStartButtonState)
{
if (startButtonState == LOW)
{
digitalWrite(rRightLedPin, LOW);
digitalWrite(rLeftLedPin, LOW);
digitalWrite(gLeftLedPin, LOW);
digitalWrite(gRightLedPin, LOW);
timer = millis();
mode = 1;
}
}
}
// yellow light #1
if (mode == 1)
{
digitalWrite(y1ledPin, HIGH);
if (millis() - timer >= yellowInterval)
{
digitalWrite(y1ledPin, LOW);
timer = millis();
mode = 2;
}
// left red light
if (digitalRead(lPedalButtonPin) == LOW)
{
digitalWrite(y1ledPin, LOW);
digitalWrite(rLeftLedPin, HIGH);
mode = 0;
}
if (digitalRead(rPedalButtonPin) == LOW)
{
digitalWrite(y1ledPin, LOW);
digitalWrite(rRightLedPin, HIGH);
mode = 0;
}
}
// yellow light #2
if (mode == 2)
{
digitalWrite(y2ledPin, HIGH);
if (millis() - timer >= yellowInterval)
{
digitalWrite(y2ledPin, LOW);
timer = millis();
mode = 3;
}
// left red light
if (digitalRead(lPedalButtonPin) == LOW)
{
digitalWrite(y2ledPin, LOW);
digitalWrite(rLeftLedPin, HIGH);
mode = 0;
}
if (digitalRead(rPedalButtonPin) == LOW)
{
digitalWrite(y2ledPin, LOW);
digitalWrite(rRightLedPin, HIGH);
mode = 0;
}
}
// yellow light #3
if (mode == 3)
{
digitalWrite(y3ledPin, HIGH);
if (millis() - timer >= yellowInterval)
{
digitalWrite(y3ledPin, LOW);
timer = millis();
mode = 4;
}
// left red light
if (digitalRead(lPedalButtonPin) == LOW)
{
digitalWrite(y3ledPin, LOW);
digitalWrite(rLeftLedPin, HIGH);
mode = 0;
}
if (digitalRead(rPedalButtonPin) == LOW)
{
digitalWrite(y3ledPin, LOW);
digitalWrite(rRightLedPin, HIGH);
mode = 0;
}
}
// green lights
if (mode == 4)
{
//if(digitalRead(lPedalButtonPin) == LOW)
//{
digitalWrite(gLeftLedPin, HIGH);
mode = 0;
//}
//if(digitalRead(rPedalButtonPin) == LOW)
//{
digitalWrite(gRightLedPin, HIGH);
mode = 0;
//}
}
}
Use what you can. Hopefully this helps.