Stepper motor limit switches not being recognized

Hello,
I am using a stepper motor to open and close a fixture. The fixture is on a left/right centering linear screw pictured below.

image

I have it working in automatic mode; i.e. wait for one momentary button press (btnStop) and it will open until it reaches the limit switch, wait for another momentary button press (btnStart) and it will close until it reaches the limit switch. The problem is when I want it to manually open and close while continuously holding either the open or close button. I've attached the code for the entire project. The TampBoxClose() and TampBoxOpen() code works as expected (the automatic mode). It's the PositionTB() code that's causing issues. It just seems like it doesn't want to recognize the limit switches even though the Serial Monitor shows the variables changing value.

CastStoneTamperv3.ino (12.9 KB)

I apologize if I did not attach/upload the image and code file correctly.

Thank you all very much.
Lisa

Please attach code the way told in this link: How to get the best out of this forum - Using Arduino / Project Guidance - Arduino Forum

Also, please post schematics.
Your private terminology using words as "automatic", "manual" makes no sense so far.

My apologies about the code. Here it is:

/* Cast Stone Tamper
  by Lisa M. Fida, 2022
  Designed specifically to tamp cast stone mixture into rubber molds
*/

// global variables
int intSafetyState; // variable for reading safety door status
int intStopState; // variable for reading stopbutton status
int intLastStopState = HIGH;   // the previous reading from the input pin (INPUT_PULLUP makes button read HIGH when open)
int intStartState; // variable for reading startbutton status
int intLastStartState = HIGH; // the previosu reading from the input pin (INPUT_PULLUP makes button read HIGH when open)
int intLinActMinState; // variable for reading linear actuator minimun status
bool boolSafety = false;
bool boolStop = false;
bool boolStart = false;
bool boolLinActMin = false;
long currentMillis = 0;
long previousMillis = 0;
// the following variables are unsigned longs because the time, measured in
// milliseconds, will quickly become a bigger number than can be stored in an int.
unsigned long lastDebounceTime = 0;  // the last time the output pin was toggled
unsigned long debounceDelay = 50;    // the debounce time; increase if the output flickers
bool boolGantryUp = false;
bool boolGantryDown = false;
bool boolTampBoxOpen = false;
bool boolTampBoxClose = false;
const int intTampBoxPulseDelay = 1000;

//digital pin locations
const int swtAirValvePin = 2;
const int relLinAct1Pin = 3;
const int relLinAct2Pin = 4;
const int stepTampBoxDirPin = 5;
const int stepTampBoxStepPin = 6;
const int swtLinActMinPin = 7;
const int btnStopPin = 8;  // red momentary button (green)
const int btnStartPin = 9;  // green mementary buttons (blue)
const int swtSafetyDoorPin = 10;
const int stepTampBoxEnablePin = 11;

//analog pin locations
const int swtTampBoxLimitPin = A1; // in parallel (use analog input)
const int swtUpperLowerLimitPin = A2; // in parallel (use analog input)
const int swtPosTBLAPin = A3; // in parallel (use analog input)

void setup() {
  // initialize serial monitor
  Serial.begin(9600);

  // initialize digital pins as input/output
  pinMode(swtAirValvePin, OUTPUT);
  pinMode(relLinAct1Pin, OUTPUT);
  pinMode(relLinAct2Pin, OUTPUT);
  pinMode(stepTampBoxDirPin, OUTPUT);
  pinMode(stepTampBoxStepPin, OUTPUT);
  pinMode(swtLinActMinPin, INPUT_PULLUP);  // reads LOW when pressed
  pinMode(btnStopPin, INPUT_PULLUP);  // reads LOW when pressed
  pinMode(btnStartPin, INPUT_PULLUP);  // reads LOW when pressed
  pinMode(swtSafetyDoorPin, INPUT_PULLUP);  // reads LOW when pressed
  pinMode(stepTampBoxEnablePin, OUTPUT);

  // initialize analog pins as input/output
  pinMode(swtTampBoxLimitPin, INPUT_PULLUP);
  pinMode(swtUpperLowerLimitPin, INPUT_PULLUP);
  pinMode(swtPosTBLAPin, INPUT_PULLUP);

  // set Linear Actuator motor off
  digitalWrite(relLinAct1Pin, LOW);
  digitalWrite(relLinAct2Pin, LOW);

  // set Tamp Box stepper motor off
  digitalWrite(stepTampBoxEnablePin, HIGH); // HIGH turns the motor off
}

void loop() {
  //Read operation switch to see which mode to enter
  // This is an anolog input so we are looking for a range of values
  int PosTBLAreading = analogRead(swtPosTBLAPin);
  if (PosTBLAreading >= 210 && PosTBLAreading <= 250) {  // Linear Actuator Positioning Mode
    PositionGantry();
  }
  else if (PosTBLAreading >= 110 && PosTBLAreading <= 155) {
    PositionTB();
  }
  else {
    TampCycle();
  }
}

void PositionGantry() {
  CheckGantryLimitState();
  CheckLinActMinState();
  // Lower the gantry as long as the start buttons are pressed and no limits reached
  if ((digitalRead(btnStartPin) == LOW) && (!boolGantryDown) && (!boolLinActMin)) {
    digitalWrite(relLinAct1Pin, LOW);
    digitalWrite(relLinAct2Pin, HIGH);
    boolStart = true;
    while ((boolStart) && (!boolGantryDown) && (!boolLinActMin)) {
      if (digitalRead(btnStartPin) == HIGH) {  // No longer being pressed
        boolStart = false;
      }
      CheckGantryLimitState();
      CheckLinActMinState();
    }
    GantryStop();
  }
  // Raise the gantry as long as the stop button is pressed and no limits reached
  else if ((digitalRead(btnStopPin) == LOW) && (!boolGantryUp)) {
    digitalWrite(relLinAct1Pin, HIGH);
    digitalWrite(relLinAct2Pin, LOW);
    boolStop = true;
    while ((boolStop) && (!boolGantryUp)) {
      if (digitalRead(btnStopPin) == HIGH) {  // No longer being pressed
        boolStop = false;
      }
      CheckGantryLimitState();
    }
    GantryStop();
  }
  else {
    GantryStop();
  }
}

void PositionTB() {
  CheckTampBoxLimitState();
  if ((digitalRead(btnStartPin) == LOW) && (boolTampBoxClose == false)) {
    digitalWrite(stepTampBoxEnablePin, LOW);
    digitalWrite(stepTampBoxDirPin, LOW);
  }
  while ((digitalRead(btnStartPin) == LOW) && (boolTampBoxClose == false)) {
    digitalWrite(stepTampBoxStepPin, HIGH);
    delayMicroseconds(intTampBoxPulseDelay);
    digitalWrite(stepTampBoxStepPin, LOW);
    delayMicroseconds(intTampBoxPulseDelay);
    CheckTampBoxLimitState();
    //    Serial.print("boolTampBoxClose = ");
    //    Serial.println(boolTampBoxClose);
  }

  if ((digitalRead(btnStopPin) == LOW) && (boolTampBoxOpen == false)) {
    digitalWrite(stepTampBoxEnablePin, LOW);
    digitalWrite(stepTampBoxDirPin, HIGH);
  }
  while ((digitalRead(btnStopPin) == LOW) && (boolTampBoxOpen == false)) {
    digitalWrite(stepTampBoxStepPin, HIGH);
    delayMicroseconds(intTampBoxPulseDelay);
    digitalWrite(stepTampBoxStepPin, LOW);
    delayMicroseconds(intTampBoxPulseDelay);
    CheckTampBoxLimitState();
    //    Serial.print("boolTampBoxOpen = ");
    //    Serial.println(boolTampBoxOpen);
  }
  digitalWrite(stepTampBoxEnablePin, HIGH);
}

void TampCycle() {
  CheckStartState();
  CheckStopState();
  if ((boolStart) && (!boolStop)) {
    TampBoxClose(); // close the Tamp Box

    GantryDown(); // lower Linear Actuator to its minimum

    if ((!boolStop) && (boolSafety)) {
      AirValve(1); // open Air Valve
    }

    while ((!boolGantryDown) && (!boolStop) && (boolSafety)) {
      CheckGantryLimitState(); // while air is open keep checking to see if Gantry has reached the bottom limit switch
      CheckStopState();
      CheckSafetyState();
    }

    AirValve(0); // close Air Valve

    GantryUp(); // raise Gantry

    TampBoxOpen(); // open the Tamp Box

    if ((!boolStop) && (boolSafety)) {
      boolStart = false;
    }
  }
}

void CheckSafetyState() {
  intSafetyState = digitalRead(swtSafetyDoorPin); // HIGH for an open door
  if (intSafetyState == HIGH) { // Door is open
    boolSafety = false;
  }
  else { // Door is closed
    boolSafety = true;
  }
}

void CheckStopState() {
  // read the state of the Stop/Reset button into a local variable: (set to INPUT_PULLUP)
  // Open button reads HIGH, so we want to look for a LOW state
  // for the rest of the code to work.
  int stopreading = digitalRead(btnStopPin); // Open button reads HIGH, so we want the opposite for the rest of the code to work.

  // check to see if you just pressed the button
  // (i.e. the input went from HIGH to LOW), and you've waited long enough
  // since the last press to ignore any noise:

  // If the switch changed, due to noise or pressing:
  if (stopreading != intLastStopState) {
    // reset the debouncing timer
    lastDebounceTime = millis();
  }

  if ((millis() - lastDebounceTime) > debounceDelay) {
    // whatever the stopreading is at, it's been there for longer than the debounce
    // delay, so take it as the actual current state:

    // if the button state has changed:
    if (stopreading != intStopState) {
      intStopState = stopreading;

      // only continue if the new button state is LOW
      if (intStopState == LOW) {
        boolStop = true;
        AirValve(0); // close the air valve
      }
    }
  }
  // save the stopreading. Next time through the loop, it'll be the lastButtonState:
  intLastStopState = stopreading;
}

void CheckStartState() {
  // read the state of the Stop/Reset button into a local variable: (set to INPUT_PULLUP)
  // Open button reads HIGH, so we want to look for a LOW state
  // for the rest of the code to work.
  int startreading = digitalRead(btnStartPin); // Open button reads HIGH, so we want the opposite for the rest of the code to work.

  // check to see if you just pressed the button
  // (i.e. the input went from HIGH to LOW), and you've waited long enough
  // since the last press to ignore any noise:

  // If the switch changed, due to noise or pressing:
  if (startreading != intLastStartState) {
    // reset the debouncing timer
    lastDebounceTime = millis();
  }

  if ((millis() - lastDebounceTime) > debounceDelay) {
    // whatever the startreading is at, it's been there for longer than the debounce
    // delay, so take it as the actual current state:

    // if the button state has changed:
    if (startreading != intStartState) {
      intStartState = startreading;

      // only continue if the new button state is LOW
      if (intStartState == LOW) {
        boolStart = true;
      }
    }
  }
  // save the startreading. Next time through the loop, it'll be the lastButtonState:
  intLastStartState = startreading;
}

void GantryDown() {
  CheckGantryLimitState();
  CheckLinActMinState();
  CheckStopState();
  CheckSafetyState();
  if ((!boolGantryDown) && (!boolLinActMin) && (!boolStop) && (boolSafety)) {
    digitalWrite(relLinAct1Pin, LOW);
    digitalWrite(relLinAct2Pin, HIGH);
    while ((!boolGantryDown) && (!boolLinActMin) && (!boolStop) && (boolSafety)) {
      CheckGantryLimitState();
      CheckLinActMinState();
      CheckSafetyState();
      CheckStopState();
    }
    GantryStop();
  }
}

void GantryUp() {
  CheckGantryLimitState();
  CheckStopState();
  CheckSafetyState();
  if ((!boolGantryUp) && (!boolStop) && (boolSafety)) {
    digitalWrite(relLinAct1Pin, HIGH);
    digitalWrite(relLinAct2Pin, LOW);
    while ((!boolGantryUp) && (!boolStop) && (boolSafety)) {
      CheckGantryLimitState();
      CheckStopState();
      CheckSafetyState();
    }
    GantryStop();
  }
}

void CheckLinActMinState() {
  intLinActMinState = digitalRead(swtLinActMinPin); // HIGH for an open door
  if (intLinActMinState == LOW) { // The minimum has been reached
    boolLinActMin = true;
  }
  else { // The minimum has not been reached
    boolLinActMin = false;
  }
}

void CheckGantryLimitState() {
  // This is an anolog input so we are looking for a range of values
  int GantryLimreading = analogRead(swtUpperLowerLimitPin);
  if (GantryLimreading >= 10 && GantryLimreading <= 35) {  // the upper limit has been reached
    boolGantryUp = true;
    boolGantryDown = false;
  }
  else if (GantryLimreading >= 150 && GantryLimreading <= 285) {  // the lower limit has been reached
    boolGantryUp = false;
    boolGantryDown = true;
  }
  else {
    boolGantryDown = false;
    boolGantryUp = false;
  }
}

void GantryStop() {
  digitalWrite(relLinAct1Pin, LOW);
  digitalWrite(relLinAct2Pin, LOW);
}

void TampBoxClose() {
  CheckTampBoxLimitState();
  CheckStopState();
  CheckSafetyState();
  if ((!boolTampBoxClose) && (!boolStop) && (boolSafety)) {
    digitalWrite(stepTampBoxEnablePin, LOW);
    digitalWrite(stepTampBoxDirPin, LOW);
  }
  while ((!boolTampBoxClose) && (!boolStop) && (boolSafety)) {
    digitalWrite(stepTampBoxStepPin, HIGH);
    delayMicroseconds(intTampBoxPulseDelay);
    digitalWrite(stepTampBoxStepPin, LOW);
    delayMicroseconds(intTampBoxPulseDelay);
    CheckTampBoxLimitState();
    CheckStopState();
    CheckSafetyState();
  }
  digitalWrite(stepTampBoxEnablePin, HIGH);
}

void TampBoxOpen() {
  CheckTampBoxLimitState();
  CheckStopState();
  CheckSafetyState();
  if ((!boolTampBoxOpen) && (!boolStop) && (boolSafety)) {
    digitalWrite(stepTampBoxEnablePin, LOW);
    digitalWrite(stepTampBoxDirPin, HIGH);
  }
  while ((!boolTampBoxOpen) && (!boolStop) && (boolSafety)) {
    digitalWrite(stepTampBoxStepPin, HIGH);
    delayMicroseconds(intTampBoxPulseDelay);
    digitalWrite(stepTampBoxStepPin, LOW);
    delayMicroseconds(intTampBoxPulseDelay);
    CheckTampBoxLimitState();
    CheckStopState();
    CheckSafetyState();
  }
  digitalWrite(stepTampBoxEnablePin, HIGH);
}

void CheckTampBoxLimitState() {
  // This is an anolog input so we are looking for a range of values
  int tbreading = analogRead(swtTampBoxLimitPin);
  if (tbreading >= 150 && tbreading <= 400) {
    boolTampBoxOpen = true;
    boolTampBoxClose = false;
  }
  else if (tbreading >= 10 && tbreading <= 40) {
    boolTampBoxOpen = false;
    boolTampBoxClose = true;
  }
  else {
    boolTampBoxOpen = false;
    boolTampBoxClose = false;
  }
}

void AirValve(int intOpenClose) {
  if (intOpenClose == 0) {
    digitalWrite(swtAirValvePin, LOW); // closes air valve
  }
  else {
    digitalWrite(swtAirValvePin, HIGH);  // opens air valve
  }
}

Sorry about my private word usage. Hopefully, once you've gone through the code and seen the schematic it will make more sense.

Thanks. You did quite well.
Reading the code I see You use INPUT_PULLUP for the inputs. Good!
However, those Fritzings are toy pictures showing the physical side of the story, not the logical, the engineering side.
A police detective working on a difficult murder case would surely take on a Fritzing and manually create the "normal", logical schematics. I will not do that.
Pin physical positions are uninteresting. The functional pin, wherever it exists, tells the story.

Copying Your code into the IDE I searched for both "manual" and "automatic" without any hit.
It's quite a large code. To get a bird perspective of it I wish You have made a flowchart before starting coding.
Most beginners don't. Either code is copied and modified, or the keyboard is the first instance. Both are bad ways developing code. It takes long experience to create code directly at the keyboard.

Hello,
Manual and automatic were used as a general way to describe the overall functionality of the machine. I use fritzing (yes, I understand the limitations) because it is easy for a non electrical engineer to use. I have coded in Lotus123, MSExcel, VBA, VB, SQL and now in Arduino. I did do a very small flowchart in the beginning and then rearranged it more recently when adding the buttons to control the "automatic" functions verses the "manual" functions. And I don't know if it would even be helpful because it doesn't have lines connecting the dots, etc. Rarely do I start with someone else's code because it doesn't apply to my application. Hence, it starts in my head the way I want the program to flow and then to the keyboard typing basic routines and figuring out/filling in where needed.

What I want to know is why the code performs flawlessly in the TampBoxOpen() and TampBoxClose() routines and not in the PositionTB() routine. The only difference is, the TampBoxOpen() and ...Close() routines are triggered by a button press (thus, automatically once the button is pressed), whereas the PositionTB() routine is triggered by a continuously pressed button (thus, manually because you have to keep the button(s) down for it to work). What gives????

Thank you,
Lisa

Okey, You do have programming experience.

Number one: You don't tell what's happening and what You want to happen.
Number two: Use serial monitor and strategic Serial.print in the code to discover what's happening inside the code. I used that to get into large, unknown programs, and it always worked well.
Your code is way to large, toon advanced to get into just reading the code. What is the thought for each part? I have no idea.

Thank you for your input. I did use SerialMonitor, that's how I know the variables are changing. I'm sorry the code is too large. Maybe someone else will be able to shed some light on my issue.

So the problem is somewhere in this PositionTB() routine, and it isn't recognizing the limit switches? Is the bad behavior that it doesn't stop when the limits switches are set because boolTampBoxClose/Open because it doesn't seem to be applying the logic correctly?

Could both buttons happen to be reading LOW at the same time? The opposite while()s could be doing the steps for the other if() -- maybe tighten the logic a bit and make it if/else so only one button and while can work at any one time.

I might Serial.print() the btns, the boolTampBox*, and the limit switch and make the print messages different in the start and stop branches.

Also, I might try a void tampBoxStep(int dir){digitalWrite(stepTampBoxDirPin, dir); ...} function that might tighten up the code and focus on where the logic is having problems. An extra digitalWrite() would be tiny compared to intTampBoxPulseDelay and checkTampBoxLimitState().

Do you know if it is dropping all the way out of positionTB() and getting called repeatedly by loop() for each step, or if it is getting caught in one of the whiles with bad behavior?

Thank you DaveX for taking the time to read and understand the code. Yes, in the PositionTB() routine, and yes, not recognizing the limit switches. Correct, it doesn't stop when the limit switches are physically engaged.

Re: buttons reading LOW at same time, I don't think so. They are both INPUT_PULLUP and therefore should be reading HIGH when not pressed. I did have them in if/else statements to begin with and had the same problem.

Yeah, as you can see I did Serial.print the bool(s) and the states do change.

I'll try the new void tampBoxStep(int dir) {digitalWrite(stepTampBoxDirPin, dir);...}. Thank you for the tip.

Could be dropping out. My understanding is that once it's in a while loop it stays there until the condition is not met. It acts like it's not reading the limits and therefore the condition is always met and staying in the loop. I'll investigate further.

I was hoping it was a simple fix, something that was right in front of me that I couldn't see.

Thank you for the input,
Lisa

I think I'd probably write the routine like this to separate the stepping from the logic, to make sure it doesn't step both ways if you push both buttons, to ensure the stepping is subordinate to the direction-setting, and to see if the logic is getting trapped in the while loops.

void tampStepBox(int dir) {
  // dir = low/0 is toward close high/1 = toward open
  digitalWrite(stepTampBoxEnablePin, LOW);
  digitalWrite(stepTampBoxDirPin, dir);
  digitalWrite(stepTampBoxStepPin, HIGH);
  delayMicroseconds(intTampBoxPulseDelay);
  digitalWrite(stepTampBoxStepPin, LOW);
  delayMicroseconds(intTampBoxPulseDelay);
}

void PositionTB() {
  CheckTampBoxLimitState();
  if ((digitalRead(btnStartPin) == LOW) && (boolTampBoxClose == false)) {
    while ((digitalRead(btnStartPin) == LOW) && (boolTampBoxClose == false)) {
      tampStepBox(LOW);
      CheckTampBoxLimitState();
      //    Serial.print("boolTampBoxClose = ");
      //    Serial.println(boolTampBoxClose);
    }
  }
  else if ((digitalRead(btnStopPin) == LOW) && (boolTampBoxOpen == false)) 
  {
    while ((digitalRead(btnStopPin) == LOW) && (boolTampBoxOpen == false)) {
      tampStepBox(HIGH);
      CheckTampBoxLimitState();
      //    Serial.print("boolTampBoxOpen = ");
      //    Serial.println(boolTampBoxOpen);
    }
  }
  digitalWrite(stepTampBoxEnablePin, HIGH);
  Serial.println("PositionTB returned");
}

You might even move the limit checking into the step code:

void tampStepBox(int dir) {
  // dir = LOW/0 is toward close HIGH/1 = toward open
  digitalWrite(stepTampBoxDirPin, dir);
  CheckTampBoxLimitState();  // and don't step further into limts
  if ((dir == LOW) && boolTampBoxClose ) return;
  if ((dir == HIGH) && boolTampBoxOpen ) return;
  Serial.print("Limit Switches: ");
  Serial.print(boolTampBoxClose);
  // safe to step:
  digitalWrite(stepTampBoxEnablePin, LOW);
  digitalWrite(stepTampBoxStepPin, HIGH);
  delayMicroseconds(intTampBoxPulseDelay);
  digitalWrite(stepTampBoxStepPin, LOW);
  delayMicroseconds(intTampBoxPulseDelay);
  digitalWrite(stepTampBoxEnablePin, HIGH);
}

void PositionTB() {
  if (digitalRead(btnStartPin) == LOW)  {
    tampStepBox(LOW);
  }
  else if (digitalRead(btnStopPin) == LOW) {
    tampStepBox(HIGH);
  }
  Serial.println("PositionTB returns");
}

This relies on loop() instead of while for the iterating, and checks the limit switches before a each step pulse. The else isn't necessary, but would keep it from stuttering back and forth if you pressed both buttons.

Thank you, DaveX. I will give those a try and let you know! :slight_smile:

DaveX, I LOVE YOU!!!!! Your first example worked! I never imagined that separating the stepping from the logic could have such an effect. I am beginning to understand some of the nuances of coding in this environment. Will definitely keep this example in mind when coding other steppers in the future. Thank you again!!!!

Okay, @DaveX, maybe my enthusiasm was a little premature. When I comment out the

It doesn't work. Very strange, to me at least. I will try the second example and see what happens. Thanks again! Still love ya!

What's not working? Maybe it needs a bit more of a pause between steps -- try a delay instead and see if it just needs more time?

Hi @DaveX, sorry for not being more specific. The stepper works, the limit switches do not. Example 2 works with the Serial.print lines, but without them the stepper stutters and doesn't want to move.

Example 1 changing the delay to 2000, and commenting out the Serial.print lines made no difference to the limit switches not being recognized. And the stepper motor moved fine, both directions.

Thanks,
Lisa

Hi @DaveX ,
Wanted to let you know I nested the while conditions, took out the Serial.print(s) and it works exactly the way I want it to. Hopefully, this will be useful to some else down the road.

Thank you so much for the help!

void PositionTB() {
  CheckTampBoxLimitState();
  if ((digitalRead(btnStartPin) == LOW) && (boolTampBoxClose == false)) {
    while (digitalRead(btnStartPin) == LOW) {
      while (boolTampBoxClose == false) {
        tampStepBox(LOW);
        CheckTampBoxLimitState();
      }
    }
  }
  else if ((digitalRead(btnStopPin) == LOW) && (boolTampBoxOpen == false))
  {
    while (digitalRead(btnStopPin) == LOW) {
      while (!boolTampBoxOpen == false) {
        tampStepBox(HIGH);
        CheckTampBoxLimitState();
      }
    }
  }
  digitalWrite(stepTampBoxEnablePin, HIGH);
}

Hmm--if you need the nested whiles, it points to the possibility that the inner whiles are exiting frequently. I'd wonder how reliable the limit switches are.

It seems perfectly adequate to my limited understanding of the system so: Win!

Hi,
Can I suggest that you read ALL your inputs at the start of the loop, assigning them variable names, then use those variables through the code.
Rather than reading the same input many times through the code.

In other words take a snapshot of your inputs, loop and act , then take another snapshot loop etc.

Tom... :grinning: :+1: :coffee: :australia:

I was still thinking about the limit switches and their possible unreliability. Since they are analog, there is opportunity for noise. If you still want to do tweaking on it beyond being solved, you could use some hysteresis and make them trigger on and off more reliably for the processes that make use of them.

I'm not sure what happens at the extremes with tbreading >400 or tbreading < 10 but you could try:

void CheckTampBoxLimitState() {
  // This is an analog input so we are looking for a range of values
  // The globals boolTampBoxOpen & boolTampBoxClose remember state 
  int tbreading = analogRead(swtTampBoxLimitPin)
  // 5 count Hysteresis on TampBoxOpen limit between 145 and 150
  if (boolTampBoxOpen == false && tbreading >= 150 && tbreading <= 400) {
    boolTampBoxOpen = true;
  }
  if (boolTampBoxOpen && tbreading < 145) {
    boolTampBoxOpen = false;
  }
  // 5 count Hysteresis on TampBoxClose limit between 40 and 45
  if (boolTampBoxClose == false && tbreading >= 10 && tbreading <= 40) {
    boolTampBoxClose = true; 
  }
  if(boolTampBoxClose == true && tbreading > 45) {
    boolTampBoxClose = false; 
  }
}

Hysteresis is a way to debounce interpretation of analog signals. For boolTampBoxOpen, it still turns on at the same limit, but it stays latched on until tbreading moves significantly away from where it was turned on.

If analog noise in analogRead(swtTampBoxLimitPin) was causing the workarounds in PositionTB, filtering out the noise with a deadband/hysteresis could help simplify things.

But if things are working fine, don't worry about it.