//by Davood Hakemi @electrohakemi with ❤
//Arduino library:
#include <Arduino.h>
//LCD I2C library:
#include <LiquidCrystal_I2C.h>
#define LED_RED_PIN 2
#define LED_YELLOW_PIN 3
#define LED_GREEN_PIN 4
enum e_traffic_light_state
{
OFF,
RED,
YELLOW,
GREEN
};
void TrafficLight( e_traffic_light_state state, uint32_t wait_ms, const char* msg );
//LCD I2C address 0x27, 16 column and 2 rows!
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup(){
//Initialize the LCD and the LEDs:
lcd.init();
lcd.backlight();
pinMode(LED_RED_PIN , OUTPUT);
pinMode(LED_YELLOW_PIN, OUTPUT);
pinMode(LED_GREEN_PIN , OUTPUT);
}
void loop(){
//Red LED
TrafficLight(RED, 5000, " STOP!");
//Yellow LED
TrafficLight(YELLOW, 1000, " Prepare to go!");
//Green LED
TrafficLight(GREEN, 3000, " GO!");
//Yellow LED
TrafficLight(YELLOW, 1000, "Prepare to stop!");
}
void TrafficLight( e_traffic_light_state state, uint32_t wait_ms, const char* msg )
{
// turn off all lights
digitalWrite(LED_RED_PIN , LOW);
digitalWrite(LED_YELLOW_PIN, LOW);
digitalWrite(LED_GREEN_PIN , LOW);
// decide what led to turn on
switch(state)
{
case RED:
digitalWrite(LED_RED_PIN, HIGH);
break;
case YELLOW:
digitalWrite(LED_YELLOW_PIN, HIGH);
break;
case GREEN:
digitalWrite(LED_GREEN_PIN, HIGH);
break;
default:
break;
}
if( msg != NULL )
{
// clear lcd
lcd.clear();
// reset lcd cursor
lcd.setCursor(0, 0);
// show message
lcd.print(msg);
// reset lcd cursor
lcd.setCursor(0, 1);
// show electorhakem signature
lcd.print(" @ElectroHakemi");
}
// delay at the state
delay(wait_ms);
}