If Statements

In my code I have two variables, StationOCC and Lift OCC that are Boolean and can either be true or false. I have a statement such as:

if (StationOCC == true && LiftOCC == false){
digitalWrite(DispatchLT, HIGH);}

The problem is, it writes the DispatchLT high even when the conditions of the if statement aren't valid. For example, if I have it set StationOCC false and LiftOCC true earlier in the code, it still lights up the dispatch light when it gets to this section. What am I doing wrong here?

I'm also having the same issue if I have it read a pin value like:

if (DispatchVAL == LOW && StationOCC == true){
digitalWrite(MotorPin, HIGH);}

In this case, it would allow the MotorPin to be high regardless of what the StationOCC variable is and only look at what the dispatch button value is.

Please post your complete code as the snippets do not reveal the cause.

I'd be inclined to use Serial to write the values of variables to the monitor at key points, and to print "here I am" from various parts of the code when there are ifs, just to verify where it got to

Alright, that's a good idea because maybe it doesn't have the values I think it does.

I can post my whole code but its very long. If that would help though I can do that once I get back home.

because maybe it doesn't have the values I think it does.

:wink:

//-----LIGHT PINS-----
const int BlockLT = 2;
const int EstopLT = 22;
const int ResetLT = 23;
const int StationLT = 24;
const int LiftLT = 25;
const int WaitLT = 26;
const int LstopLT = 27;
const int ReintLT = 28;
const int DispatchLT = 29;
//-----BUTTON PINS-----
const int EstopBT = 30;
const int ResetBT = 31;
const int ModeBT = 32;
const int LstopBT = 33;
const int ReintBT = 34;
const int JogBT = 35;
const int DispatchBT = 36;
//-----MOTOR PINS-----
const int LiftMotor = 52;
const int StationMotor = A15;
const int BlockMotor = 50;
const int WaitMotor = 51;
//------PHOTOEYE PINS-----
const int LiftEye = A0;
const int BlockEye = A1;
const int WaitEye = A2;
const int StationEye = A3;
//-----BRAKE PINS-----
const int LWaitBrake = 49;
const int RWaitBrake = 47;
const int StationBrake = 53;
//-----ALL ZONES OCCUPIED-----
boolean StationOCC = true;
boolean LiftOCC = true;
boolean BlockOCC = true;
boolean WaitOCC = true;
//-----VARIABLES-----
boolean ReintPRO = false; //Begin Initalizing Process
boolean CascadePRO;
boolean EstopPRO = false; //Estop Active When 'true'
boolean LstopPRO = false; //Lstop Active When 'true'
boolean DispatchPRO = false;
int EstopLIT = LOW;
int LstopLIT = LOW;
int DispatchLIT;
//-----TIMERS-----
long previousMillis = 0;
long EstopInterval = 250;
long LstopInterval = 250;
long DispatchInterval = 750;

void setup(){
//-----LIGHT PIN SETUP-----  
  pinMode(BlockLT, OUTPUT);
  pinMode(EstopLT, OUTPUT);
  pinMode(ResetLT, OUTPUT);
  pinMode(StationLT, OUTPUT);
  pinMode(LiftLT, OUTPUT);
  pinMode(WaitLT, OUTPUT);
  pinMode(LstopLT, OUTPUT);
  pinMode(ReintLT, OUTPUT);
  pinMode(DispatchLT, OUTPUT);
//-----BUTTON PIN SETUP-----
  pinMode(EstopBT, INPUT_PULLUP);
  pinMode(ResetBT, INPUT_PULLUP);
  pinMode(ModeBT, INPUT_PULLUP);
  pinMode(LstopBT, INPUT_PULLUP);
  pinMode(ReintBT, INPUT_PULLUP);
  pinMode(JogBT, INPUT_PULLUP);
  pinMode(DispatchBT, INPUT_PULLUP);
//-----MOTOR PIN SETUP-----
  pinMode(LiftMotor, OUTPUT);
  pinMode(StationMotor, OUTPUT);
  pinMode(BlockMotor, OUTPUT);
  pinMode(WaitMotor, OUTPUT);
//-----PHOTOEYE PIN SETUP-----
  pinMode(LiftEye, INPUT);
  pinMode(BlockEye, INPUT);
  pinMode(WaitEye, INPUT);
  pinMode(StationEye, INPUT);
//-----BRAKE PIN SETUP-----
  pinMode(LWaitBrake, OUTPUT);
  pinMode(RWaitBrake, OUTPUT);
  pinMode(StationBrake, OUTPUT);
}

void loop(){
  //Starting Millis Timer
  unsigned long currentMillis = millis();
  //Reading Button Values
  int EstopVAL = digitalRead(EstopBT);
  int DispatchVAL = digitalRead(DispatchBT);
  int ReintVAL = digitalRead(ReintBT);
  int LstopVAL = digitalRead(LstopBT);
  int JogVAL = digitalRead(JogBT);
  int ResetVAL = digitalRead(ResetBT);
  int StationEyeVAL = digitalRead(StationEye);
  int LiftEyeVAL = digitalRead(LiftEye);
  int BlockEyeVAL = digitalRead(BlockEye);
  int WaitEyeVAL = digitalRead(WaitEye);
  int ModeVAL = digitalRead(ModeBT);
  

//-----SYSTEM IN MAINT MODE-----
if (ModeVAL == HIGH){    
  //Use Dispatch Button to Test Block Motor
  if (DispatchVAL == LOW){
    digitalWrite(BlockMotor, HIGH);}
  else{
    digitalWrite(BlockMotor, LOW);}
    
  //Use Initalize Button to Test Station Motor
  if (ReintVAL == LOW){
    digitalWrite(StationMotor, HIGH);}
  else{
    digitalWrite(StationMotor, LOW);}
    
  //Use Reset Button to Test Station Brake
  if (ResetVAL == LOW){
    digitalWrite(ResetLT, HIGH);
    digitalWrite(StationBrake, HIGH);}
  else{
    digitalWrite(ResetLT, LOW);
    digitalWrite(StationBrake, LOW);}
    
  //Use LiftStop Button to Test Lift Motor  
  if (LstopVAL == LOW){
    digitalWrite(LstopLT, HIGH);
    digitalWrite(LiftMotor, LOW);}   
   else{
    digitalWrite(LstopLT, LOW);
    digitalWrite(LiftMotor, HIGH);}
    
  //Photoeye at Station Test
  if (StationEyeVAL == LOW){
    digitalWrite(StationLT, HIGH);}
  else{
    digitalWrite(StationLT, LOW);}
    
  //Photoeye at Lift Test
  if (LiftEyeVAL == LOW){
    digitalWrite(LiftLT, HIGH);}
  else{
    digitalWrite(LiftLT, LOW);}
  
  //Photoeye at Block Test
  if (BlockEyeVAL == LOW){
    digitalWrite(BlockLT, HIGH);}
  else{
    digitalWrite(BlockLT, LOW);}
    
  //Photoeye at Wait Test
  if (WaitEyeVAL == LOW){
    digitalWrite(WaitLT, HIGH);}
  else{
    digitalWrite(WaitLT, LOW);}  
    
  //Wait Brake Test
  if (JogVAL == LOW){
    digitalWrite(RWaitBrake, HIGH);
    digitalWrite(LWaitBrake, HIGH);
    digitalWrite(EstopLT, HIGH);}  
  else{
    digitalWrite(LWaitBrake, LOW);
    digitalWrite(RWaitBrake, LOW);
    digitalWrite(EstopLT, LOW);}
  if (JogVAL == HIGH && WaitEyeVAL == LOW){
    digitalWrite(WaitMotor, HIGH);
    delay(1200);}
  else{
    digitalWrite(WaitMotor, LOW);} 
}
//-----SYSTEM IN AUTO MODE-----
else{
//-----*****EMERGENCY STOP MODE*****-----
if (EstopVAL == LOW){
  EstopPRO = true;}
if (EstopPRO == true){
  digitalWrite(LiftMotor, LOW);
  digitalWrite(BlockMotor, LOW);
  digitalWrite(WaitMotor, LOW);
  digitalWrite(StationMotor, LOW);
  digitalWrite(RWaitBrake, HIGH);
  digitalWrite(LWaitBrake, HIGH);
  digitalWrite(StationBrake, HIGH);
  digitalWrite(LstopLT, HIGH);
    if(currentMillis - previousMillis > EstopInterval){//Estop Blinking
    previousMillis = currentMillis;
      if (EstopLIT == LOW){
        EstopLIT = HIGH;}
      else{
        EstopLIT = LOW;}
      digitalWrite(EstopLT, EstopLIT);}}
//-----*****LIFT STOP MODE*****-----
if (LstopVAL == LOW){
  LstopPRO = true;}
if (LstopPRO == true){
  digitalWrite(LiftMotor, LOW);
    if(currentMillis - previousMillis > LstopInterval){//Lift Stop Blinking
    previousMillis = currentMillis;
      if (LstopLIT == LOW){
        LstopLIT = HIGH;}
      else{
        LstopLIT = LOW;}
      digitalWrite(LstopLT, LstopLIT);}}
//-----*****FAULT RESETING*****-----
if (EstopPRO == true && EstopVAL == HIGH){
  digitalWrite(ResetLT, HIGH);}
if (LstopPRO == true && LstopVAL == HIGH){
  digitalWrite(ResetLT, HIGH);}
if (ResetVAL == LOW && EstopVAL == HIGH && LstopVAL == HIGH){
  EstopPRO = false;
  LstopPRO = false;
  digitalWrite(EstopLT, LOW);
  digitalWrite(LstopLT, LOW);
  digitalWrite(ResetLT, LOW);}
  
//Indicates initalization needs to be performed when all blocks are occupied  
  if (StationOCC == true && LiftOCC == true && BlockOCC == true && WaitOCC == true){
    digitalWrite(ReintLT, HIGH);}
//Occupied lights on when zone is occupied  
  if (StationOCC == true){
    digitalWrite(StationLT, HIGH);}
  if (LiftOCC == true){
   digitalWrite(LiftLT, HIGH);}
  if (BlockOCC == true){
   digitalWrite(BlockLT, HIGH);}
  if (WaitOCC == true){
   digitalWrite(WaitLT, HIGH);}

//-----*****DISPATCHING FROM STATION*****-----
//-----*****TRAIN IN LIFT ZONE*****-----
//-----*****TRAIN IN BLOCK ZONE*****-----
if(BlockEyeVAL == LOW && EstopPRO == false && WaitOCC == false){
  digitalWrite(BlockMotor, HIGH);}
else{
  digitalWrite(BlockMotor, LOW);}
//-----*****TRAIN IN HOLD ZONE*****------
} 

}

Here is my code. The area where I am having an issue is at the end.

This is where excess brackets can really help.

Try the following and report back:

if ((StationOCC == true) && (LiftOCC == false)){
digitalWrite(DispatchLT, HIGH);}

Edit: Nevermind

Not really part of the problem, but make these unsigned long
long previousMillis = 0;
long EstopInterval = 250;
long LstopInterval = 250;
long DispatchInterval = 750;

these are all used as digital inputs:
pinMode(LiftEye, INPUT);
pinMode(BlockEye, INPUT);
pinMode(WaitEye, INPUT);
pinMode(StationEye, INPUT);

maybe change INPUT to INPUT_PULLUP so they are not left floating.

Where do these get changed?

boolean StationOCC = true;
boolean LiftOCC = true;
boolean BlockOCC = true;
boolean WaitOCC = true;

I only see them tested in void loop, but never changed.

Add some Serial.print's to see if the variable are getting set to just before the if (... && ... &&...) statements
May turn that something is not wired to the pin think it is