#define INTERVAL_2k 0
#define INTERVAL_8k 1
#define INTERVAL_10k 2
#include <Arduino.h>
#include <Bounce2.h> // https://github.com/thomasfredericks/Bounce2/archive/refs/heads/master.zip
const byte Red1_pin = 9;
const byte Yellow1_pin = 7;
const byte Green1_pin = 8;
const byte Red2_pin = 6;
const byte Yellow2_pin = 5;
const byte Green2_pin = 4;
const byte Bus_pin = 3;
const byte numIntervals = 3;
const unsigned int interval[numIntervals] = {2000, 8000, 10000};
byte intervalIndex = 0;
byte state = 1;
unsigned long starttime;
bool print = false;
bool pressedOneTime = false;
Bounce2::Button button = Bounce2::Button();
void printDebug(bool start = false)
{
if (start == true)
{
Serial.println("Starting...");
}
Serial.print("\nState: ");
Serial.print(state);
Serial.print(", Interval: ");
Serial.println(interval[intervalIndex]);
}
void controlLightsOfSectionOne(bool red1, bool yellow1, bool green1)
{
digitalWrite(Red1_pin, red1);
digitalWrite(Yellow1_pin, yellow1);
digitalWrite(Green1_pin, green1);
}
void controlLightsOfSectionTwo(bool red2, bool yellow2, bool green2)
{
digitalWrite(Red2_pin, red2);
digitalWrite(Yellow2_pin, yellow2);
digitalWrite(Green2_pin, green2);
}
void setup()
{
Serial.begin(9600);
button.attach(Bus_pin, INPUT_PULLUP);
button.interval(5);
button.setPressedState(LOW);
pinMode(Red1_pin, OUTPUT);
pinMode(Yellow1_pin, OUTPUT);
pinMode(Green1_pin, OUTPUT);
pinMode(Red2_pin, OUTPUT);
pinMode(Yellow2_pin, OUTPUT);
pinMode(Green2_pin, OUTPUT);
controlLightsOfSectionOne(LOW, LOW, LOW); // All lights OFF
controlLightsOfSectionTwo(LOW, LOW, LOW); // All lights OFF
printDebug(true);
starttime = millis();
}
void loop()
{
if ((millis() - starttime) >= interval[intervalIndex])
{
state++;
if (state > 5)
{
state = 2;
}
starttime = millis();
print = true;
}
button.update();
if (button.pressed())
{
Serial.println("Button Pressed!");
if ((pressedOneTime == false) && (state < 3))
{
pressedOneTime = true;
state = 3;
print = true;
starttime = millis();
}
}
switch (state)
{
case 1: // Idle state
{
intervalIndex = INTERVAL_2k;
controlLightsOfSectionOne(HIGH, LOW, LOW); // Red ON, yellow and green OFF
controlLightsOfSectionTwo(HIGH, LOW, LOW); // Red ON, yellow and green OFF
break;
}
case 2: // Green for section 1 and red for section 2
{
intervalIndex = INTERVAL_8k;
controlLightsOfSectionOne(LOW, LOW, HIGH);
controlLightsOfSectionTwo(HIGH, LOW, LOW);
pressedOneTime = false;
break;
}
case 3: // yellow for section 1 and red for section 2
{
if(pressedOneTime == true)
{
Serial.println("Fast green enabled");
}
intervalIndex = INTERVAL_2k;
controlLightsOfSectionOne(LOW, HIGH, LOW);
controlLightsOfSectionTwo(HIGH, LOW, LOW);
break;
}
case 4: // Red for section 1 and green for section 2
{
if(pressedOneTime == true)
{
intervalIndex = INTERVAL_2k;
}
else
{
intervalIndex = INTERVAL_10k;
}
controlLightsOfSectionOne(HIGH, LOW, LOW);
controlLightsOfSectionTwo(LOW, LOW, HIGH);
break;
}
case 5: // Yellow for section 2 and red for section 1
{
intervalIndex = INTERVAL_2k;
controlLightsOfSectionOne(HIGH, LOW, LOW);
controlLightsOfSectionTwo(LOW, HIGH, LOW);
break;
}
default:
{
break;
}
}
if(print == true)
{
print = false;
printDebug();
}
}