Go Down

Topic: Interrupt (Read 117 times) previous topic - next topic

Ashesh132

I have a program. I have included a button which acts as an EMERGENCY STOP. When pressed, it should stop all the program and display "EMERGENCY STOP" on LCD.

But the LCD printing does not happen in the interrupt function. Code given below -


Code: [Select]


volatile boolean E_Stop = false;

void setup()
{
...Code
   attachInterrupt(0, Emergency_Stop, RISING);

}

void loop()
{
...Code
}

void Emergency_Stop()
{
  if (!E_Stop)
  {
    E_Stop = true;

    while (true)
    {
      digitalWrite(12, HIGH); //works fine
      lcd.clear(); //doesnt work in interrupt
      lcd.print("EMERGENCY STOP"); //doesnt work in interrupt
    }

  }
}




PaulRB

You should not use an interrupt for this. Just use digitalRead().

Robin2

I wonder if your short program represents the entirety of what you want to happen when the Emergency Stop button is pressed.

If the eventual purpose is to shut off something that is potentially dangerous then NO computer code should be involved. The Emergency Stop button should directly cut off the power and only as a secondary feature inform the Arduino that it has done so.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

DrDiettrich

Your attempt to never leave the ISR is dangerous. It can prevent e.g. interrupt driven output of messages and other things (time...). For shut down of a device it may be required to move the actuators into defined positions, what can require some time and interrupts for actuator control.

GolamMostafa

#4
Jul 23, 2018, 02:18 pm Last Edit: Jul 23, 2018, 02:20 pm by GolamMostafa
@OP

If it is just an exercise, you execute this instruction interrupts(); after arriving at the ISR to get your lcd related functions to work? (untested)

MarkT

#5
Jul 23, 2018, 02:23 pm Last Edit: Jul 23, 2018, 02:25 pm by MarkT
Use of an ISR that never exits for an energency stop is fine, if and only if all the actions it
needs to do to park hardware in a safe state are doable with the rest of the system frozen.

Writing to the LCD doesn't work in the ISR, so that action (which isn't safety critical) should
be deferred to the main loop which looks for an emergency stopped flag set by the ISR perhaps?
The whole of the rest of the code would then need to cooperate with the emergency stop
protocol - perhaps better to light up an LED from the ISR directly?

The advantage of the ISR is that it is guaranteed to work in a small microcontroller by the hardware
(assuming certain constraints in the other interrupts flying around), which is better than relying
on the rest of the software never crashing.
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]

MarkT

@OP

If it is just an exercise, you execute this instruction interrupts(); after arriving at the ISR to get your lcd related functions to work? (untested)
That might very well allow the rest of the program to restart the thing you were trying to stop...
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]

Go Up