#include <LiquidCrystal_I2C.h>
int Red_ledA = 2;
int Green_ledA = 3;
int Yellow_ledA = 4;
int Red_ledB = 5;
int Green_ledB = 6;
int Yellow_ledB = 7;
int Red_ledC = 8;
int Green_ledC = 9;
int Yellow_ledC = 10;
int White_led = 11;
int LDR = A6;
int LDR_reading;
int lcdColumns = 16;
int lcdRows = 2;
int count = 29;
int S1_L1 = 12;
int S2_L1 = 13;
int S1_L2 = A0;
int S2_L2 = A1;
int S1_L3 = A2;
int S2_L3 = A3;
int green_count_L1 = 5;
int yellow_count_L1 = 3;
int red_count_L1 = 5;
int green_count_L2 = 5;
int yellow_count_L2 = 3;
int red_count_L2 = 5;
int green_count_L3 = 5;
int yellow_count_L3 = 3;
int red_count_L3 = 5;
int activate = 0;
int normal;
LiquidCrystal_I2C lcd(0x27, lcdColumns, lcdRows);
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode (Red_ledA, OUTPUT);
pinMode (Green_ledA, OUTPUT);
pinMode (Yellow_ledA, OUTPUT);
pinMode (Red_ledB, OUTPUT);
pinMode (Green_ledB, OUTPUT);
pinMode (Yellow_ledB, OUTPUT);
pinMode (Red_ledC, OUTPUT);
pinMode (Green_ledC, OUTPUT);
pinMode (Yellow_ledC, OUTPUT);
pinMode(LDR, INPUT);
pinMode(White_led, OUTPUT);
lcd.init();
lcd.setCursor(0, 0);
lcd.print(" SYSTEM ");
lcd.setCursor(0, 1);
lcd.print("INITIALIIZING...");
delay(2000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" DENSITY BASED ");
lcd.setCursor(0, 1);
lcd.print("TRAFFIC CONTROL ");
delay(1000);
lcd.clear();
}
void loop() {
LDR_reading = analogRead(LDR);
Serial.println(LDR_reading);
if (LDR_reading < 250) {
digitalWrite(White_led, HIGH);
}
else if (LDR_reading > 250) {
digitalWrite(White_led, LOW);
}
if ((digitalRead(S1_L1) == HIGH) && (digitalRead(S2_L1) == HIGH)) {
green_count_L1 = 10;
activate = 1;
while (activate == 1) {
L1_L2();
if (green_count_L1 == 0) {
normal = 2;
activate = 0;
}
}
}
else if ((digitalRead(S1_L2) == HIGH) && (digitalRead(S2_L2) == HIGH)) {
green_count_L2 = 10;
activate = 2;
while (activate == 2) {
L2_L3();
if (green_count_L2 == 0) {
normal = 3;
activate = 0;
}
}
}
else if ((digitalRead(S1_L3) == HIGH) && (digitalRead(S2_L3) == HIGH)) {
green_count_L3 = 10;
activate = 3;
while (activate == 3) {
L3_L1();
if (green_count_L2 == 0) {
normal = 1;
activate = 0;
}
}
}
else {
normal = 1;
while (normal == 1) {
L1_L2();
if (green_count_L1 == 0) {
normal = 2;
while (normal == 2) {
L2_L3();
if (green_count_L2 == 0) {
normal = 3;
while (normal == 3) {
L3_L1();
if (green_count_L3 == 0) {
normal = 1;
}
}
}
}
}
green_count_L1 = 5;
green_count_L2 = 5;
green_count_L3 = 5;
yellow_count_L1 = 3;
yellow_count_L2 = 3;
yellow_count_L3 = 3;
}
}
if (activate == 1){
green = 10;
normal = 2;
}
else if (activate == 2){
green = 10;
normal = 3;
}
else if (activate == 3){
green = 10;
normal = 1;
}
if (normal == 1){
while (normal == 1){
green --;
delay (1000);
if (green == 0){
normal = 2;
green = 5;
}
}
}
else if (normal == 2){
while (normal == 2){
green--;
delay(1000);
if (greeen == 0){
normal = 3;
green = 5;
}
}
}
else if (normal == 3){
while (normal == 3){
green--;
delay(1000);
if (green == 0){
normal = 1;
green = 5;
}
}
}
}
void L1_L2() {
green_count_L1--;
yellow_count_L1--;
if (green_count_L1 <= 10 && green_count_L1 >= 0) { //green_light_L1 on for 5 or 10 secs
digitalWrite (Red_ledA, LOW);
digitalWrite (Green_ledA, HIGH);
digitalWrite (Yellow_ledA, LOW);
lcd.setCursor(2, 1);
lcd.print(count - 24);
lcd.setCursor(1, 0);
lcd.print("L1");
lcd.setCursor(0, 1);
lcd.print("G:");
digitalWrite (Red_ledB, HIGH);
digitalWrite (Green_ledB, LOW);
digitalWrite (Yellow_ledB, LOW);
lcd.setCursor(7, 1);
lcd.print(count - 24);
lcd.setCursor(6, 0);
lcd.print("L2");
lcd.setCursor(5, 1);
lcd.print("R:");
digitalWrite (Red_ledC, HIGH);
digitalWrite (Green_ledC, LOW);
digitalWrite (Yellow_ledC, LOW);
lcd.setCursor(12, 1);
lcd.print(count - 24);
lcd.setCursor(11, 0);
lcd.print("L3");
lcd.setCursor(10, 1);
lcd.print("R:");
}
else if (yellow_count_L2 <= 3 && yellow_count_L2 >= 0) { //yellow light of lane 2
digitalWrite (Red_ledA, LOW);
digitalWrite (Green_ledA, HIGH);
digitalWrite (Yellow_ledA, LOW);
lcd.setCursor(2, 1);
lcd.print(count - 21);
lcd.setCursor(1, 0);
lcd.print("L1");
lcd.setCursor(0, 1);
lcd.print("G:");
digitalWrite (Red_ledB, LOW);
digitalWrite (Green_ledB, LOW);
digitalWrite (Yellow_ledB, HIGH);
lcd.setCursor(7, 1);
lcd.print(count - 21);
lcd.setCursor(6, 0);
lcd.print("L2");
lcd.setCursor(5, 1);
lcd.print("Y:");
digitalWrite (Red_ledC, HIGH);
digitalWrite (Green_ledC, LOW);
digitalWrite (Yellow_ledC, LOW);
lcd.setCursor(12, 1);
lcd.print(count - 21);
lcd.setCursor(11, 0);
lcd.print("L3");
lcd.setCursor(10, 1);
lcd.print("R:");
}
delay(1000);
}
void L2_L3() {
green_count_L2--;
yellow_count_L3--;
if (green_count_L2 <= 10 && green_count_L2 >= 0) { //green_light_L2 on for 5 or 10 secs
digitalWrite (Red_ledA, HIGH);
digitalWrite (Green_ledA, LOW);
digitalWrite (Yellow_ledA, LOW);
lcd.setCursor(2, 1);
lcd.print(count - 16);
lcd.setCursor(1, 0);
lcd.print("L1");
lcd.setCursor(0, 1);
lcd.print("R:");
digitalWrite (Red_ledB, LOW);
digitalWrite (Green_ledB, HIGH);
digitalWrite (Yellow_ledB, LOW);
lcd.setCursor(7, 1);
lcd.print(count - 16);
lcd.setCursor(6, 0);
lcd.print("L2");
lcd.setCursor(5, 1);
lcd.print("G");
digitalWrite (Red_ledC, HIGH);
digitalWrite (Green_ledC, LOW);
digitalWrite (Yellow_ledC, LOW);
lcd.setCursor(12, 1);
lcd.print(count - 16);
lcd.setCursor(11, 0);
lcd.print("L3");
lcd.setCursor(10, 1);
lcd.print("R:");
}
else if (yellow_count_L3 <= 3 && yellow_count_L3 >= 0) { //yellow light of lane 3
digitalWrite (Red_ledA, HIGH);
digitalWrite (Green_ledA, LOW);
digitalWrite (Yellow_ledA, LOW);
lcd.setCursor(2, 1);
lcd.print(count - 13);
lcd.setCursor(1, 0);
lcd.print("L1");
lcd.setCursor(0, 1);
lcd.print("R:");
digitalWrite (Red_ledB, LOW);
digitalWrite (Green_ledB, HIGH);
digitalWrite (Yellow_ledB, LOW);
lcd.setCursor(7, 1);
lcd.print(count - 13);
lcd.setCursor(6, 0);
lcd.print("L2");
lcd.setCursor(5, 1);
lcd.print("G:");
digitalWrite (Red_ledC, LOW);
digitalWrite (Green_ledC, LOW);
digitalWrite (Yellow_ledC, HIGH);
lcd.setCursor(12, 1);
lcd.print(count - 13);
lcd.setCursor(11, 0);
lcd.print("L3");
lcd.setCursor(10, 1);
lcd.print("Y:");
}
delay(1000);
}
void L3_L1() {
green_count_L3--;
yellow_count_L1--;
if (green_count_L3 <= 10 && green_count_L3 >= 0) { //green_light_L3 on for 5 or 10 secs
digitalWrite (Red_ledA, HIGH);
digitalWrite (Green_ledA, LOW);
digitalWrite (Yellow_ledA, LOW);
lcd.setCursor(2, 1);
lcd.print(count - 8);
lcd.setCursor(1, 0);
lcd.print("L1");
lcd.setCursor(0, 1);
lcd.print("R:");
digitalWrite (Red_ledB, HIGH);
digitalWrite (Green_ledB, LOW);
digitalWrite (Yellow_ledB, LOW);
lcd.setCursor(7, 1);
lcd.print(count - 8);
lcd.setCursor(6, 0);
lcd.print("L2");
lcd.setCursor(5, 1);
lcd.print("R:");
digitalWrite (Red_ledC, LOW);
digitalWrite (Green_ledC, HIGH);
digitalWrite (Yellow_ledC, LOW);
lcd.setCursor(12, 1);
lcd.print(count - 8);
lcd.setCursor(11, 0);
lcd.print("L3");
lcd.setCursor(10, 1);
lcd.print("G:");
}
else if (yellow_count_L1 <= 3 && yellow_count_L1 >= 0) { //yellow light of lane 1
digitalWrite (Red_ledA, LOW);
digitalWrite (Green_ledA, LOW);
digitalWrite (Yellow_ledA, HIGH);
lcd.setCursor(2, 1);
lcd.print(count - 5);
lcd.setCursor(1, 0);
lcd.print("L1");
lcd.setCursor(0, 1);
lcd.print("Y:");
digitalWrite (Red_ledB, HIGH);
digitalWrite (Green_ledB, LOW);
digitalWrite (Yellow_ledB, LOW);
lcd.setCursor(7, 1);
lcd.print(count - 5);
lcd.setCursor(6, 0);
lcd.print("L2");
lcd.setCursor(5, 1);
lcd.print("R:");
digitalWrite (Red_ledC, LOW);
digitalWrite (Green_ledC, HIGH);
digitalWrite (Yellow_ledC, LOW);
lcd.setCursor(12, 1);
lcd.print(count - 5);
lcd.setCursor(11, 0);
lcd.print("L3");
lcd.setCursor(10, 1);
lcd.print("G:");
}
delay(1000);
}