The SX1509 provides an interrupt signal to indicate when a key-down is detected. However, when the readKeypad() method is called inside an interrupt handler a “core panic” occurs.
Is this expected, or is it potentially a problem with my Arduino platform?
For MCUs it’s generally not recommended to perform I/O operations (especially those involving communication buses) directly within interrupt handlers. This can cause conflicts with other interrupts (in the i2c or SPI bus) or exceed its timing constraints
Instead, use the interrupt as a simple flag setter and then in your main loop, check this flag and call readKeypad() when needed
Example:
volatile bool keypressDetected = false;
void interruptHandler() {
keypressDetected = true; // Just set a flag, do nothing else
}
void loop() {
if (keypressDetected) {
uint16_t keyData = readKeypad(); // Read keypad data in main loop
// Process key data...
keypressDetected = false; // Reset flag
}
// Rest of your main loop
}
This way, the interrupt handler simply sets a flag, and the main loop handles the key press, avoiding complex operations within the interrupt context (common in embedded systems to avoid the exact “core panic” issue you’re experiencing)
While it looks like that is what i will need to do, I would prefer to allow for the possibility of multiple interrupts between “loop” calls in order to support complex situations.
I hadn’t thought of that call as an I/O operation, but it makes sense that it is a bus I/O operation.
I had hoped to add the Key-Data to a list to be processed when the “loop” got around to it.
Thanks for the prompt answer.