Hello!
I want to build a Traffic Light with LCD in which time left for signal will be shown. I manage to make the wiring (see the diagram) and also I build the code.
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#define SDA_PIN 21
#define SCL_PIN 22
int red = 13;
int yellow = 33;
int green = 32;
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup() {
Wire.begin(SDA_PIN, SCL_PIN);
pinMode(red, OUTPUT);
pinMode(yellow, OUTPUT);
pinMode(green, OUTPUT);
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Turning On");
lcd.setCursor(0, 1);
lcd.print("Traffic Light");
delay(2000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("SYSTEM");
lcd.setCursor(0, 1);
lcd.print("ON");
delay(1500);
lcd.clear();
}
void loop() {
// Red light
displayLight("Stop Red Light", 15, red);
// Yellow light
displayBlinkingLight("On Yellow Light", 5, yellow);
// Green light
displayLight("GoGo Green Light", 20, green);
// Yellow light again
displayBlinkingLight("Of Yellow Light", 5, yellow);
}
void displayLight(const char* message, int seconds, int pin) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(message);
for (int i = seconds; i > 0; i--) {
lcd.setCursor(0, 1);
if (i > 1) {
lcd.print(i);
lcd.print(" Seconds");
} else {
lcd.print(i);
lcd.print(" Second");
}
digitalWrite(pin, HIGH);
delay(1000);
}
digitalWrite(pin, LOW);
}
void displayBlinkingLight(const char* message, int seconds, int pin) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(message);
for (int i = seconds; i > 0; i--) {
lcd.setCursor(0, 1);
if (i > 1) {
lcd.print(i);
lcd.print(" Seconds");
} else {
lcd.print(i);
lcd.print(" Second");
}
digitalWrite(pin, HIGH);
delay(500);
digitalWrite(pin, LOW);
delay(500);
}
}
My issue is that when I'm running it, the text is print it on my LCD but the lights are off completely. Do you have any idea what could be the problem with my lights that are not on?