#define BIG_G 2
#define BIG_Y 1
#define BIG_R 0
#define Small_r 3
#define Small_g 4
#define BIG_G2 7
#define BIG_Y2 6
#define BIG_R2 5
#define Small_r2 8
#define Small_g2 9
#define A 19
#define B 18
#define C 24
#define D 22
#define E 23
#define F 20
#define G 21
#define s_A 15
#define s_B 14
#define s_C 29
#define s_D 26
#define s_E 27
#define s_F 16
#define s_G 17
unsigned long previousMillis = 0;
const long interval = 1000; // Interval for changing light
bool greenLightOn = false;
bool yellowLightOn = false;
bool redLightOn = false;
bool greenLightOn2 = false;
bool yellowLightOn2 = false;
bool redLightOn2 = false;
bool greenLightOn3 = false;
bool redLightOn3 = false;
bool ButttonOn = false;
void setup()
{
pinMode(BIG_G, OUTPUT);
pinMode(BIG_Y, OUTPUT);
pinMode(BIG_R, OUTPUT);
pinMode(Small_r, OUTPUT);
pinMode(Small_g, OUTPUT);
pinMode(BIG_G2, OUTPUT);
pinMode(BIG_Y2, OUTPUT);
pinMode(BIG_R2, OUTPUT);
pinMode(Small_r2, OUTPUT);
pinMode(Small_g2, OUTPUT);
pinMode(A, OUTPUT);
pinMode(B, OUTPUT);
pinMode(C, OUTPUT);
pinMode(D, OUTPUT);
pinMode(E, OUTPUT);
pinMode(F, OUTPUT);
pinMode(G, OUTPUT);
pinMode(s_A, OUTPUT);
pinMode(s_B, OUTPUT);
pinMode(s_C, OUTPUT);
pinMode(s_D, OUTPUT);
pinMode(s_E, OUTPUT);
pinMode(s_F, OUTPUT);
pinMode(s_G, OUTPUT);
pinMode(11, INPUT_PULLUP);
}
void loop()
{
unsigned long currentMillis = millis();
// Check button input constantly
if (digitalRead(11) == LOW)
{
WalkerLightsOrder(yellowLightOn2, greenLightOn3, redLightOn3);
ButttonOn = true;
}
if (ButttonOn)
{
if (currentMillis - previousMillis >= interval)
{
previousMillis = currentMillis;
if (!greenLightOn3)
{
SredLightOn();
redLightOff2();
greenLightOff();
SgreenLightOn2();
SmallSecondsOff();
threeSecongSmall();
SecondsOff();
threeSecong();
GreenLightOn();
GreenLightOff2();
YellowLightOff();
RedLightOff();
RedLightOn2();
greenLightOn3 = true;
}
else if (!greenLightOn2)
{
SredLightOn();
redLightOff2();
greenLightOff();
SgreenLightOn2();
SmallSecondsOff();
twoSecongSmall();
SecondsOff();
twoSecong();
GreenLightOn();
GreenLightOff2();
YellowLightOff();
RedLightOff();
RedLightOn2();
greenLightOn2 = true;
}
else if (!greenLightOn)
{
// Turn on green light
SredLightOn();
redLightOff2();
greenLightOff();
SgreenLightOn2();
SmallSecondsOff();
oneSecongSmall();
SecondsOff();
oneSecong();
GreenLightOn();
GreenLightOff();
GreenLightOn();
GreenLightOff();
GreenLightOn();
GreenLightOff2();
YellowLightOff();
RedLightOff();
RedLightOn2();
greenLightOn = true;
ButttonOn = false;
}
else if (!yellowLightOn2)
{
// Turn on yellow light
redLightOff();
greenLightOff();
redLightOff2();
greenLightOff2();
SecondsOff();
twoSecong();
SmallSecondsOff();
twoSecongSmall();
GreenLightOff();
GreenLightOff2();
YellowLightOn();
RedLightOff();
RedLightOff2();
yellowLightOn2 = true;
}
else if (!yellowLightOn)
{
// Turn on yellow light
redLightOff();
greenLightOff();
redLightOff2();
greenLightOff2();
SecondsOff();
oneSecong();
SmallSecondsOff();
oneSecongSmall();
GreenLightOff();
GreenLightOff2();
YellowLightOn();
RedLightOff();
RedLightOff2();
yellowLightOn = true;
ButttonOn = false;
}
else if (!redLightOn3)
{
SredLightOn2();
redLightOff();
greenLightOff2();
SgreenLightOn();
SecondsOff();
threeSecong();
SmallSecondsOff();
threeSecongSmall();
GreenLightOn2();
GreenLightOff();
YellowLightOff();
RedLightOff2();
RedLightOn();
redLightOn3 = true;
}
else if (!redLightOn2)
{
SredLightOn2();
redLightOff();
greenLightOff2();
SgreenLightOn();
SecondsOff();
twoSecong();
SmallSecondsOff();
twoSecongSmall();
GreenLightOn2();
GreenLightOff();
YellowLightOff();
RedLightOff2();
RedLightOn();
redLightOn2 = true;
}
else if (!redLightOn)
{
// Turn on red light
SredLightOn2();
redLightOff();
greenLightOff2();
SgreenLightOn();
SecondsOff();
oneSecong();
SmallSecondsOff();
oneSecongSmall();
GreenLightOn2();
GreenLightOff2();
GreenLightOn2();
GreenLightOff2();
GreenLightOn2();
GreenLightOff2();
GreenLightOff();
YellowLightOff();
RedLightOff2();
RedLightOn();
redLightOn = true;
ButttonOn = false;
}
else
{
// Reset to green
SredLightOn();
redLightOff2();
greenLightOff();
SgreenLightOn2();
SmallSecondsOff();
threeSecongSmall();
SecondsOff();
threeSecong();
GreenLightOn();
GreenLightOff2();
YellowLightOff();
RedLightOff();
RedLightOn2();
greenLightOn = false;
yellowLightOn = false;
redLightOn = false;
greenLightOn2 = false;
yellowLightOn2 = false;
redLightOn2 = false;
greenLightOn3 = false;
redLightOn3 = false;
ButttonOn = false;
}
}
}
else if (!ButttonOn && currentMillis - previousMillis >= interval)
{
redLightOff();
greenLightOff();
redLightOff2();
greenLightOff2();
SmallSecondsOff();
}
// Handle traffic light sequence without delay
if (currentMillis - previousMillis >= interval)
{
previousMillis = currentMillis;
if (!greenLightOn3)
{
SecondsOff();
threeSecong();
GreenLightOn();
GreenLightOff2();
YellowLightOff();
RedLightOff();
RedLightOn2();
greenLightOn3 = true;
}
else if (!greenLightOn2)
{
SecondsOff();
twoSecong();
GreenLightOn();
GreenLightOff2();
YellowLightOff();
RedLightOff();
RedLightOn2();
greenLightOn2 = true;
}
else if (!greenLightOn)
{
// Turn on green light
SecondsOff();
oneSecong();
GreenLightOn();
GreenLightOff();
GreenLightOn();
GreenLightOff();
GreenLightOn();
GreenLightOff2();
YellowLightOff();
RedLightOff();
RedLightOn2();
greenLightOn = true;
}
else if (!yellowLightOn2)
{
SecondsOff();
twoSecong();
GreenLightOff();
GreenLightOff2();
YellowLightOn();
RedLightOff();
RedLightOff2();
yellowLightOn2 = true;
}
else if (!yellowLightOn)
{
// Turn on yellow light
SecondsOff();
oneSecong();
GreenLightOff();
GreenLightOff2();
YellowLightOn();
RedLightOff();
RedLightOff2();
yellowLightOn = true;
}
else if (!redLightOn3)
{
SecondsOff();
threeSecong();
GreenLightOn2();
GreenLightOff();
YellowLightOff();
RedLightOff2();
RedLightOn();
redLightOn3 = true;
}
else if (!redLightOn2)
{
SecondsOff();
twoSecong();
GreenLightOn2();
GreenLightOff();
YellowLightOff();
RedLightOff2();
RedLightOn();
redLightOn2 = true;
}
else if (!redLightOn)
{
// Turn on red light
SecondsOff();
oneSecong();
GreenLightOn2();
GreenLightOff2();
GreenLightOn2();
GreenLightOff2();
GreenLightOn2();
GreenLightOff();
YellowLightOff();
RedLightOff2();
RedLightOn();
redLightOn = true;
}
else
{
// Reset to green
SecondsOff();
threeSecong();
GreenLightOn();
GreenLightOff2();
YellowLightOff();
RedLightOff();
RedLightOn2();
greenLightOn = false;
yellowLightOn = false;
redLightOn = false;
greenLightOn2 = false;
yellowLightOn2 = false;
redLightOn2 = false;
greenLightOn3 = false;
redLightOn3 = false;
}
}
}
void WalkerLightsOrder(bool yellowLightOn2, bool greenLightOn3, bool redLightOn3)
{
if (redLightOn3 && yellowLightOn2 && greenLightOn3)
{
SredLightOn();
redLightOff2();
greenLightOff();
SgreenLightOn2();
}
else if (yellowLightOn2 && greenLightOn3)
{
redLightOff();
greenLightOff();
redLightOff2();
greenLightOff2();
}
else if (greenLightOn3)
{
SredLightOn2();
redLightOff();
greenLightOff2();
SgreenLightOn();
}
}
void RedLightOn()
{
digitalWrite(BIG_R, HIGH);
}
void RedLightOff()
{
digitalWrite(BIG_R, LOW);
}
void RedLightOn2()
{
digitalWrite(BIG_R2, HIGH);
}
void RedLightOff2()
{
digitalWrite(BIG_R2, LOW);
}
void GreenLightOn()
{
digitalWrite(BIG_G, HIGH);
}
void GreenLightOff()
{
digitalWrite(BIG_G, LOW);
delay(100);
}
void GreenLightOn2()
{
digitalWrite(BIG_G2, HIGH);
}
void GreenLightOff2()
{
digitalWrite(BIG_G2, LOW);
delay(100);
}
void YellowLightOn()
{
digitalWrite(BIG_Y, HIGH);
digitalWrite(BIG_Y2, HIGH);
}
void YellowLightOff()
{
digitalWrite(BIG_Y, LOW);
digitalWrite(BIG_Y2, LOW);
}
// small
void SredLightOn()
{
digitalWrite(Small_r, HIGH);
}
void redLightOff()
{
digitalWrite(Small_r, LOW);
}
void SgreenLightOn()
{
digitalWrite(Small_g, HIGH);
}
void greenLightOff()
{
digitalWrite(Small_g, LOW);
}
void SredLightOn2()
{
digitalWrite(Small_r2, HIGH);
}
void redLightOff2()
{
digitalWrite(Small_r2, LOW);
}
void SgreenLightOn2()
{
digitalWrite(Small_g2, HIGH);
}
void greenLightOff2()
{
digitalWrite(Small_g2, LOW);
}
void SecondsOff()
{
digitalWrite(A, LOW);
digitalWrite(B, LOW);
digitalWrite(C, LOW);
digitalWrite(D, LOW);
digitalWrite(E, LOW);
digitalWrite(F, LOW);
digitalWrite(G, LOW);
}
void SmallSecondsOff()
{
digitalWrite(s_A, LOW);
digitalWrite(s_B, LOW);
digitalWrite(s_C, LOW);
digitalWrite(s_D, LOW);
digitalWrite(s_E, LOW);
digitalWrite(s_F, LOW);
digitalWrite(s_G, LOW);
}
void oneSecong()
{
// digitalWrite(2, HIGH); // Wait for 1000 millisecond(s)
digitalWrite(C, HIGH);
digitalWrite(B, HIGH); // Wait for 1000 millisecond(s)
}
void oneSecongSmall()
{
// digitalWrite(2, HIGH); // Wait for 1000 millisecond(s)
digitalWrite(s_C, HIGH);
digitalWrite(s_B, HIGH); // Wait for 1000 millisecond(s)
}
void twoSecong()
{
digitalWrite(A, HIGH); // Wait for 1000 millisecond(s)
digitalWrite(B, HIGH);
digitalWrite(G, HIGH); // Wait for 1000 millisecond(s)
digitalWrite(E, HIGH);
digitalWrite(D, HIGH); // Wait for 1000 millisecond(s)
}
void threeSecong()
{
digitalWrite(A, HIGH); // Wait for 1000 millisecond(s)
digitalWrite(B, HIGH);
digitalWrite(C, HIGH); // Wait for 1000 millisecond(s)
digitalWrite(G, HIGH);
digitalWrite(D, HIGH); // Wait for 1000 millisecond(s)
}
void threeSecongSmall()
{
digitalWrite(s_A, HIGH); // Wait for 1000 millisecond(s)
digitalWrite(s_B, HIGH);
digitalWrite(s_C, HIGH); // Wait for 1000 millisecond(s)
digitalWrite(s_G, HIGH);
digitalWrite(s_D, HIGH); // Wait for 1000 millisecond(s)
}
void twoSecongSmall()
{
digitalWrite(s_A, HIGH); // Wait for 1000 millisecond(s)
digitalWrite(s_B, HIGH);
digitalWrite(s_G, HIGH); // Wait for 1000 millisecond(s)
digitalWrite(s_E, HIGH);
digitalWrite(s_D, HIGH); // Wait for 1000 millisecond(s)
}