Hi! I want to have two interrupts that are triggered by button click:
- interrupt 2 will be always attached and their isr will attach interrupt 1
- interrupt 1 will be attached at the beginning but its ISR will deatach and it couldn’t be called until interrupt2’s ISR is executed
In practice ISR2 is kind of a reset for ISR1.
The problem i am having is in this flow:
btn1 clicked → ISR1 executed → btn2 clicked → ISR1 executed → ISR2 executed
here is a code i was testing with:
#include <Arduino.h>
#define INTERRUPT_ONE_PIN 6
#define INTERRUPT_TWO_PIN 4
// put function declarations here:
void interruptOne();
void interruptTwo();
volatile uint8_t state = 3;
volatile uint8_t called = 0;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
while (!Serial);
pinMode(INTERRUPT_ONE_PIN, INPUT_PULLDOWN);
pinMode(INTERRUPT_TWO_PIN, INPUT_PULLDOWN);
attachInterrupt(digitalPinToInterrupt(INTERRUPT_ONE_PIN), interruptOne, FALLING);
attachInterrupt(digitalPinToInterrupt(INTERRUPT_TWO_PIN), interruptTwo, FALLING);
}
void loop() {
// put your main code here, to run repeatedly:
uint8_t localState = state;
if (localState == 0)
{
Serial.println("state is 0");
Serial.print("called is");
Serial.println(called);
state = 3;
called = 0;
}
if (localState == 1)
{
Serial.println("state is 1");
Serial.print("called is");
Serial.println(called);
state = 3;
}
}
void interruptOne() {
detachInterrupt(digitalPinToInterrupt(INTERRUPT_ONE_PIN));
state = 0;
called = 1;
}
void interruptTwo() {
state = 1;
attachInterrupt(digitalPinToInterrupt(INTERRUPT_ONE_PIN), interruptOne, FALLING);
}