Here is the whole thing :-
#include <LiquidCrystal.h>
int rs=7;
int en=8;
int d4=9;
int d5=10;
int d6=11;
int d7=12;
LiquidCrystal lcd(rs,en,d4,d5,d6,d7);
int lightPen=A0;
int lightVal;
int dv=2000;
int lival=450;
int j;
int lapCount;
int greenPin=2; //slow in zone B
int redPin=3; // Zone A stop
int bluePin=4; //Zone A slow
int greePin=5; //Zone A fast
int orangePin=6; //Zone B slow
int grePin=13; //Zone B fast
int dvl=500;
int dvs=2000;
int StStop=30000;
void setup() {
Serial.begin(9600);
lcd.begin(16,2);
lapCount=0;
pinMode(lightPen,INPUT);
pinMode(redPin,OUTPUT);
pinMode(greenPin,OUTPUT);
pinMode(greePin,OUTPUT);
pinMode(grePin,OUTPUT);
pinMode(bluePin,OUTPUT);
pinMode(orangePin,OUTPUT);
}
void loop() {
lcd.setCursor(0,0);
lcd.print("Lap Count = ");
lcd.print(lapCount);
lightVal=analogRead(lightPen);
lcd.setCursor(0,2);
lcd.print("lightVal = ");
lcd.println(lightVal);
Serial.print("Lightval = ");
Serial.println(lightVal);
delay(dv);
if(lightVal<lival){
digitalWrite(orangePin,LOW); // ISO 1 & 3 closed - conducting current green
digitalWrite(redPin,LOW); // controller to fast in zone B green
digitalWrite(bluePin,LOW); // ISO 2 closed (current flow) green
lapCount=lapCount+1;
Serial.print("lapCount = ");
Serial.println(lapCount);
delay(dv);
if(lapCount>3){
Serial.println(" laps>3");
delay(dv);
digitalWrite(orangePin,HIGH); //Zone B slow, ISO 1 & 3 open (isolated) red
digitalWrite(redPin,HIGH); // Zone B slow - feed on slow red
digitalWrite(bluePin,HIGH); //Zone A stopped ISO 2 open (isolated) red
Serial.println("train stopped at station");
delay(dv);
delay(StStop); //station stop
digitalWrite(bluePin,LOW); //Zone A slow ISO 2 closed (current flow) green
lapCount=0;
delay(dv);
}
Serial.println("Moving out of station");
delay(dv);
}
Serial.println("Train cycling");
delay(dv);
}