Can I use a Pin for 2 ISRs ?

I am trying to lock and unlock a robot arm, it has to be fast...

The digitalRead, sucks so I am trying an ISR

Pretty simple:

attachInterrupt(PushToSet_Pin, RELEASE_ISR, FALLING); // PUSH BUTTON -- Release Arm
attachInterrupt(PushToSet_Pin, LOCK_ISR, RISING); // Release BUTTON -- LOCK Arm

} //END SETUP

void RELEASE_ISR()
{
Message = "ARM FREE ";
digitalWriteFast (AllBrakePin, 0);
ShowEncBool = 1;
Serial.println ("RELEASE");
}
void LOCK_ISR()
{ Message = "ARM LOCKED"; // goes to Tablet
digitalWriteFast (AllBrakePin, 1);
ShowEncBool = 0;
Serial.println ("LOCK");
}

I would like the arm to stay UN locked while the button is pressed, (Logic state 0)

I ran it and it only goes in the LOCK mode,

What to do?

Thanks

Use direct port access for fastest IO, but that won't help in your case. The microsecond or so that digitalRead takes is probably 1000x - 10000x faster than your robot arm lock.

You can attach only one interrupt to a pin, and you should never print or send messages from an ISR.

An Arduino can do 16 million things in a second. Checking a digitalRead() for a button can be done thousands of times if you code it correctly.

Interrupts are not for buttons.

Try using one ISR, changing the mode to CHANGE, and have the ISR deduce the RISING/FALLLING by reading the pin.

Serial.print() needs interrupts to work.
An ISR disables interrupts so that the interrupt can't be interrupted.
Can you set a flag, and use that when the ISR ends to send a message to the tablet and also do the Serial.print()?

I like the Rising and Falling interrupts. I think I did something in a prior project to sync up with a falling or rising edge of a camera clock.

const byte PushToSet_Pin = 2;
const byte AllBrakePin = 3;


const char * Message = NULL;
bool ShowEncBool;


void setup()
{
  Serial.begin(115200);
  attachInterrupt(digitalPinToInterrupt(PushToSet_Pin), ARM_ISR, CHANGE);  // HIGH=LOCK, LOW=RELEASE
}  //END SETUP


// LOCK on HIGH, RELEASE on LOW
void ARM_ISR()
{
  if (digitalRead(PushToSet_Pin))
  {
    //HIGH
    Message = "ARM LOCKED"; // goes to Tablet
    digitalWrite(AllBrakePin, HIGH);
    ShowEncBool = 0;
    Serial.println ("LOCK");
  }
  else
  {
    // LOW
    Message = "ARM FREE  "; // goes to Tablet
    digitalWrite(AllBrakePin, LOW);
    ShowEncBool = 1;
    Serial.println ("RELEASE");
  }
}


void loop() {}

John Wasser,

Tried that and it worked like a charm.

My loop is a lot faster now.

I wonder if you know a reliable way to measure how fast is the loop?

Thanks

  1. Blink a pin on and off in the loop. Use an oscilloscope to measure frequency.

  2. Record the value of micros() then count 10,000 loops and record micros() again. Speed is count divided by time difference.