How to remove multiple loops and make the traffic signal program more efficient

#include <LiquidCrystal.h>
LiquidCrystal lcd(12,13,11,10,9,8);

int LED_RED = 0;
int LED_YELLOW = 1;
int LED_GREEN = 2;
int second;
int sec;
int i;
void setup()
{
  pinMode(LED_RED, OUTPUT);
  pinMode(LED_YELLOW, OUTPUT);
  pinMode(LED_GREEN, OUTPUT);
  lcd.clear();
  lcd.begin(16,2);
  lcd.setCursor(1,0);
  lcd.print("Traffic Signal!");
  delay(6000);
}
void loop()
{
 lcd.print("Traffic Signal!");
 for (int second = 60; second >=5; second--)
  {
    lcd.setCursor(0,2);
    lcd.print(second);
    digitalWrite(LED_RED, LOW);
    digitalWrite(LED_YELLOW, LOW);
  	digitalWrite(LED_GREEN, HIGH);
    delay(1000); 
  }
 for (int sec=4; sec>=0; sec--)
   {
   	 
     lcd.setCursor(0,2);
     lcd.print(sec);
     digitalWrite(LED_RED, LOW);
   	 digitalWrite(LED_YELLOW, HIGH);
     digitalWrite(LED_GREEN, LOW);	
     delay(1000);// Wait for 1000 millisecond(s)
  	}
 for (int i = 30; i>=0; i--)
    lcd.setCursor(0,2);
  	lcd.print("Traffic Signal!");
   	lcd.print(i);
    digitalWrite(LED_RED, HIGH);
    digitalWrite(LED_YELLOW, LOW);
   	digitalWrite(LED_GREEN, LOW);
    lcd.clear();
   	delay(1000); // Wait for 1000 millisecond(s)
    
}

Respected fellow community members,
I have an issue as to how I can make the entire thing work in a single loop and How I can make it a better traffic signal program. Eagerly waiting for any response from your end

While you wait, entertain yourself with your friend google

arduino blink without delay

and

arduino two things at once

and

arduino finite state machine

and

arduino finite state machine traffic lights

No time spent doing that will be a waste.

Poke around, give a few sites a few minutes each to see if any match your level of knowledge and preferred learning style.

HTH & L8R

a7

Thanks, alto777 pal, I would implement this and will let you know of my progress. So that if I misunderstand something it can be corrected with your help

Why not make a smart traffic signal? One that responds to a sensor and only changing the light if there's something waiting.

I had thought about that but the problem is how would U receive the density of vehicles crossing the road and then determine whether it's higher or lower? If it's higher, what is the maximum number of two-wheelers and four-wheelers that is safe for road crossing at the same time?

that would depend on the type of lane I agree but how will define the condition for overcrowding?

I would strongly suggest improving your code before adding features.

please do help me with how can I improve this code (mentioned above)

You need to immediately go study the methods in reply #2.

One really obvious deficiency, is that the lights only control traffic in one direction.

1 Like

about the code

Why do you many ( more than fifty) times outputs to the lamps the same signal?
Do you think it would be better to move repeated tasks outside the loop?

  digitalWrite(LED_RED, LOW);
  digitalWrite(LED_YELLOW, LOW);
  digitalWrite(LED_GREEN, HIGH);
 for (int second = 60; second >=5; second--)
  {
    lcd.setCursor(0,2);
    lcd.print(second);
    delay(1000); 
  }

You beat me to it!

I'd build off what aarg said,

and I would further ask why is there an LCD screen at all? Do one thing well, and there's no need to have an LCD screen that tells you that it's a traffic signal. I mean, real ones don't come with that sort of signage, right? At least most don't (except for walk signal timers where I live, but that's just the walk signal).

They might actually have one inside the control box. Although, I'm sure they never display "Traffic Signal!".

What would be "fun" is to use the LCD to emulate the pedestrian countdown sign (we have them where I live, I hope you know what I mean...). That starts a countdown a while after a green signal, to tell pedestrians how much time they have left to cross before a red signal.

Actually, if it is just an academic exercise, you could just use the LCD and not use LEDs at all.

But usually, the entire reason for the assignment, is to learn about sequencing and driving LEDs, then ambush the student with the requirement to add a push button for the pedestrian crossing request. That usually sends everyone to this forum for answers. :slight_smile:

May be OP want to add screen with timer that shows estimated time to signal switching.
9776a0b9ff460caa20c49e59341f9676

where I live we have countdown timers on all traffic signals, not only pedestrian.

yes

I wanted to add a timer so that people can be attentive as to when the signal changes and turn off their vehicles and help in the conservation of the planet in one way

1 Like

You should move your connections away from pin 0 and 1. Those are reserved for serial comms.

Thanks, I just shifted them

Technically, you don't need a state machine. But if you use one now, you will be sitting back at the beach with a pina colada when the rest of the class is flabbergasted by the "surprise" request for the additional feature of a push button. :slight_smile:

yes your advice made code much more sensible
Now my main problem...
Actually when the red light should light instead a green light gets lit up when I interchanged the state of both green and red (as shown in the code above) they worked! I want to understand why that works the way it did.
Like U just pointed out a very elegant way of making my code a better one please help me understand and root out the error in my way of writing this code