if it is of any help, I have a simple traffic light sketch without delays:
// Traffic Lights
// for https://forum.arduino.cc/index.php?topic=649027.0
// by noiasca
// introduce the taffic manager
/* State NorthWest WestEast
NorthGo green red
NorthAwaitRed yellow red
NorthRed red red
WestAwaitGreen red red yellow
WestGo red green
WestAwaitRed red yellow
WestRed red red
NorthAwaitGreen red yellow red
*/
enum trafficLightColor {red, redyellow, green, yellow}; // available colors/colorcombinations on a traffic lights
// row, colum
const uint8_t sequenceA [8][3]
{
{ 8, green, red}, //interval, north, west
{ 2, yellow, red},
{ 2, red, red},
{ 2, red, redyellow},
{ 8, red, green},
{ 2, red, yellow},
{ 2, red, red},
{ 2, redyellow, red}
};
byte trafficStateCurrent = 0; // the current state of the traffic
const uint16_t factorIntervall = 500; // a factor for the intervall
class TrafficLight {
trafficLightColor state = red;
unsigned long previousMillis;
const byte ledPinRed;
const byte ledPinYellow;
const byte ledPinGreen;
public:
TrafficLight(byte red, byte yellow, byte green):
ledPinRed(red),
ledPinYellow(yellow),
ledPinGreen(green)
{}
void begin()
{
pinMode(ledPinRed, OUTPUT);
pinMode(ledPinYellow, OUTPUT);
pinMode(ledPinGreen, OUTPUT);
}
void setState(trafficLightColor _state)
{
state = _state;
switch (state)
{
case red :
digitalWrite(ledPinRed, HIGH);
digitalWrite(ledPinYellow, LOW);
digitalWrite(ledPinGreen, LOW);
break;
case redyellow :
digitalWrite(ledPinRed, HIGH);
digitalWrite(ledPinYellow, HIGH);
digitalWrite(ledPinGreen, LOW);
break;
case green :
digitalWrite(ledPinRed, LOW);
digitalWrite(ledPinYellow, LOW);
digitalWrite(ledPinGreen, HIGH);
break;
case yellow :
digitalWrite(ledPinRed, LOW);
digitalWrite(ledPinYellow, HIGH);
digitalWrite(ledPinGreen, LOW);
break;
}
}
};
TrafficLight trafficLightNorth(2, 3, 4); // the traffic light North-South and the LED pins for red, yellow, green
TrafficLight trafficLightWest(5, 6, 7); // the traffic light West-East and the LED pins for red, yellow,
void setup() {
Serial.begin(115200);
Serial.println(F("traffic lights"));
trafficLightNorth.begin();
trafficLightWest.begin();
}
void runTrafficController() {
static uint32_t lastMillis = -10000; // last update
static uint8_t trafficStateIntervall = 0;
if (millis() - lastMillis >= trafficStateIntervall * factorIntervall) // do something by time
{
lastMillis = millis();
trafficStateCurrent++; // --> simple switch to next state
if (trafficStateCurrent >= 8) trafficStateCurrent = 0; //MISSING magic number ... abhängig von der jeweiligen Sequence
Serial.print(F("sequenceA:")); Serial.println(trafficStateCurrent);
trafficStateIntervall = sequenceA[trafficStateCurrent][0];
trafficLightNorth.setState(sequenceA[trafficStateCurrent][1]);
trafficLightWest.setState(sequenceA[trafficStateCurrent][2]);
}
}
void loop()
{
runTrafficController();
// do what ever you want unblocked here
}
adding a digitalRead and force the state to a yellow/red should be easy.
this was the old thread: LED Sequence Setup - LEDs and Multiplexing - Arduino Forum