However I have the following problem with this routine:
the interrupt can set "eStop" anywhere during the code execution but will only it will only be effective once the loop restarts.
Is there are way, whether implemented in the interrupt or elsewhere, that when the interrupt is triggered the loop is reset to the top ( if(eStop ==0) ) to stop all execution?
If a different approach is possible, I'm open to suggestions!
If your goal is to actually implement an "emergency stop" then you are not just barking up the wrong tree you are hunting in the wrong forest.
Your solution should not involve any software nor any decisions (like the "if" in your example) and should use the minimum possible number of parts to reach the goal.
the intent is to recognise that there was an eStop request and stop the code there and then. Since the arduino is connected to a PC, killing power to the board is not really an option.
Currently and eStop button will stop the physical process when pressed but the code keeps running.
Implementing my initial post works to detect the eStop request, but it still needs to completed the loop before detecting the eStop set
What I had in mind what something like to safely stop the code:
On intr, eStop = 1 -> safely stop code then Goto 0 (which the start of the loop)
Maybe put it to sleep? Of course, this would shut down any display peripherals, possibly making it hard to diagnose the cause.
An alternative might be to call a function (often) which monitors the E-stop input. When it's activated the code just loops there, think 'while()', until the E-stop is removed.
Lets define an emergency stop as opposed to an error condition. An "emergency" stop is when someone removes a safety cover and has their arm caught in a chain driven gear. An error condition is when the machine is filling cans with tomatoes and the cans jam and the tomatoes are spilling on the floor.
Which condition are you attempting to cover? If it is really an emergency, then the advice to stop all power immediately is the correct answer.
sherzaad:
That's EXACTLY what I want - "Arduino that detects when the big red button is triggered - but only AFTER the safety stop has happened."
I what I am trying to achive to to stop the CODE safely to avoid it returning/reading undesired values the moment the e-stop is pressed.
Obviously cutting off power to the Arduino as well as to the dangerous machine is one option.
If you want the Arduino to continue working but to be aware that the E-stop has been triggered then you need some means for the Arduino to detect that.
As you have not given us any information about what you are controlling, how the E-stop button is wired etc etc etc any advice would be guesswork and might be dangerous.
I'm beginning to think that my english is not transparent enough....
@ Robin,
please allow me to re-summerise my query below.
As the PC station is away from the rig, what I am trying to achieve is to notify to the PC that eStop has been pressed that any further data received (ie until the loop restarts if a Goto 0 is really not possible) is invalid
The eStop button kills the power to my rig as should any eStop button.
the arduino board is connected, powered and communicating with a PC and therefore will not power down when the eStop is pressed (unless I do what UKHeliBob suggested which is not really my preferred option)
I would like to implement an INTERRUPT to detect the eStop condition, so that the arduino knows that an eStop has happened and that, where ever it is in "loop", whatever data it is receiving is now invalid
To avoid me adding many "if" statements checking the eStop variable in my loop surely there much be a way of implementing a "Goto 0" which the start of the loop so that I have only ONE if statement that checks the eStop to decide whether or not to run the "normal" code
As to your English, I think a lot of the confusion would have been avoided if your Original Post had said
I have a project on which I would like to detect an emergency stop.
rather than
I have a project on which I would like to implement an emergency stop.
As to your English, I think a lot of the confusion would have been avoided if your Original Post had said
I have a project on which I would like to detect an emergency stop.
rather than
I have a project on which I would like to implement an emergency stop.
...R
Thank you for highlighting to me the confusing point. I've amended the title.
I know that should work in principle.
Where the challenge lies is the second part to my query: Is possible to reset the loop to the start the moment the interrupt is triggered?
sherzaad:
Where the challenge lies is the second part to my query: Is possible to reset the loop to the start the moment the interrupt is triggered?
It seems to me that the pseudo code you posted will do exactly that - unless, of course, the code that is being executed at the time the interrupt is triggered takes a long time to complete before returning to loop(). Code should be designed to return to loop() very quickly even if there is no interrupt being used.
Have a look at the code in Several things at a time. Note how each function runs very briefly and returns to loop() so the next one can be called. And there may be dozens of calls to a function before it is actually time for it to do anything.
If these ideas do not solve the problem then you need to explain what is happening that needs to stop even more quickly and post the code that you are using.
sherzaad:
the closest idea that I found that I think will fit my purpose I found the software example shown here:
IMHO that is because you are thinking of the wrong type of solution. Why would you need to restart the whole program?
And AFAIK a jmp 0 is not the same as pressing the reset button. If you really do need to restart the Arduino then get the E-stop process to trigger the Arduino reset pin using a suitable electrical circuit.