Limit switch used as deadman switch

I'm really new to programming so bear with me. I have my program running exactly how I want it to. All I'm trying to do now is add a sort-of deadman switch that stops the loop if it is not depressed. Basically there's a tray with a limit switch on it. If the tray is pushed in, the program will run as normal. If it is not pushed in, the loop will not run. Everything I've searched for so far has been slightly different than I want and since I'm new to this, I don't know how to make that conversion.

Oh, and to clarify, I don't want the power cut to the board. If I wanted that I would just run the power supply through the switch. I only want the loop stopped. It seems like it would be a simple thing, but I'm just not finding it. Please help.

You can stop loop() with this line:

while(1);

... and please post your code, so your question isn't so abstract.

But you really don't want to "stop the loop" but rather, not do what the normal loop does while the tray is not pushed in...

void loop() {
  if ( digitalRead(switchPin) == HIGH ) {
    // tray is not in place, do nothing
    delay(250);  // avoid debounce
    return;  // run loop again
  }
  // rest of "normal" code here
  ...
}

Yeah, I put a wedge in a little device I made rather than do it “correctly”… which would have required understanding code from only a decade ago:

while (digitalRead(pauseSwitch))
    ;

It’s not pretty but it surely stops. Everything.

Find a good place for that in your flow, you all set.

Promise yourself to do it differently one day. I’m about 4 years down the road, still intend to improve that and a buncha other things.

Life too short, it works.

Oh, if you need a hair trigger stop and don’t start up:

 if (digitalRead(panicSwitch))
     while (1)
          ;

Which will need a power cycle or reset.

a7

Actually, I might have just thought an "analog" way of doing this. This thing I'm making has a momentary button that when pressed starts the loop. I can just run this button through the limit switch so that the button can't start that loop unless the tray is pushed in.

Please eliminate any guesswork on the part of readers here, and post your entire sketch.

Could this "tray" cause any serious injury if it operates incorrectly? If so, you need a lot more than a programmed limit switch to provide adequate human safety.

Hello
A schematic and picture would be immens helpfull too

But then it would no longer be a dead man switch, as the loop will run after the button has pressed and your switch would no longer affect it. It may work for your application, but it does show your description of the desired functionality isn't complete.

#include <Servo.h>

Servo myservo;  // create servo object to control a servo 
#define servoPin 3 //~
#define pushButtonPin 2                
 
 int angle =90;    // variable to store the servo position 
 int angleStep =10;
 const int minAngle = 0;
 const int maxAngle = 180;

 const int type =1;

 int buttonPushed =0;
 
void setup() {
  // Servo button
  Serial.begin(9600);
  myservo.attach(servoPin);
  pinMode(pushButtonPin, INPUT_PULLUP);
   Serial.println("Servo Button");
   myservo.write(angle);

}

void loop() {
 if (digitalRead(pushButtonPin) == LOW){
  buttonPushed = 1;
 }

  if ( buttonPushed ){
  angle = angle + angleStep;

   if (angle >= maxAngle) {
    angleStep = -angleStep;
     if(type ==1)
     {
      buttonPushed =0;
     }
   }

   if (angle <= minAngle) {
    angleStep = -angleStep;
     if(type ==2)
     {
       buttonPushed =0;
     }
   }
   myservo.write(angle);
    Serial.print("Moved to: ");
    Serial.print(angle);
    Serial.println(" degree");
  delay(100);
  } 
}

My analog solution should still work because the button only runs the desired function once and has to be pressed again in order to run again. But here's my current code for ya'll reference.

Not so - if you hold the button down, the "function" will run every 100 ms.