Traffic lights using IR proximity sensor

I'm in the process of developing a traffic light system which dynamically changes its duration. The idea is to interface the proximity sensors with the Arduino, place them at a certain distance away from the signal, far enough to determine that enough traffic has been accumulated and needs to be flushed. Once the green signal on one side of the road is ON, the proximity sensor on that lane will be temporarily turned OFF and the other proximity sensor will start reading values till it ascertains the criteria of the area being filled up with enough traffic to flush out. Can anybody help me with the coding part of it?

P.S.: This is only a two-way traffic system, that is, only for two lane.

Wow! A real traffic light system on a public highway! Using an Arduino? I'm impressed! :grinning:

OK, tongue now firmly away from my cheek I'm assuming you're talking about a model set up? In which case your sensors need to be placed down the lane somewhere to detect the presence of 'traffic' backing up (although I feel sorry for the single car at the red light praying for more traffic to come along and let the light go green).

And the 'green' lane will most certainly have to flip to red now and again to avoid the situation I've described above.

Anyway, what hardware have you got to interface to the Arduino? Using SPI or I2C? What traffic light modules have you got and have to managed to at least sequence the lights OK in your code ignoring the sensor bit for now?

Post details (and photos if it helps) of what you've got and we'll see what can be done to assist.

This is the code that I came up with for my problem statement. Please let me know what is erroneous and what can be done to optimise it. Much appreciated. :slight_smile:

int s1 = 0;
int s2 = 1;
int r1 = 2;
int y1 = 3;
int g1 = 4;
int r2 = 5;
int y2 = 6;
int g2 = 7;
int t[5];
bool test = 0;

void setup() {
 pinMode(s1, INPUT);
 pinMode(s2, INPUT);
 pinMode(r1, OUTPUT);
 pinMode(y1, OUTPUT);
 pinMode(g1, OUTPUT);
 pinMode(r2, OUTPUT);
 pinMode(y2, OUTPUT);
 pinMode(g2, OUTPUT);
 analogReference(DEFAULT);
 memset(t,0,5);
 digitalWrite(r1,HIGH);
 digitalWrite(y1,LOW);
 digitalWrite(g1,LOW);
 digitalWrite(r2,LOW);
 digitalWrite(y2,LOW);
 digitalWrite(g2,HIGH);
 
}

void loop() {
 while(analogRead(test)<500)
 {
   for(int i=0;i<5;i++)
   {
     t[i]=digitalRead(test);
     delay(2000);
   }
   int temp=minimum(t);
   for(i=0;i<5;i++)
     t[i]-=temp;
   for(i=0;i<5;i++)
   {
     if(t[i]>=0 && t[i]<=5)
       switchSignal(test);
     else 
     {
       test=!test;
       memset(t,0,5);
       break;  
     }
   }
 }
}
int minimum(int* s)
{
 int temp=s[0];
 for(int i=1;i<5;i++)
 {
   if(temp>s[i])
     temp=s[i];
 }
 return temp;
}
void switchSignal(bool s)
{
 switch(s)
 {
   case 0:
     digitalWrite(r1,LOW);
     digitalWrite(y1,HIGH);
     delay(3000);
     digitalWrite(y1,LOW);
     digitalWrite(g1,HIGH);
     digitalWrite(g2,LOW);
     digitalWrite(r2,HIGH);
     break;

   case 1:
   digitalWrite(r2,LOW);
   digitalWrite(y2,HIGH);
   delay(3000);
   digitalWrite(y2,LOW);
   digitalWrite(g2,HIGH);
   digitalWrite(g1,LOW);
   digitalWrite(r1,HIGH);
   break;

   default:
     break;
 }
}