Call one function inside an ISR interrupt

Hi everyone!

I'm doing a robotic arm, and I'm trying to implement an emergency button. I would like to know if it is possible inside the function of an interruption (the functio that works like an emergency button) to be able to call another function that what it does is to take the robot to a resting position.

At the moment I only have a serial.print (I know it is not the most appropriate) inside the function of the interrupt, that appears on the screen every time I click the button:

ISR(PCINT0_vect) {
  interrupt_state = (PINA & 0x04) >> 1;
  Serial.println("EMERGENCY BUTTON!");
}

And this is the function I wanted to call or put into the function of the interruption:

void go_to_position(int pos1[], int pos2[], int servo)//function
{
  while (pos1[servo] != pos2[servo])
  {
    if (pos2[servo] < pos1[servo])
    {
      dxlSetGoalPosition(SERVO_ID[servo], pos1[servo]);
      pos1[servo]--;
      delayMicroseconds(800);
    }
    else if (pos2[servo] > pos1[servo])
    {
      dxlSetGoalPosition(SERVO_ID[servo], pos1[servo]);
      pos1[servo]++;
      delayMicroseconds(800);
    }
  }
}

Thank you!

Urko.

ISR(PCINT0_vect) {
  interrupt_state = (PINA & 0x04) >> 1;
  Serial.println("EMERGENCY BUTTON!");
}

You are already calling a function from within the ISR, albeit one that you shouldn't use in an ISR because it uses interrupts which are disabled within an ISR. What makes you think that you could not call another one instead ?

Whether it is sensible is another question. Why not flag the fact that the button has become pressed and call your function from loop() ?

Whether it is sensible is another question. Why not flag the fact that the button has become pressed and call your function from loop() ?

And how i could do that?

And how i could do that?

I think that is perfectly obvious. Fire up the IDE, open your sketch, and start typing. Geez, do we have to do ALL of the thinking around here?

It is OBVIOUS that reading the state of a switch, in an ISR, and then setting a flag so that loop will later see that the EMERGENCY STOP switch was pressed, is stupid.

Your EMERGENCY STOP switch should STOP the robot RIGHT NOW. In other words, the Arduino should NOT be involved in reading the emergency stop switch. The emergency stop switch should NOT make the robot curl up in a fetal position. It should make the robot STOP!

Some serious rethink of what you are trying to do is in order BEFORE you write any more code.

PaulS:
Your EMERGENCY STOP switch should STOP the robot RIGHT NOW. In other words, the Arduino should NOT be involved in reading the emergency stop switch. The emergency stop switch should NOT make the robot curl up in a fetal position. It should make the robot STOP!

What @PaulS said.

E-stops are not implemented in software they are implemented with physical wires and hard-contact relays and switches. Think about it. If the Arduino is operating the arm and something goes goofy in the code there's no guarantee, in fact the opposite is true, that the Arduino is going to scan and execute the code which implements your E-stop. You'd be relying on a failed program to insure someone's or something's safety.

What you can do is *monitor * the E-stop circuit, via an auxiliary contact on the relay which cuts I/O power, in the Arduino. That way the Arduino can respond to the emergency condition by adjusting the program to the machine state. When the fault is cleared the Arduino can then restart from a controlled condition.

However if you are doing something in software you should use the highest priority non-maskable interrupt in the
system, as that can always be externally triggered (but the ISR itself shouldn't depend on the state
of the RAM, but just shut the hardware down directly).

Most Arduino's don't have a non-maskable interrupt channel I believe.

Let's not call what you want an "emergency stop" button. Let's call it a "reset" button.

If all you want to do is stop the robot with an interrupt and not do anything else in your program or use the interrupt for anything else, then it is fine to call functions from the interrupt function. Just don't expect the interrupt function to be used in your shut-down code.

Paul