Arduino UNO EMERGENCY STOP BUTTON

Hi everyone i am quite new to arduino.
i was wondering if i can create a emergency stop button in a void loop() with a lot of while nested without using #interrupt , but with goto done; maybe? The stop button ( s2) has to allow the whole board to return to its initial state .

done:
digitalWrite(ledgreen, LOW);
digitalWrite(mm1, LOW);
digitalWrite(mm2, LOW);
digitalWrite(m21, LOW);
digitalWrite(m12,LOW);
digitalWrite ( heater,LOW);
digitalWrite(fan, LOW);
digitalWrite(act, LOW);
digitalWrite(ledred, HIGH);

startState= digitalRead(start);

while(startState== LOW)

{

digitalWrite(ledgreen,HIGH);
digitalWrite(ledred,LOW);

conveyor1F();

s1state= digitalRead(s1);

while (s1state==LOW)

{

digitalWrite(act, HIGH);
digitalWrite(m1, LOW);
digitalWrite(m2, LOW);
}
}

This is an example of my code. Will not be posting the whole code !
would appreciate help!

In an emergency turn off the power to the Arduino or the errant hardware causing there to be the emergency. Do not rely on a software solution.

Hello! thanks for the quick reply. This is purely for my project as it requires it to have an E-Stop button ( s2) in the programe!

If you really must have a stop button then turn your program into a state machine using switch/case with a different case for each state and eliminate the while loops. Then you can check the emergency stop button at the start of each pass through loop()

How would an interrupt help? Just add code in each of your "while" loops to check for the switch and if "on" return.

Perhaps a better approach would have been to consider the emergency start switch at the beginning of the program design.

Paul

hi all thanks for the reply. i was told to only use goto and break if that helps..

bob112112:
hi all thanks for the reply. i was told to only use goto and break if that helps..

Who told you that? Sounds like they stopped learning with BASIC.

Paul

For a truly emergency stop button, you want something more rigorous than a software solution, ie, so it can still stop everything even if you managed to hang the microcontroller (eg, due to power glitch or software bug). Could a latching button tied to reset be used? (with pullup/pulldown resistors connected externally to ensure that when the board is in reset state, the pins are held in the "safe" configuration?

If it doesn't need to be that rigorous, can't you just poll it in your while loop? Or do those functions you call (which you didn't inlude the code for) have delays in them or otherwise take a long time to execute? If it needs to be more responsive, or if you can't get away from delays or blocking code, use interrupts.

Goto and break are not the right approach.

Assuming you are NOT talking about an emergency that can cause death or injury then have a look at how the code is organized in Several Things at a Time and in Planning and Implementing a Program

Note how all of the functions return very quickly even if it takes many calls to the function before its action can complete. That allows any button press to be detected in the middle of other things happening.

...R

bob112112:
The stop button ( s2) has to allow the whole board to return to its initial state

Sounds like the reset button. Wire a switch from the reset pin to ground. Done.

1 Like

DrAzzy:
For a truly emergency stop button, you want something more rigorous than a software solution, ie, so it can still stop everything even if you managed to hang the microcontroller (eg, due to power glitch or software bug). Could a latching button tied to reset be used? (with pullup/pulldown resistors connected externally to ensure that when the board is in reset state, the pins are held in the "safe" configuration?

If it doesn't need to be that rigorous, can't you just poll it in your while loop? Or do those functions you call (which you didn't inlude the code for) have delays in them or otherwise take a long time to execute? If it needs to be more responsive, or if you can't get away from delays or blocking code, use interrupts.

Goto and break are not the right approach.

void conveyor1F ()
{
digitalWrite(conm1, HIGH);
digitalWrite(conm2, HIGH);
delay(delayTime);
//position 1
digitalWrite(conm1,HIGH);
digitalWrite(conm2, LOW);
delay(delayTime);
//pos2
digitalWrite(conm1,LOW);
digitalWrite(conm2, LOW);
delay(delayTime);
//pos3
digitalWrite(conm1,LOW);
digitalWrite(conm2, HIGH);
delay(delayTime);
//pos 4
}

void conveyor2F ()
{
digitalWrite(connm1, HIGH);
digitalWrite(connm2, HIGH);
delay(delayTime);
//position 1
digitalWrite(connm1,HIGH);
digitalWrite(connm2, LOW);
delay(delayTime);
//pos2
digitalWrite(connm1,LOW);
digitalWrite(connm2, LOW);
delay(delayTime);
//pos3
digitalWrite(connm1,LOW);
digitalWrite(connm2, HIGH);
delay(delayTime);
//pos 4
}

void conveyor2R ()
{
digitalWrite(connm1, HIGH);
digitalWrite(connm2, HIGH);
delay(delayTime);
//position 1

digitalWrite(connm1,LOW);
digitalWrite(connm2, HIGH);
delay(delayTime);
//pos 4

digitalWrite(connm1,LOW);
digitalWrite(connm2, LOW);
delay(delayTime);
//pos3
digitalWrite(connm1,HIGH);
digitalWrite(connm2, LOW);
delay(delayTime);
//pos2
}
these are the subroutines i use , very basic . To control a stepper motor
As for the goto, i am not to sure as my project requires me to only use goto() and not interrupt..

Your project does not require goto, and it certainly doesn't require or even want delay().

Please remember to use code tags when posting code.

1 Like

bob112112:
these are the subroutines i use , very basic .

You need to post the complete program - and please post your program using the code button </> so it looks like this. See How to use the Forum

Have you studied the links I gave you?
Also look at the second example in this Simple Stepper Code which does not use delay()

...R
Stepper Motor Basics

my project requires me to only use goto()

Does it require you to use goto or is it that you are allowed to use goto ?

What is it that you are trying to achieve with this project ?