Arduino seemingly ignoring a Boolean value?

I've got an Arduino Mega set up to automatically switch power to the tracks around a model railroad turntable. The idea is that when the turntable is aligned with the track, it will trigger an LDR under the turntable (via a pair of 1mm holes), and this will turn the track power on. Once the locomotive has moved off the turntable, it'll trigger a sensor on the turntable lead track, which will turn the power off. The reverse will happen when a train arrives.

However, what actually happens is that when the turntable is aligned (during a departure), the track power flickers on and off several times, continuously, and I'm unable to drive the train off. I've been over my code, and I can't see what may be going wrong. When the system goes into departure mode, it checks the trackPowered boolean variable. If it's false, it turns on the track power. If true, then it checks the sensor on the lead track.

Below is the loop() function of my sketch:
(I have the arrival mode commented out, as I just want to focus on getting the departure working at the moment.)

void loop() {
  // put your main code here, to run repeatedly:
  //Check the buttons for pressing/held.
  clockwButton.tick();
  cclockButton.tick();
  if (baseET.receiveData()){
    //We've received data from the base station.  A loco has been called.
    if ((baseData.controllerID == -1) && (baseData.locoAddress != 0)){
      //ControllerID of -1 is used to indicate it's for the staging controller.
      //Handset sends out an address of 0 on startup  - need to ignore it.
      currLoco = baseData.locoAddress; //set the current loco address
      disNumLed(currLoco);
      delay(1000);
      /* Check if the locomotive should be in the turntable.  -1 from the
       *  getLocoTrack function indicates it's one of the BVLC locos.  I.e. 
       *  it's not the PRM one.  If it's equal to the value in locoOut, then
       *  it's the BVLC loco in staging, and doesn't need any action taken.
       *  
       *  Remember, the turntable bridge will be aligned with the exit track
       *  by default.
       */
       currLocoTrack = getLocoTrack(currLoco);
       disNumLed(currLocoTrack);
       delay(1000);
    }
  }
  if (!departMode && !arriveMode && (currLoco > 0)){
    if ((currLocoTrack > 0) && trackReached(currLocoTrack)){
      //Then we've selected a locomotive and moved the turntable to its track.
      departMode = true;
      disNumLed(currLoco);
    }
    if (trackReached(EXIT_TRACK)){
      //Then an incoming loco has tripped the sensor on the exit track.
      //arriveMode = true;
      //disNumLed(currLoco);
    }
  }
  if (departMode){
    if (!trackPowered){
      //Track isn't powered - turn it on.
        setTrackPower(currLocoTrack, true);
    } 
    if (trackPowered) {
      //Track is turned on - wait to see when train has left.
      if (trackReached(EXIT_TRACK)){
        //Departing loco has tripped the exit sensor.  Power down and reset.
        setTrackPower(currLocoTrack, false);
        departMode = false;
        dispLED.clearDisplay(0);
      }
    }
  }
  if (arriveMode){
    /* At this point, a locomotive has passed the sensor on the exit track.  Turntable
     *  will move to that locomotive's track, then loco will move off.  Need to detect
     *  when turntable has stopped moving, then check it.
     */
     if (trackReached(currLocoTrack)){
      //Then the turntable has reached the track for this loco.
      if (!trackPowered){
        /* If track isn't powered, then turntable has just arrived.  Turn track power
         *  on and start the timer to check if track is still moving.
         */
        setTrackPower(currLocoTrack, true);
        moveTime = millis(); 
      }
      if (trackPowered && !tableStopped){
        //Check if turntable is still moving.
        if ((millis() - moveTime) > BRIDGE_DELAY){
          //Then it's time to do the check.
          if ((stepCounter - lastCount) == 0){
            tableStopped = true;
          } else {
            //Turntable has moved.  Take current step reading for next 
            lastCount = stepCounter;
          }
        }
      }
      if (trackPowered && tableStopped){
        //Wait for train to get off bridge.
        delay(ARRIVE_DELAY);
        //Turn off track power and reset everything
        setTrackPower(currLocoTrack, false);
        arriveMode = false;
        tableStopped = false;
        dispLED.clearDisplay(0);
        currLoco = 0;
        currLocoTrack = 0;
      }
     }
  }
}

Below is the setTrackPower function:

void setTrackPower(int trackno, boolean turnOn){
  if (turnOn){
    //We're turning track power on
    digitalWrite(relayPins[trackno], LOW);
    digitalWrite(LED_PIN, HIGH);
    trackPowered = true;
  } else {
    //We're turning track power off.
    digitalWrite(relayPins[trackno], HIGH);
    digitalWrite(LED_PIN, LOW);
    trackPowered = false;
  }
}

I can't see why the track power is 'flickering' rather than staying on. I thought reading the trackPowered boolean would be enough to actually determine if the track power was turned on or not. What am I missing?

What happens if the last statement in SetTackPower() is:

  • delay(1000UL);*
 if (!departMode && !arriveMode && (currLoco > 0)){

How sure are you of "order of execution" in that statement?
Extra parenthesis would make me feel better...

westfw:
How sure are you of "order of execution" in that statement?
Extra parenthesis would make me feel better...

&& is associative. Evaluation order makes absolutely no difference...
Regards,
Ray L.