How to display IR sensor for traffic lights

Thread Starter

AhmedHelmy

Joined Dec 16, 2017
8
I designed a traffic light that works for two roads intersected by arduino , leds and a lcd crystal. But what is required in the project is to determine the traffic density in any way, which is more heavy so I need to put 3 IR sensor for each road to Determine which road more heavy traffic and the red-marked road makes it a green sign and makes the green-marked road make it a red signal .. how i can add to this code ?

C:
// traffic light 1
int red1 = 2;
int yellow1 = 3;
int green1 = 4;
// traffic light 2
int red2 = 5;
int yellow2 = 6;
int green2 = 7;

int i;

#include <Wire.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27,16,2); // set the LCD address to 0x27 for a 16 chars and 2 line display


void setup() {
Serial.begin(9600);
lcd.init();
lcd.backlight();
pinMode(red1,OUTPUT);
pinMode(yellow1,OUTPUT);
pinMode(green1,OUTPUT);
pinMode(red2,OUTPUT);
pinMode(yellow2,OUTPUT);
pinMode(green2,OUTPUT);
lcd.setCursor(0,0);
lcd.print("T1=");
lcd.setCursor(0,1);
lcd.print("T2=");
}
int c1=10,j=0;
int c2=10,k=0;
void loop() {
for (i=1;i<=190;i++){
delay(100);
if (i == 1){ // first of 1 sec
digitalWrite(green1,1);digitalWrite(red1,0);digitalWrite(yellow1,0);
lcd.setCursor(13,0);
lcd.print(" G");//green light
digitalWrite(red2,1);digitalWrite(yellow2,0);digitalWrite(green2,0);
lcd.setCursor(13,1);
lcd.print("R ");//red light
}

if (i == 50){ //at 5 sec
digitalWrite(yellow1,1);
lcd.setCursor(14,0);
lcd.print("Y");//yellow light
digitalWrite(yellow2,1);
lcd.setCursor(14,1);
lcd.print("Y");//yellow light
}

if (i == 100){ // at 10 sec
digitalWrite(green1,0);digitalWrite(red1,1);digitalWrite(yellow1,0);
lcd.setCursor(13,0);
lcd.print("R ");//red light
digitalWrite(red2,0); digitalWrite(yellow2,0);digitalWrite(green2,1);
lcd.setCursor(13,1);
lcd.print(" G");//red light
}

if (i == 140){ //at 15 sec
digitalWrite(yellow1,1);
lcd.setCursor(14,0);
lcd.print("Y");//yellow light
digitalWrite(yellow2,1);
lcd.setCursor(14,1);
lcd.print("Y");//yellow light
}

// at 20 sec
j++;
if(j == 10 && i != 190){
c1--;j=1;
lcd.setCursor(3,0);
lcd.print(" ");
lcd.setCursor(3,0);
lcd.print(c1);
if (c1==0){c1=10;}
}

k++;
if(k == 10 && i != 190){
c2--;k=1;
lcd.setCursor(3,1);
lcd.print(" ");
lcd.setCursor(3,1);
lcd.print(c2);
if (c2==0){c2=10;}
}
}
c1=10;j=0;
c2=10;k=0;
}
Mod edit: moved code to post with code tags

 

Attachments

Last edited by a moderator:
Top