#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
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
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?
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);
}
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.
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
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.
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