Bool thinks it's true when it isn't

I am making a control/clearance system for a zipline. The way it works is each side has three buttons, one for saying zipline clear, one for e-stopping, and one to indicate the zipline is moving/not moving. I have 4 independent bools: topClear, bottomClear, zipping, and fault. topClear, fault, and bottomClear all work just fine. But, no matter how many times I tell zipping that it is false, it still thinks it's true. I've checked this multiple ways and confirmed that it is the bool thinking it is true... but I'm not sure how it is even possible. I might have made a mistake, but I am almost certain that it is the bool being true. Any help is greatly appreciated!
Code (sorry it is pretty long and lots of things being repeated):

//LEDs
int topR = 7;
int topG = 2;
int topY = 3;
int bottomR = 4;
int bottomG = 5;
int bottomY = 6;

//buttons
int bottomFAULT = 9;
int bottomCLEAR = 10;
int topFAULT = 11;
int topCLEAR = 12;
int zipOn = 8;
int zipOff = 13;

//for flashing
int ledState = LOW;
int ledState2 = LOW;
unsigned long previousMillis = 0;
const long interval = 100;
const long interval2 = 1000;

//the bools indicating if things are happening or not
bool zipping = false;
bool topClear = false;
bool bottomClear = false;
bool fault = false;

//lots of edge detection variables

//bottom fault
int bfPushCounter = 1;   
int bfState = 0;         
int lastbfState = 0; 

//bottom clear
int bcPushCounter = 1;   
int bcState = 0;         
int lastbcState = 0; 

//top fault
int tfPushCounter = 1;   
int tfState = 0;         
int lasttfState = 0; 

//top clear
int tcPushCounter = 1;   
int tcState = 0;         
int lasttcState = 0; 

//zip on
int zipOnPushCounter = 1;
int zipOnState = 0;
int lastZipOnState = 0;

//zip off
int zipOffPushCounter = 1;
int zipOffState = 0;
int lastZipOffState = 0;

void setup() {
pinMode(topR, OUTPUT);
pinMode(topG, OUTPUT);
pinMode(topY, OUTPUT);
pinMode(bottomR, OUTPUT);
pinMode(bottomG, OUTPUT);
pinMode(bottomY, OUTPUT);
pinMode(bottomFAULT, INPUT);
pinMode(bottomCLEAR, INPUT);
pinMode(topFAULT, INPUT);
pinMode(topCLEAR, INPUT); 
pinMode(zipOn, INPUT);
pinMode(zipOff, INPUT);
Serial.begin(9600);
}

//flashing the LEDs when the zipline is in motion
void weZippin(){
  digitalWrite(topR, LOW);
  digitalWrite(bottomR, LOW);
  digitalWrite(topG, LOW);
  digitalWrite(bottomG, LOW);
  digitalWrite(topY, LOW);
  digitalWrite(bottomY, LOW);
  
    unsigned long currentMillis = millis();

  if (currentMillis - previousMillis >= interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;

    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW) {
      ledState = HIGH;
    } else {
      ledState = LOW;
    }

    // set the LED with the ledState of the variable:
    digitalWrite(topG, ledState);
    digitalWrite(bottomG, ledState);
    digitalWrite(bottomY, !ledState);
    digitalWrite(topY, !ledState);
    
  }
  

    
  }
  
  

// **** VOID LOOOOOOOP **************************************************************//
  

void loop() {
  
//EDGE DETECTION 
//I'll put all the stuff that happens at the bottom, all that happens here is just incrementing the counters

//top clear
tcState = digitalRead(topCLEAR);
if(tcState != lasttcState){
  if(tcState == HIGH){
    tcPushCounter++;
    Serial.println("Top clear");
  }
  else{
  }
  delay(50);
}
lasttcState = tcState;

//top fault---------------------------------------------
tfState = digitalRead(topFAULT);
if(tfState != lasttfState){
  if(tfState == HIGH){
    tfPushCounter++;
    Serial.println("Top E-stop activated");
  }
  else{
  }
  delay(50);
}

lasttfState = tfState;

//bottom clear---------------------------------------------
bcState = digitalRead(bottomCLEAR);
if(bcState != lastbcState){
  if(bcState == HIGH){
    bcPushCounter++;
    Serial.println("Bottom Clear");

  }
  else{
  }
  delay(50);
}
lastbcState = bcState;

//bottom fault---------------------------------------
bfState = digitalRead(bottomFAULT);
if(bfState != lastbfState){
  if(bfState == HIGH){
    bfPushCounter++;
    Serial.println("Bottom E-stop activated");

  }
  else{
  }
  delay(50);
}
lastbfState = bfState;

//zip on-------------------------------------------------
zipOnState = digitalRead(zipOn);
if(zipOnState != lastZipOnState){
  if(zipOnState == HIGH){
    zipOnPushCounter++;
    Serial.println("Zipline in motion");
  }
  else{
  }
  delay(50);
}
lastZipOnState = zipOnState;




//zip off---------------------------------------
zipOffState = digitalRead(zipOff);

if(zipOffState != lastZipOffState){
  if(zipOffState == HIGH){
    tcPushCounter++;
    bcPushCounter++;
   zipOnPushCounter++;
   Serial.println("Zipline stopped");
  }
  else{
  }
  delay(50);

}
  lastZipOffState = zipOffState;


//and now ... (drumroll) the action I said earlier ( kind of - I just turn bools to true or false here)
//top clear
if (tcPushCounter % 2 == 0) {
  

  topClear = true;
  } else {
    topClear = false;
  }

  //bottom clear
  if (bcPushCounter % 2 == 0) {
    bottomClear = true;
    

  } else {
    bottomClear = false;
    
  }

  //zip on
  if (zipOnPushCounter % 2 == 0) {
    zipping = true;
  } 
  else {
   }
  if(zipOffPushCounter % 2 == 0){
    zipping = false;
  }
  else{
    
  }
  //faults

  //top fault
  if (tfPushCounter % 2 == 0) {
    fault = true;
  } 
  else {
    fault = false;
  }

  //bottom fault
  if (bfPushCounter % 2 == 0) {
    fault = true;
  } 
  else {
    fault = false;
  }
  
  

  //and nowwwww the action (for real this time)

//if neither top or bottom is clear
if(topClear == false && bottomClear == false){
  digitalWrite(topR, HIGH);
    digitalWrite(topG, LOW);
    digitalWrite(topY, LOW);
    digitalWrite(bottomR, HIGH);
    digitalWrite(bottomG, LOW);
    digitalWrite(bottomY, LOW);
}

  //if top is clear and ground is not
  
if(topClear == true && bottomClear == false){
  digitalWrite(topR, LOW);
    digitalWrite(topG, LOW);
    digitalWrite(topY, HIGH);
    digitalWrite(bottomR, LOW);
    digitalWrite(bottomG, LOW);
    digitalWrite(bottomY, HIGH);
}


//if top is not clear and ground is clear
if(topClear == false && bottomClear == true){
  digitalWrite(topR, LOW);
    digitalWrite(topG, LOW);
    digitalWrite(topY, HIGH);
    digitalWrite(bottomR, LOW);
    digitalWrite(bottomG, LOW);
    digitalWrite(bottomY, HIGH);
}

//if both are clear
if(topClear == true && bottomClear == true){
  digitalWrite(topR, LOW);
    digitalWrite(topG, HIGH);
    digitalWrite(topY, LOW);
    digitalWrite(bottomR, LOW);
    digitalWrite(bottomG, HIGH);
    digitalWrite(bottomY, LOW);
}
 

  //if the zipline is in motion
  if(zipping = true){
    weZippin();
    Serial.println("test");
  }


  //if either fault button is presssed
  if(fault = true){
    zipping = false;
    topClear = false;
    bottomClear = false;
    digitalWrite(topR, HIGH);
    digitalWrite(topG, LOW);
    digitalWrite(topY, LOW);
    digitalWrite(bottomR, HIGH);
    digitalWrite(bottomG, LOW);
    digitalWrite(bottomY, LOW);
  }


}
//the LED commands ( for copy and pasting )
/* digitalWrite(topR, LOW);
    digitalWrite(topG, LOW);
    digitalWrite(topY, LOW);
    digitalWrite(bottomR, LOW);
    digitalWrite(bottomG, LOW);
    digitalWrite(bottomY, LOW);
 */

Yeah. :blush:

Oops. :roll_eyes:

Thank you! This fixed it. As with most programming problems, the solution to the hours of nothing working can be solved with just two character changes.

1 Like

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.