 void setInitialTrafficCondition() {
   //initial stage
   //NS green on, red and amber off
-  if (!isNSTrafficGreen) {
-    digitalWrite(trafficNSLights[0], HIGH);
-    digitalWrite(trafficEWLights[2], HIGH);
-    delay(greenLightDelay);
-    isNSTrafficGreen = true;
-  }
-}
-
-/**
-   Passes vehicles
-*/
-void passVehicles() {
-  //Pass any blocked vehicles in NS traffic
-  if (isNSTrafficGreen) {
-    //when NS traffic cycle change traffic to north south
-    if (vehicleNSCounter > 0) {
-      Serial.print("Total NORTH-SOUTH vehicles. ");
-      Serial.println(vehicleNSCounter);
-      vehicleNSCounter = vehicleNSCounter - 1;
-      Serial.print("Vehicle NORTH-SOUTH passing. Total Number remainning");
-      Serial.println(vehicleNSCounter);
-    }
+  if (traffic == NS_GREEN) {
+    setEWTrafficLights(LOW, LOW, HIGH);
+    setNSTrafficLights(HIGH, LOW, LOW);
+  }
+}
+
+void changeEWGreenToAmber() {
+  if (timeSinceStart - previousTimeSinceStart > minimumGreenLight) {
+    Serial.println("Vehicle EAST-WEST direction. Changing signal EAST-WEST to green");
+
+    //change NS to amber
+    setNSTrafficLights(LOW, HIGH, LOW);
+
+    traffic = NS_AMBER;
+    previousTimeSinceStart = timeSinceStart;
+  }
+  else {
+    //if waiting for green light
+    Serial.print("Time left for changing EAST WEST to green in : ");
+    Serial.print((long(minimumGreenLight) - (timeSinceStart - previousTimeSinceStart)) / long(1000));
+    Serial.println("s");
+  }
+}
+
+void changeNSGreenToAmber() {
+  if (timeSinceStart - previousTimeSinceStart > minimumGreenLight) {
+    Serial.println("Vehicle NORTH-SOUTH direction. Changing signal NORTH-SOUTH to green");
+
+    //change EW to amber
+    setEWTrafficLights(LOW, HIGH, LOW);
+    traffic = EW_AMBER;
+    previousTimeSinceStart = timeSinceStart;
   } else {
-    //when NS traffic cycle change traffic to north south
-    if (vehicleEWCounter > 0) {
-      Serial.print("Total EAST-WEST vehicles. ");
-      Serial.println(vehicleEWCounter);
-      vehicleEWCounter = vehicleEWCounter - 1;
-      Serial.print("Vehicle EAST-WEST passing. Total Number remainning");
-      Serial.println(vehicleEWCounter);
-    }
-  }
-}
-
-void changeEWTrafficToGreen() {
-  Serial.println("Vechile EAST-WEST direction. Changing signal EAST-WEST to green");
-  //Change NS to red
-  setNSTrafficLights(LOW, HIGH, LOW);
-  delay(amberLightDelay);
-  setNSTrafficLights(LOW, LOW, HIGH);
-  //Change EW to green
-  delay(redLightDelay);
-  setEWTrafficLights(HIGH, LOW, LOW);
-  isNSTrafficGreen = false;
-}
-
-void changeNSTrafficToGreen() {
-  Serial.println("Vechile NORTH-SOUTH direction. Changing signal NORTH-SOUTH to green");
-  //Change EW to red
-  setEWTrafficLights(LOW, HIGH, LOW);
-  delay(amberLightDelay);
-  setEWTrafficLights(LOW, LOW, HIGH);
-  //Change NS to green
-  delay(redLightDelay);
-  setNSTrafficLights(HIGH, LOW, LOW);
-  isNSTrafficGreen = true;
+    //if waiting for green light
+    Serial.print("Time left for changing NORTH SOUTH to green in : ");
+    Serial.print((long(minimumGreenLight) - (timeSinceStart - previousTimeSinceStart)) / long(1000));
+    Serial.println("s");
+  }
+}
+
+void changeNSAmberToRed() {
+  if (timeSinceStart - previousTimeSinceStart > amberLightDelay) {
+    //Set NS to red
+    setNSTrafficLights(LOW, LOW, HIGH);
+    traffic = NS_RED;
+    previousTimeSinceStart = timeSinceStart;
+  }
+}
+
+void changeNSRedToEWGreen() {
+  if (timeSinceStart - previousTimeSinceStart > redLightDelay) {
+    //Set EW to green
+    setEWTrafficLights(HIGH, LOW, LOW);
+    timeWhenGreenSignal = timeSinceStart;
+    Serial.println("EAST-WEST direction signal green");
+    traffic = EW_GREEN;
+    previousTimeSinceStart = timeSinceStart;
+  }
+}
+
+void changeEWAmberToRed() {
+  if (timeSinceStart - previousTimeSinceStart > amberLightDelay) {
+    //Set EW to red
+    setEWTrafficLights(LOW, LOW, HIGH);
+    traffic = EW_RED;
+    previousTimeSinceStart = timeSinceStart;
+  }
+}
+
+void changeEWRedToNSGreen() {
+  if (timeSinceStart - previousTimeSinceStart > redLightDelay) {
+    //Set NS to green
+    setNSTrafficLights(HIGH, LOW, LOW);
+    timeWhenGreenSignal = timeSinceStart;
+    Serial.println("NORTH-SOUTH direction signal green");
+    traffic = NS_GREEN;
+    previousTimeSinceStart = timeSinceStart;
+  }
 }
 
 void setNSTrafficLights(uint8_t  greenValue, uint8_t amberValue, uint8_t redValue) {

const int vehicleNS = 13;

// Light delay
int delayTime = 1000; //1sec

// Light delays
int amberLightDelay = 2000; //2sec
int greenLightDelay = 5000; //20sec for now 5 sec
int redLightDelay = 3000; //3 sec

//boolean to control state
boolean isNSTrafficGreen = false;

int vehicleNSCounter = 0;   // counter for the number of button presses
int vehicleNSState = 0;        // current state of the button
int lastVehicleNSState = 0;    // previous state of the button

int vehicleEWCounter = 0;   // counter for the number of button presses
int vehicleEWState = 0;        // current state of the button
int lastVehicleEWState = 0;    // previous state of the button

//FUNCTIONS


  lastVehicleEWState = vehicleEWState;
}

/**
   Passes vehicles
*/
void passVehicles() {
  //pass vehicles only after minimum vehicle time
  if (timeSinceStart - previousVehicleTime > vehicleTime) {
    //Pass any blocked vehicles in NS traffic
    if (traffic == NS_GREEN) {
      //when NS traffic cycle change traffic to north south
      if (vehicleNSCounter > 0 && (timeSinceStart - timeWhenGreenSignal > vehicleTime)) {
        vehicleNSCounter = vehicleNSCounter - 1;
        Serial.print("Vehicle NORTH-SOUTH passing. Total vehicles remainning: ");
        Serial.println(vehicleNSCounter);
        previousVehicleTime = timeSinceStart;
      }
    }

    if (traffic == EW_GREEN) {
      //when NS traffic cycle change traffic to north south
      if (vehicleEWCounter > 0 && (timeSinceStart - timeWhenGreenSignal > vehicleTime)) {
        vehicleEWCounter = vehicleEWCounter - 1;
        Serial.print("Vehicle EAST-WEST passing. Total vehicles remainning: ");
        Serial.println(vehicleEWCounter);
        previousVehicleTime = timeSinceStart;
      }
    }
  }
}

//creates a couple timers that will fire repeatedly every x ms
TimedAction vehiclesNSThread = TimedAction(50, countNSVehicles); // checks for NS vehicle every 50 sec and count the total vehicle
TimedAction vehiclesEWThread = TimedAction(50, countEWVehicles); // checks for ES vehicle every 50 sec and count the total vehicle
TimedAction vehicleRemovalThread = TimedAction(1000, passVehicles); //a vehicle take minimum 5 to pass


// where's our third task? well, it's the loop itself :) the task
