#define Ldr A5 // Defining that LDR is connected on Analog Input Pin A5 and will be called as Ldr. boolean stateIR[]={ 0,0,0,0,0}; // Creating an Array Called stateIR in which current state of all the IR Sensors will be stored. boolean crossed[]={0,0,0,0,0}; // Creating an array Called Crossed in which if the person has crossed some particular Ir, then corresponding array element will be true. boolean last_stateIR[]= {0,0,0,0,0}; // Creating an Array Called last_stateIR in which last state of all the IR Sensors will be stored. int ir_pin[]={2,3,4,5,6}; // Creating an Array in which the Digital pin no to which IR sensors are connected is stored. int led_pin[]={7,8,9,10,11}; // Creating an Array in which the Digital pin no to which LED's are connected is stored. unsigned long timer; // Defining Variable for timer. This timer In will get reset once someone crosses the IR sensor. int LDR; // Creating a variable called LDR in which the value of LDR is stored. void setup() { timer=millis(); // millis() is the real time clock. It starts as soon as the Arduino is powered up. Here the timer is made equal to real time. Serial.begin(9600); // Initiating Serial Monitor for Debugging Purpose for(int i=0; i<=4; i++) // Defining all the IR Pins as INPUT by using for loop. ir_pin[0]=2 ; ir_pin[1]=3 ; ir_pin[2]=4 ; ir_pin[3]=5 ; ir_pin[4]=6 { pinMode(ir_pin[i],INPUT); // Defining that the pin on which IR is connected is in INPUT Mode } for(int i=0; i<=4; i++) // Defining all the LED Pins as OUTPUT by using for loop. led_pin[0]=7 ; led_pin[1]=8 ; led_pin[2]=9 ; led_pin[3]=10 ; led_pin[4]=11 { pinMode(led_pin[i],OUTPUT); // Defining that the pin on which LED is connected is in OUTPUT Mode } for(int i=0; i<=4; i++) // Turning OFF All the LED's using for loop. { digitalWrite(led_pin[i], LOW); // Turning OFF all the LED's by providing it LOW Value. } } void loop() { LDR=analogRead(Ldr); // Reading Value from LDR and storing it in variable LDR. while(LDR<=500) // If the value of LDR is less than 500 i.e. there is not enough ambient light { LDR=analogRead(Ldr); // Reading Value from LDR and storing it in variable LDR. for(int i=0; i<=4; i++) // Checking the current value of all sensors one by one using for loop { stateIR[i]=digitalRead(ir_pin[i]); // Checking the current value of all IR Sensors and storing it in StateIR array variable } for(int i=0; i<=4; i++) // Resetting Value of Crossed for all IR Sensors using for loop { crossed[i]=false; // Crossed Variable is reset by providing false value. } for(int i=0; i<=4; i++) // Checking if Car has Crossed one by one for all Sensors using for loop { if(stateIR[i] != last_stateIR[i]) // Checking if current value of Inside IR sensor is equal to the last IR Value. { if(!stateIR[i]) // If the current state of IR Sensor is false i.e. no body is there, that means someone has crossed. { crossed[i]=true; // Turning the crossed value of the sensor from which car has crossed to true. Serial.print("Crossed"); // Printing on Serial Monitor for debugging Serial.println(ir_pin[i]); } else { crossed[i]=false; } delay(50); } last_stateIR[i]=stateIR[i]; // Making the last state of IR equal to current state so that in next loop, it becomes last state } for(int i=0; i<=4; i++) // Performing the task if car has crossed any sensor { if(crossed[i]==true) // Checking if crossed state is true for any of the sensor { // If the state is true, then digitalWrite(led_pin[i],HIGH); // Turning on the next Light digitalWrite(led_pin[i-1],HIGH);// Turning On the lights at which car is there digitalWrite(led_pin[i+1],HIGH);// Turning on Next to next lights so that lights are On for next 500m timer=millis(); // Resetting the timer so that later we can check the timer since last car was crossed } } for(int i=0; i<=6; i++) // Checking if it has been 10 seconds and no one has crossed the next sensor { if(millis()-timer>=5000) // if the difference between timer and real time is greater than 5000, that means it has been 5000 milli seconds { digitalWrite(led_pin[i-2],LOW); // Turning off the previous Light if it has been 5 seconds } if(millis()-timer>=10000) // If it has been 10 seconds since last car crossed, then { digitalWrite(led_pin[i],LOW); // turning off all the Lights that are ON because of that particular IR sensor digitalWrite(led_pin[i+1],LOW); digitalWrite(led_pin[i-1],LOW); } } LDR=analogRead(Ldr); // Again checking the Value of LDR if(LDR>500) // If value of LDR Goes abovr 500, then break the loop and turn off all the Lights { break; // Break the loop } } // End of while condition for(int i=0; i<=4; i++) ` // Turning off all the Lights { digitalWrite(led_pin[i], LOW); } }