understanding timing and millis()

rewritten in basic "if"

boolean reloadComplete = 0; //flag for conditional statements

const int entryGate = 7; //sets Pin for entry gate
boolean entryGateState = LOW;  //sets first state for entry gate
unsigned long prevEGmillis = 0; //sets time for entry gate on first run
long EGinterval = 2000; //how long to keep the entry gate open
int entryGateRun = 30; //how long to keep gate open for
int entryGateTimer = 0; //initial timer number

const int exitGate = 8; //
boolean exitGateState = LOW;
boolean exitGateComplete = 0;
unsigned long prevXGmillis;
long XGinterval = 3000;
int exitGateStartDelay = 1;
int exitGateStopDelay = 30;
int exitGateTimer = 0;


const int conveyorSol = 9;
boolean conveyorState = LOW;
boolean conveyorComplete = 0;
unsigned long prevConveyorMillis = 0;
long conveyorInterval = 5000;
int conveyorStartDelay = 1;
int conveyorStopDelay = 60;
int conveyorTimer = 0;


const int pistonSol = 6; //controls the air solenoid for the piston that actually fills the bottles
boolean pistonSolState = LOW;

const int startBut = 3; //start button
boolean startButState = 0; //resting state
boolean lastStartButState = 0;
//int startButInterval = 4000;

const int pauseBut = 4;  //single cycle start button setup
boolean pauseButState = 0;
boolean lastPauseButState = 0;

const int stopBut = 5; //emergency stop button. at this point, it stops and reset the micro count, kills the piston solenoid, and stops the conveyor and opens the start/stop gates
boolean stopButState = 0;

const int microSwitch = 2; //sets pin for micro switch
byte microCount = 0; //sets initial count to 0.
boolean  microState = 0;
boolean  lastMicroState = 0;
byte pistonCycleCnt = 4; //the number of times the piston will cycle before resetting

boolean reloadState = 0; //if TRUE then run reload program

void setup() {
  Serial.begin (115200);
  pinMode (entryGate, OUTPUT);
  pinMode(exitGate, OUTPUT);
  pinMode(pistonSol, OUTPUT);
  pinMode(conveyorSol, OUTPUT);
  pinMode (startBut, INPUT_PULLUP);
  pinMode(stopBut, INPUT_PULLUP);
  pinMode (microSwitch, INPUT_PULLUP);
  pinMode(pauseBut, INPUT_PULLUP);
  Serial.println ("Piston Filler 01");
}
void loop() {

  //sets the state for the stop button from which other functions follow
  if (digitalRead(stopBut) == LOW)
  {
    stopButState = 1;
    //debounce/delay is intentional
  }

  //starts the process. At a future date this will start with the reload process to get the bottles under the machine.

  if ((digitalRead(startBut) == LOW) && (reloadState == 0) && (startButState == 0))
  {
    startButState = 1;
    pauseButState = 0; //zeroes out the pause button if it was set earlier
    pistonSolState = HIGH; //opens the solenoid to start filling bottles
    stopButState = 0; //zeroes out the stop button if it was hit earlier
    Serial.println ("startBut LOW");
    delay(150); //debouncer
  }


  if (startButState == 1)
  {
    pistonRun();
  }


  if (digitalRead(pauseBut) == LOW) //look for the pause button to be pressed. This allows the machine to finish filling and stops the bottles from being ejected (either before or during reload procedure)
  {
    pauseButState = 1;
  }
  reload(); //added to the loop to for the counter functions, however it will not be called until reloadState==1
}


void pistonRun()
{

  if ((digitalRead(microSwitch) == LOW) && (startButState == 1)) //conditional statement so that the count only incremets when in run mode
  {
    microCount++; //counts how many time the micro switch is hit by the piston
    Serial.print("M.Cnt  ");
    Serial.println(microCount);
    delay(300);
  }


  if (microCount == pistonCycleCnt) //runs the reload program if piston has cycled enough times
  {
    reloadState = 1;
    pistonSolState = 0;
  }

  if (stopButState == 1) // stops the piston from cycling and resets the count so when it starts, its back at 0
  {
    pistonSolState = 0; //stops the piston from cycling again
    microCount = 0; //reset the count so it's ready to go for the next run
  }


  digitalWrite(pistonSol, pistonSolState); //sets the piston solenoid to run or not
  if (reloadState == 1) //if the state was set earlier, then its time to run the reload program. this is separate from the act of counting in case i needed to add additional conditionals
  {
    exitGateTimer = 0; //moved from the entry gate section of reload to here
    conveyorTimer = 0; //""
    entryGateTimer = 0; //""
    reload();
    microCount = 0;
    startButState = 0;
  }
}


void reload() //is trigger when the micro switch has been triggered enough times
{
  //Serial.println("reload");


  unsigned long conveyorMillis = millis();
  if ((reloadState == 1) && (pauseButState == 0) && (stopButState == 0)) {
    //since this is looping, this is the main phrase to call upon the relays to throw. called in pistonRun()

    if (conveyorMillis - prevConveyorMillis > 100L)
    {
      //Serial.println("inside millis");
      exitGateTimer++;
      conveyorTimer++;
      entryGateTimer++;
      prevConveyorMillis = conveyorMillis;
    }

    conveyorStopDelay = conveyorStopDelay + conveyorStartDelay; //same counter so correcting maths
    exitGateStopDelay = exitGateStopDelay + exitGateStartDelay; //same counter so correcting maths

    exitGateTimer = constrain(exitGateTimer, 0, 255);
    conveyorTimer = constrain(conveyorTimer, 0, 255);
    entryGateTimer = constrain(entryGateTimer, 0, 255);

    Serial.print("conveyor timer  ");
    Serial.println(conveyorTimer);

    if ((conveyorState == LOW) && (conveyorTimer > conveyorStartDelay))
    {
      //Serial.println("start conveyor");
      conveyorState = HIGH;
    }
    if ((conveyorState == HIGH) && (conveyorTimer > conveyorStopDelay))
    {
      //Serial.println("stop conveyor");
      conveyorState = LOW;
      conveyorComplete = 1;
    }
    if ((exitGateState == LOW) && (exitGateTimer > exitGateStartDelay))
    {
      exitGateState = HIGH;
    }
    if ((exitGateState == HIGH) && (exitGateTimer > exitGateStopDelay))
    {
      exitGateState = LOW;
      exitGateComplete = 1;
    }
    if ((exitGateComplete == 1) && (entryGateState == LOW))
    {
      entryGateState = HIGH;
      entryGateTimer = 0;
    }
    if ((entryGateState == HIGH) && (entryGateTimer > entryGateRun) && (conveyorComplete == 1))
    {
      reloadState = 0;
      entryGateState = LOW;
      startButState = 1;
      pistonSolState = 1;
      exitGateComplete = 0;
      conveyorComplete = 0;
      //exitGateTimer = 0;
      //conveyorTimer = 0;
      //entryGateTimer = 0;
      pistonRun();
    }
  } //this switch closes reloadState == 1 and pauseButState == 0 and stopButState == 0

  if (stopButState == 1) {
    entryGateState = LOW;
    exitGateState = LOW;
    conveyorState = LOW;
    reloadState = 0;
    exitGateComplete = 0;
    conveyorComplete = 0;
    exitGateTimer = 0;
    conveyorTimer = 0;
    entryGateTimer = 0;
  }
  if (pauseButState == 1) {
    prevConveyorMillis = conveyorMillis;
    conveyorState = 0;
    exitGateState = 0;
    entryGateState = 0;
    reloadState = 0;
    startButState = 0;
    exitGateComplete = 0;
    conveyorComplete = 0;
    exitGateTimer = 0;
    conveyorTimer = 0;
    entryGateTimer = 0;
  }

  //Serial.print("digital write conveyor  ");
  //Serial.println(conveyorState);
  digitalWrite (exitGate, exitGateState);
  digitalWrite(conveyorSol, conveyorState);
  digitalWrite (entryGate, entryGateState);



}//this is the end of the reload loop