Code for traffic lights with light sensor…
Red on for 4s, Green on for 4 s, Yellow on for 2 s
if light covered when red light on, it will switch to green immediately…

int ledPinR=9;		//redLED controlled by pin 9
int ledPinY=10;		//yellowLED controlled by pin 10
int ledPinG=11; 		//greenLED controlled by pin 11
int lightamount = 0;
int photocell = 2;

void setup (){
  pinMode (ledPinR, OUTPUT);
  pinMode (ledPinY, OUTPUT);
  pinMode (ledPinG, OUTPUT);
  Serial.begin(9600);
}
 
void loop(){  
digitalWrite(ledPinG, HIGH);   //just green on
delay(4000);
 
digitalWrite(ledPinY, HIGH);  //just yellow on
digitalWrite(ledPinG, LOW);  
delay(2000);    
 
digitalWrite(ledPinR, HIGH);  //just red one
digitalWrite(ledPinY, LOW);
lightamount = analogRead(photocell);  //read light amount
if (lightamount>200)          //if sensor not covered, delay for 0.5 s
{delay(500);
lightamount = analogRead(photocell);  //read light amount
if (lightamount>200)          //if sensor not covered, delay for 0.5 s
{delay(500);
lightamount = analogRead(photocell);  //read light amount
if (lightamount>200)          //if sensor not covered, delay for 0.5 s
{delay(500);
lightamount = analogRead(photocell);  //read light amount
if (lightamount>200)          //if sensor not covered, delay for 0.5 s
{delay(500);
lightamount = analogRead(photocell);  //read light amount
if (lightamount>200)          //if sensor not covered, delay for 0.5 s
{delay(500);
lightamount = analogRead(photocell);  //read light amount
if (lightamount>200)          //if sensor not covered, delay for 0.5 s
{delay(500);
lightamount = analogRead(photocell);  //read light amount
if (lightamount>200)          //if sensor not covered, delay for 0.5 s
{delay(500);
lightamount = analogRead(photocell);  //read light amount
if (lightamount>200)          //if sensor not covered, delay for 0.5 s
{delay(500);
 
}}}}}}}}
digitalWrite(ledPinR, LOW);  
 
}