0% found this document useful (0 votes)
66 views

Traffic Density Code

The document describes an Arduino program that monitors traffic density on 4 roads using sensors and controls a traffic light accordingly. It takes sensor readings every second, calculates the maximum traffic density and corresponding road, and adjusts the traffic light patterns to prioritize the busier road for 3 seconds then the other road for 5 seconds. If no road is particularly busy, it runs the light in normal alternating fashion.

Uploaded by

Sushmita Singh
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
66 views

Traffic Density Code

The document describes an Arduino program that monitors traffic density on 4 roads using sensors and controls a traffic light accordingly. It takes sensor readings every second, calculates the maximum traffic density and corresponding road, and adjusts the traffic light patterns to prioritize the busier road for 3 seconds then the other road for 5 seconds. If no road is particularly busy, it runs the light in normal alternating fashion.

Uploaded by

Sushmita Singh
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

TRAFFIC DENSITY CODE

int i=0;
int j=0;
void setup() {
// put your setup code here, to run once:
pinMode(data, OUTPUT);
pinMode(clock, OUTPUT);
pinMode(latch, OUTPUT);
Serial.begin(9600);
int delayTime = 1000;
updateLEDs(0);
updateLEDs(2);
// delay(delayTime);
Serial.println("traffic light");
}

void loop() {
// put your main code here, to run repeatedly:
int road_density_count[4] ={0,0,0,0};
int max_rush=0;
int max_rush_road =0;

for(int i=0;i<4;i++){
for(int j=0;j<3;j++){
// Serial.println("=");
if(digitalRead(road[i][j]) ==HIGH){
road_density_count[i] = road_density_count[i]+road_density[i][j];
Serial.print(" road_density_count[");
Serial.print(i);
Serial.print("]:");
Serial.println(road_density_count[i]);
delay(1000);
}
}

}
for(i=0;i<4;i++){
Serial.println(road_density_count[i]);
if(road_density_count[i]==1||road_density_count[i]==4||
road_density_count[i]==9){

}
else{
road_density_count[i]=0;
}
}

for(i=0;i<4;i++){
if(road_density_count[0]<road_density_count[i]){
road_density_count[0]=road_density_count[i];
max_rush = road_density_count[0];
max_rush_road =i;
Serial.print("max_rush_road: ");
Serial.println(max_rush_road);
delay(1000);

}
}
Serial.print("max_rush_road: ");
Serial.println(max_rush_road);
if(max_rush!=0){
Serial.print("rush_road: ");
Serial.print(max_rush_road);
Serial.print(" Max. Rush: ");
Serial.println(max_rush);
}

if(max_rush!=0&&max_rush_road == 1 || max_rush_road == 3){


Serial.println("(max_rush_road == 1 ||max_rush_road == 3)");
updateLEDs(5);
updateLEDs(20);
delay(3000);
//clear1:
updateLEDs(3);
updateLEDs(12);
delay(5000);
// if(digitalRead(road[1][0]) ==LOW || digitalRead(road[1][1]) ==LOW ||
digitalRead(road[1][2]) ==LOW )
// { goto clear1;
// }
// if( digitalRead(road[3][0]) ==LOW || digitalRead(road[3][1]) ==LOW ||
digitalRead(road[3][2]) ==LOW)
// {goto clear1;
// }
}

else if(max_rush!=0&&max_rush_road == 0 ||max_rush_road == 2){


Serial.println("(max_rush_road == 0 ||max_rush_road == 2)");
updateLEDs(8);
updateLEDs(162);
delay(3000);
// clear2:
updateLEDs(8);
updateLEDs(97);
delay(5000);
// if(digitalRead(road[0][0]) ==LOW || digitalRead(road[0][1]) ==LOW ||
digitalRead(road[0][2]) ==LOW )
// { goto clear2;
// }
// if( digitalRead(road[2][0]) ==LOW || digitalRead(road[2][1]) ==LOW ||
digitalRead(road[2][2]) ==LOW){
// goto clear2;
// }
}
else { // display normaly
Serial.println("TRAFFIC LIGHT NORMAL MODE");
updateLEDs(5);
updateLEDs(20);
Serial.println("1");
delay(3000);
updateLEDs(3);
updateLEDs(12);
Serial.println("2");
delay(5000);
updateLEDs(8);
updateLEDs(162);
Serial.println("3");
delay(3000);
updateLEDs(8);
updateLEDs(97);
Serial.println("4");
delay(5000);
}
}

void updateLEDs(int value){


digitalWrite(latch, LOW); //Pulls the chips latch low
shiftOut(data, clock, MSBFIRST, value); //Shifts out the 8 bits to the shift
register
digitalWrite(latch, HIGH); //Pulls the latch high displaying the data
}

1. We transfer the program from the compiler to the memory of the micro-controller.

You might also like