2

Trying to formulate the correct if condition test. I want to take action only if both floor locks are online (not reporting eStatusLockUnknown). The two tests I've tried are below. When debugging, I can see that both floor locks are reporting *.lockState = eStatusLockIndeterminate, but I'm not falling into my action code. Microchip xc16 v1.25 compiler.

if (( sLockStatus[eHeadFloorLock].lockState == ( eStatusLockIndeterminate 
|| eStatusLockEngaged || eStatusLockStowed || eStatusLockFullyEngaged ))
   && ( sLockStatus[eFootFloorLock].lockState == ( eStatusLockIndeterminate || eStatusLockEngaged || eStatusLockStowed || eStatusLockFullyEngaged )))
{
    // take action
}

if (( sLockStatus[eHeadFloorLock].lockState == eStatusLockIndeterminate )
     || ( sLockStatus[eHeadFloorLock].lockState == eStatusLockEngaged )
     || ( sLockStatus[eHeadFloorLock].lockState == eStatusLockStowed )
     || ( sLockStatus[eHeadFloorLock].lockState == eStatusLockFullyEngaged )
   && ( sLockStatus[eFootFloorLock].lockState == eStatusLockIndeterminate )
     || ( sLockStatus[eFootFloorLock].lockState == eStatusLockEngaged )
     || ( sLockStatus[eFootFloorLock].lockState == eStatusLockStowed )
     || ( sLockStatus[eFootFloorLock].lockState == eStatusLockFullyEngaged ))
   {
      // take action
   }
tung
  • 719
  • 2
  • 14
  • 30
kward
  • 21
  • 2

1 Answers1

1

First is wrong, because you are doing || on the constants and not on the conditions.

Second is wrong, since && binds more tightly than ||. You need parenthesis around the outermost || conditions.

Following should do what you want.

LockState headLockState = sLockStatus[eHeadFloorLock].lockState;
LockState footLockState = sLockStatus[eHeadFloorLock].lockState;

if (( headLockState == eStatusLockIndeterminate
     || headLockState == eStatusLockEngaged
     || headLockState == eStatusLockStowed
     || headLockState == eStatusLockFullyEngaged )
   && ( footLockState == eStatusLockIndeterminate
     || footLockState == eStatusLockEngaged
     || footLockState == eStatusLockStowed
     || footLockState == eStatusLockFullyEngaged ))
zch
  • 14,931
  • 2
  • 41
  • 49