Traffic lights sequence using BlinkWithoutDelay and push button input issue

Good morning folks, I hoping one of you could point out where I’m going wrong with my traffic lights system using BlinkWithoutDelay.

First of all the philosophy of the system;

This is supposed to be a traffic lights system on a main road, where the light is always green for the main road (deemed TL1) until a car approaches the traffic lights from the side street, for now I’m simulating are car pulling up at the side street traffic lights (deemed TL2) using a push button. The idea is that if a car is detected at TL2, the car waits for 5 seconds before the sequence changes to let that car out, also, when the push button is pressed I bring on a separate LED to let me know the push button has been detected. The sketch then returns back to the TL1 being on until another car at TL2 is detected. I hope this makes sense.

First of all, I wrote a test sketch that cycles between TL1 & TL2 just to prove my circuit and basic code., this worked fine.

const int TL[] = {6, 7, 8, 9, 10, 11}; // TL pins
const int inputPinCount = 6;
 
unsigned long previousMillis = 0;
 
 
 
//----------------------------------------------------------------------------------------------------------------------------
 
void setup() {
for (int thisPin = 0; thisPin < inputPinCount; thisPin++) {
    pinMode(TL[thisPin], OUTPUT);
  }
Serial.begin(9600);
 
//digitalWrite(carGreen, 1); // initial traffic light state, carGreen on, pedRed off
}
 
void loop() {
unsigned long currentMillis = millis();
unsigned long difference = currentMillis - previousMillis;
delay(1000);
Serial.println(currentMillis);
Serial.println(difference);
if (difference < 1000)
{
TL2Stop();
TL1Stop();
}
else if (difference < 3000)
{
TL2Ready();
}
else if (difference < 8000)
{
TL2Go();
}
else if (difference < 10000)
{
TL2SD();
}
else if (difference < 11000)
{
TL2Stop();
}
else if (difference < 13000)
{
TL1Ready();
}
else if (difference < 18000)
{
TL1Go();
}
else if (difference < 20000)
{
TL1SD();
}
else {
TL1Stop();
previousMillis = currentMillis;
}
}
 
void TL1Stop (){
  digitalWrite(TL[5],1);
  digitalWrite(TL[4],0);
  digitalWrite(TL[3],0);
}
void TL1Ready (){
  digitalWrite(TL[5],1);
  digitalWrite(TL[4],1);
  digitalWrite(TL[3],0);
}
void TL1Go (){
  digitalWrite(TL[5],0);
  digitalWrite(TL[4],0);
  digitalWrite(TL[3],1);
}
void TL1SD (){
  digitalWrite(TL[5],0);
  digitalWrite(TL[4],1);
  digitalWrite(TL[3],0);
}
void TL2Stop (){
  digitalWrite(TL[2],1);
  digitalWrite(TL[1],0);
  digitalWrite(TL[0],0);
}
void TL2Ready (){
  digitalWrite(TL[2],1);
  digitalWrite(TL[1],1);
  digitalWrite(TL[0],0);
}
void TL2Go (){
  digitalWrite(TL[2],0);
  digitalWrite(TL[1],0);
  digitalWrite(TL[0],1);
}
void TL2SD (){
  digitalWrite(TL[2],0);
  digitalWrite(TL[1],1);
  digitalWrite(TL[0],0);
}

Now my code for the system described above.

const int TL[] = {5, 6, 7, 8, 9, 10, 11}; // TL pins
const int inputPinCount = 7;
const int carWaitingInput = 13;
unsigned long previousMillis = 0;
unsigned long currentMillis;
unsigned long difference;
 
void TL1Stop (){
  digitalWrite(TL[6],1);
  digitalWrite(TL[5],0);
  digitalWrite(TL[4],0);
}
void TL1Ready (){
  digitalWrite(TL[6],1);
  digitalWrite(TL[5],1);
  digitalWrite(TL[4],0);
}
void TL1Go (){
  digitalWrite(TL[6],0);
  digitalWrite(TL[5],0);
  digitalWrite(TL[4],1);
}
void TL1SD(){
  digitalWrite(TL[6],0);
  digitalWrite(TL[5],1);
  digitalWrite(TL[4],0);
}
void TL2Stop (){
  digitalWrite(TL[3],1);
  digitalWrite(TL[2],0);
  digitalWrite(TL[1],0);
}
void TL2Ready (){
  digitalWrite(TL[3],1);
  digitalWrite(TL[2],1);
  digitalWrite(TL[1],0);
}
void TL2Go (){
  digitalWrite(TL[3],0);
  digitalWrite(TL[2],0);
  digitalWrite(TL[1],1);
}
void TL2SD (){
  digitalWrite(TL[3],0);
  digitalWrite(TL[2],1);
  digitalWrite(TL[1],0);
}
 
 
//----------------------------------------------------------------------------------------------------------------------------
 
void setup() {
for (int thisPin = 0; thisPin < inputPinCount; thisPin++) {
    pinMode(TL[thisPin], OUTPUT);
  }
pinMode(carWaitingInput,INPUT);
Serial.begin(9600);
 
//digitalWrite(carGreen, 1); // initial traffic light state, carGreen on, pedRed off
 
}
 
 
void loop() {
 
int state = LOW;
TL2Stop();
TL1Go();
//Serial.println(currentMillis);
//Serial.println(difference);
state = digitalRead(carWaitingInput);
if (state == HIGH){
  Serial.print("Button Pressed");
  difference = currentMillis - previousMillis;
 if (difference < 5000)
 {
 digitalWrite(TL[0],1);
 }
 else if (difference < 7000)
 {
 TL1SD();
 }
 else if (difference < 8000)
 {
 TL1Stop();
 }
 else if (difference < 10000)
 {
 TL2Ready();
 }  
 else if (difference < 15000)
 {
 TL2Go();
 }  
 else if (difference < 17000)
 {
 TL2SD();
 }
 else if (difference < 18000)
 {
 TL2Stop();
 digitalWrite(TL[0],0);
 }
 else
 {
 TL1Ready();
 previousMillis = currentMillis;
 }
 
}
}

The code initialises fine, and sits with TL1 on green and TL2 on red. When I press the button to simulate a car, the separate LED comes on but then nothing else happens, I assume it’s something to do with how I’m timing the TL2 function, but I’ve spent hours trying different things and can’t anywhere, I’ve also had the system where some of the LEDs come on but REALLY dim, as if the output voltage was really low.

Another odd thing was that in the first code I posted, all the functions were defined after void loop() and this worked fine. When I tried this in the second code, it told me the function was not defined in the scope, so I’ve had to move the functions to before void setup(), I’m not sure why it worked on one and not the other.

Any help on this would be greatly appreciated.

We don't like going off site for code, so post it after you have read:- How to use this forum

Your second sketch never uses millis() and never sets currentMillis. You lost something from the first sketch there!

Leds lighting dimly can be caused by several things. Using digitalWrite(pin, HIGH) without first setting pinMode(pin, OUTPUT) is one way. Another is "runaway" code that is switching the led on and off at high speed (but more time off than on) is another. So effectively dimming it with software pwm, but unintentionally.

Well done for using an array, but you are not really using arrays to their best advantage. Your code is no shorter than it would have been without an array, except in setup(). I can give you some suggestions if you want.

Your indentation is a mess. Please click Tools-->Auto Format before you post the updated version.

@Grumpy_Mike.

My apologies, i've updated my post.

@PaulRB Thanks for your response, I've actually read up and re worked my system and using FSM which now works. I've also gone away from using arrays as i'm still new to the Arduino and I found following the code using arrays a little confusing, but I'd love to hear your suggestions on how to use them better, I do try to aim for more efficiency and I imagine as my programs get more complex, arrays will come in useful.

I've also taken on board your comment on my indentation, will keep and eye on this in the future.

Unfortunately using FSM has raised more questions, but I will post them in the appropriate section.

Cheers again

Unfortunately using FSM has raised more questions, but I will post them in the appropriate section.

The forum mods are likely to view that as cross posting and merge the two threads, so better to continue with this one.

Post your new code and I will try to suggest ways to use arrays.