traffic light system

Hello guys, I need some help here. Currently I am trying to develop a traffic light system by using arduino. Here is the situation. If the particular road is facing traffic jam, so I would like to turn on my programme B from the original programme, so that the green light on the particular road will light up for a longer time (in order to let more car to pass through the road). So, in my case, I am using the LDR as the sensor to detect the traffic situation.
Lets say if I set the light_sensitivity = 50, if the LDRValue is less than 50 (the LDR is block by the car), then the programme B will turn on (with green light light up for a longer time).

int LDR = 0 ;
int LDRValue = 0;
int light_sensitivity = 50;

void setup ()
{
  Serial.begin (9600);
  pinMode ( 13, OUTPUT);
  pinMode ( 12, OUTPUT);
  pinMode ( 11, OUTPUT);
  
}

void loop ()
{
  LDRValue = analogRead ( LDR);
  Serial.println ( LDRValue);
  delay (50);
  
  if (LDRValue < light_sensitivity)
   { 
     
   for (int i=0;i <3 ; i++)
   {
 
 digitalWrite(13, HIGH); // Turns LED on pin 13 on 
 delay(5000); // LED on pin 13 remains on for 5 seconds 
 digitalWrite(13, LOW); // Turns LED on pin 13 off 
 
 digitalWrite(12, HIGH); // Turns LED on pin 12 on 
 delay(5000); // LED on pin 12 remains on for 5 seconds 
 digitalWrite(12, LOW); // Turns LED on pin 12 off 

 digitalWrite(11, HIGH); // Turns LED on pin 11 on 
 delay(5000); // LED on pin 11 remains on for 5 seconds 
 digitalWrite(11, LOW); // Turns LED on pin 11 off 

}  }

  else if(LDRValue > light_sensitivity)
  {  

    
 digitalWrite(13, HIGH); // Turns LED on pin 13 on 
 delay(1000); // LED on pin 13 remains on for 5 seconds 
 digitalWrite(13, LOW); // Turns LED on pin 13 off 

 digitalWrite(12, HIGH); // Turns LED on pin 12 on 
 delay(1000); // LED on pin 12 remains on for 5 seconds 
 digitalWrite(12, LOW); // Turns LED on pin 12 off 
 
 digitalWrite(11, HIGH); // Turns LED on pin 11 on 
 delay(1000); // LED on pin 11 remains on for 5 seconds 
 digitalWrite(11, LOW); // Turns LED on pin 11 off 
 
  }}

But now I would like to add more condition to it, if the LDRValue < light_sensitivity, and for more than 30 seconds, then it will only turn on the programme B. (in another word, the LDR sensor is blocked more than 30 seconds, the green light will light up for more longer time). If less than 30 seconds, the program will not turn on the programme B.How should I write for the coding?

Get rid of your delays. See the 'Blink without delay' example in the examples page of this site or in the IDE and also see this post: http://forum.arduino.cc/index.php?topic=223286.0 for methods to avoid using delay(). These will also show you how to adjust your program to take timing into account. Remember that, while your Arduino is in a delay(), it can do NOTHING else.

He just spent the time to tell you and you didn't even try. Make that 2 problems.

If you want some code to work from, here is my code for the pedestrian lights.

You obviously would have to extend it for an intersection situation and then add in your time variations, but it provides a flexible framework for the sequencing and timing.

// Pedestrian crossing lights

const int led13Pin =  13;    // LED pin number
const int PgrnPin =  2;      // Pedestrian
const int PredPin =  3;
const int RgrnPin =  5;      // Road
const int RyelPin =  6;
const int RredPin =  7;
const int button1 =  10;     // Button to ground

int led13State = LOW;        // initialise the LED
int PgrnState = LOW;
int PredState = LOW;
int RgrnState = LOW;
int RyelState = LOW;
int RredState = LOW;
char bstate1 = 0;
boolean press = false;

unsigned long count1 = 0;   // will store last time LED was updated
unsigned long count2 = 0;
unsigned long count3 = 0;
unsigned long count4 = 0;
unsigned long count5 = 0;
unsigned long bcount1 = 0; // button debounce timer.  Replicate as necessary.

// Have we completed the specified interval since last confirmed event?
// "marker" chooses which counter to check 
boolean timeout(unsigned long *marker, unsigned long interval) {
  if (millis() - *marker >= interval) { 
    *marker += interval;    // move on ready for next interval
    return true;       
  } 
  else return false;
}

void setout(unsigned long *marker) {
  *marker = millis();             // initialise
}

// Deal with a button read; true if button pressed and debounced is a new event
// Uses reading of button input, debounce store, state store and debounce interval.
boolean butndown(char button, unsigned long *marker, char *butnstate, unsigned long interval) {
  switch (*butnstate) {               // Odd states if was pressed, >= 2 if debounce in progress
  case 0: // Button up so far, 
    if (button == HIGH) return false; // Nothing happening!
    else { 
      *butnstate = 2;                 // record that is now pressed
      *marker = millis();             // note when was pressed
      return false;                   // and move on
    }

  case 1: // Button down so far, 
    if (button == LOW) return false; // Nothing happening!
    else { 
      *butnstate = 3;                 // record that is now released
      *marker = millis();             // note when was released
      return false;                   // and move on
    }

  case 2: // Button was up, now down.
    if (button == HIGH) {
      *butnstate = 0;                 // no, not debounced; revert the state
      return false;                   // False alarm!
    }
    else { 
      if (millis() - *marker >= interval) {
        *butnstate = 1;               // jackpot!  update the state
        return true;                  // because we have the desired event!
      }
      else 
        return false;                 // not done yet; just move on
    }

  case 3: // Button was down, now up.
    if (button == LOW) {
      *butnstate = 1;                 // no, not debounced; revert the state
      return false;                   // False alarm!
    }
    else { 
      if (millis() - *marker >= interval) {
        *butnstate = 0;               // Debounced; update the state
        return false;                 // but it is not the event we want
      }
      else 
        return false;                 // not done yet; just move on
    }
  default:                            // Error; recover anyway
    {  
      *butnstate = 0;
      return false;                   // Definitely false!
    }
  }
}


void setleds() {
  digitalWrite(led13Pin, led13State);
  digitalWrite(PredPin, PredState);
  digitalWrite(PgrnPin, PgrnState);
  digitalWrite(RredPin, RredState);
  digitalWrite(RyelPin, RyelState);
  digitalWrite(RgrnPin, RgrnState);
}

boolean ispress() { // One-directional read of button - sets but does not clear!
  if (butndown(digitalRead(button1), &bcount1, &bstate1, 10UL )) {
    press = true;
  } 
  return(press);
}

void setup() {
  Serial.begin(9600);
  pinMode(led13Pin, OUTPUT);      
  pinMode(PgrnPin, OUTPUT);      
  pinMode(PredPin, OUTPUT);      
  pinMode(RgrnPin, OUTPUT);      
  pinMode(RyelPin, OUTPUT);      
  pinMode(RredPin, OUTPUT);      
  pinMode(button1, INPUT);      
  digitalWrite(button1,HIGH);        // internal pullup all versions
  press = false;
  Serial.println("Starting ...");
}

void loop() {
  // All red phase
  RredState = HIGH;
  RyelState = LOW; 
  RgrnState = LOW; 
  PredState = HIGH;
  PgrnState = LOW; 
  setleds();
  Serial.println("Red phase");
  setout(&count3);  
  while (!timeout(&count3, 3000UL )) {
    ispress();  // Check on the button
  }

  // Road Green
  RredState = LOW;
  RyelState = LOW; 
  RgrnState = HIGH; 
  PredState = HIGH;
  PgrnState = LOW; 
  setleds();
  Serial.println("Road green");
  setout(&count3);  
  while (!timeout(&count3, 8000UL )) { // Reasonable time on green
    ispress();  // Check on the button
  }
  Serial.println("Green stale, wait on button");

  while ( press == false )  // Now wait for the button 
  {
    if (timeout(&count2, 300UL )) {
      if (led13State == LOW) {
        led13State = HIGH;
      }
      else {
        led13State = LOW; 
      } 
      digitalWrite(led13Pin, led13State);
    }
    ispress();   
  }
  led13State = LOW; 
  digitalWrite(led13Pin, led13State);

  Serial.println("Button sensed");
  setout(&count3);  
  while (!timeout(&count3, 4000UL )) { // Do not respond immediately!
  }

  // Road Yellow
  RredState = LOW;
  RyelState = HIGH; 
  RgrnState = LOW; 
  PredState = HIGH;
  PgrnState = LOW; 
  setleds();
  Serial.println("Road yellow");
  setout(&count3);  
  while (!timeout(&count3, 5000UL )) {
  }

  // Road Red
  RredState = HIGH;
  RyelState = LOW; 
  RgrnState = LOW; 
  PredState = HIGH;
  PgrnState = LOW; 
  setleds();
  Serial.println("Road red");
  setout(&count3);  
  while (!timeout(&count3, 3000UL )) {
  }

  // Walk Green
  RredState = HIGH;
  RyelState = LOW; 
  RgrnState = LOW; 
  PredState = LOW;
  PgrnState = HIGH; 
  setleds();
  press = false;  
  Serial.println("Walk");
  setout(&count3);  
  while (!timeout(&count3, 6000UL )) {
  }

  // Flash Don't Walk
  RredState = HIGH;
  RyelState = LOW; 
  RgrnState = LOW; 
  PgrnState = LOW; 
  Serial.println("Flash Don't Walk");
  setout(&count3);  
  while (!timeout(&count3, 7000UL )) {
    if (timeout(&count2, 500UL )) {
      if (PredState == LOW) {
        PredState = HIGH;
      }
      else {
        PredState = LOW; 
      } 
      setleds();
    }
    ispress();  // Check on the button
  }

}

GoForSmoke: He just spent the time to tell you and you didn't even try. Make that 2 problems.

Was that comment for me or the OP? If it was for the OP, he seems to have removed the post you were replying to, hence my confusion.

The reply to yours that is indeed gone.

Had he even looked down the link that you spent time providing then the LEAST you'd expect are more intelligent questions.

GoForSmoke: The reply to yours that is indeed gone.

That would explain the strangeness.

Just be happy you missed it. It was one of those thanks but that's not the answer replies (I get em too) from someone who glossed what they checked if they bothered to look at all.

I know what it's like to get invested in a code dead end while thinking it isn't. It's even worse when you have somone breathing down your neck who has never written a line of code in their life yet expects to manage development.

GoForSmoke: The reply to yours that is indeed gone.

Had he even looked down the link that you spent time providing then the LEAST you'd expect are more intelligent questions.

Only from an intelligent person.

An intelligent person would have read the link and learned something before answering the post.

GoForSmoke: An intelligent person would have read the link and learned something before answering the post.

That's what I was implying.