 
 void loop() {
   // put your main code here, to run repeatedly:
+  timeSinceStart = millis();
+
+  switch (traffic) {
+    case NS_GREEN:
+      if (vehicleEWCounter > 0) {
+        changeEWGreenToAmber();
+      }
+      break;
+    case EW_GREEN:
+      if (vehicleNSCounter > 0) {
+        changeNSGreenToAmber();
+      }
+      break;
+    case NS_AMBER:
+      changeNSAmberToRed();
+      break;
+    case NS_RED:
+      changeNSRedToEWGreen();
+      break;
+    case EW_AMBER:
+      changeEWAmberToRed();
+      break;
+    case EW_RED:
+      changeEWRedToNSGreen();
+      break;
+    default:
+      break;
+  }
 
   //check on our threads. based on how long the system has been
   //running, do they need to fire and do work? if so, do it!
   vehiclesNSThread.check();
   vehiclesEWThread.check();
-  signalThread.check();
   vehicleRemovalThread.check();
 
   vehicleEWState = digitalRead(vehicleEW);

  lastVehicleEWState = vehicleEWState;
}

//our second task, fires every few seconds and rotates signals
void changeSignal() {
  //check current state. check if theres any vehicle in other side if so rotate signal
  if (isNSTrafficGreen && vehicleEWCounter > 0) {
    changeEWTrafficToGreen();
  } else if (!isNSTrafficGreen && vehicleNSCounter > 0) {
    changeNSTrafficToGreen();
  } else {
    //do nothing
  }
}

//create a couple timers that will fire repeatedly every x ms
TimedAction vehiclesNSThread = TimedAction(300, countNSVehicles);
TimedAction vehiclesEWThread = TimedAction(300, countEWVehicles);
TimedAction signalThread = TimedAction(5000, changeSignal); //minimum cycle green is 20s we use 5 for now
TimedAction vehicleRemovalThread = TimedAction(1000, passVehicles); //minimum 1 second for a vehicle to pass



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

const int vehicleNS = 13;

// Light delay
unsigned long delayTime = 1000; //1sec

// Light delays
unsigned long amberLightDelay = 2000; //2sec
unsigned long minimumGreenLight = 20000; //20 sec
unsigned long vehicleTime = 5000; //5s
unsigned long redLightDelay = 3000; //3 sec

int vehicleNSCounter = 0;   // counter for the number of vehicles in NS
int vehicleNSState = 0;        // current state of the vehicleNS button
int lastVehicleNSState = 0;    // previous state of the vehicleNS button

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

// time since processing started
unsigned long timeSinceStart;
unsigned long previousTimeSinceStart;
// time to track the greenSignal
unsigned long timeWhenGreenSignal = 0;

unsigned long previousVehicleTime = 0;

enum trafficStates {
  NS_GREEN, // North South green
  NS_AMBER, // North South amber
  NS_RED, // North South red
  EW_GREEN, // East West green
  EW_AMBER, // East West amber
  EW_RED // East West red
};

enum trafficStates traffic = NS_GREEN;

//FUNCTIONS

