const int interruptPin2 = 2; // Interrupt pin D2
const int interruptPin3 = 3; // Interrupt pin D3
const int interruptPin18 = 18; // Interrupt pin D18
const int interruptPin19 = 19; // Interrupt pin D19
const int interruptPin20 = 20; // Interrupt pin D20
const int interruptPin21 = 21; // Interrupt pin D21
volatile bool interruptTriggered = false;
volatile int triggeredPin = -1; // Store which pin triggered the interrupt
void setup() {
Serial.begin(9600);
// Set interrupt pins as input with pull-up resistors
pinMode(interruptPin2, INPUT_PULLUP);
pinMode(interruptPin3, INPUT_PULLUP);
pinMode(interruptPin18, INPUT_PULLUP);
pinMode(interruptPin19, INPUT_PULLUP);
pinMode(interruptPin20, INPUT_PULLUP);
pinMode(interruptPin21, INPUT_PULLUP);
// Attach interrupts
attachInterrupt(digitalPinToInterrupt(interruptPin2), handleInterrupt2, FALLING);
attachInterrupt(digitalPinToInterrupt(interruptPin3), handleInterrupt3, FALLING);
attachInterrupt(digitalPinToInterrupt(interruptPin18), handleInterrupt18Func, FALLING);
attachInterrupt(digitalPinToInterrupt(interruptPin19), handleInterrupt19Func, FALLING);
attachInterrupt(digitalPinToInterrupt(interruptPin20), handleInterrupt20Func, FALLING);
attachInterrupt(digitalPinToInterrupt(interruptPin21), handleInterrupt21Func, FALLING);
}
void loop() {
static unsigned long previousMillis = 0; // Store last time
unsigned long currentMillis = millis(); // Get the current time
if (currentMillis - previousMillis >= 1000) { // Check for 1 second interval
previousMillis = currentMillis; // Update last time
Serial.println("Non-blocking delay - Loop continues.");
}
// Print which pin triggered the interrupt if set
if (interruptTriggered) {
interruptTriggered = false; // Reset the flag
printTriggeredPin();
}
delay(5000);
}
// Interrupt handler functions for each pin
void handleInterrupt2() {
interruptTriggered = true; // Set interrupt flag
triggeredPin = interruptPin2; // Store the pin that triggered the interrupt
Serial.println("2");
}
void handleInterrupt3() {
interruptTriggered = true; // Set interrupt flag
triggeredPin = interruptPin3; // Store the pin that triggered the interrupt
}
void handleInterrupt18Func() {
interruptTriggered = true; // Set interrupt flag
triggeredPin = interruptPin18; // Store the pin that triggered the interrupt
}
void handleInterrupt19Func() {
interruptTriggered = true; // Set interrupt flag
triggeredPin = interruptPin19; // Store the pin that triggered the interrupt
}
void handleInterrupt20Func() {
interruptTriggered = true; // Set interrupt flag
triggeredPin = interruptPin20; // Store the pin that triggered the interrupt
}
void handleInterrupt21Func() {
interruptTriggered = true; // Set interrupt flag
triggeredPin = interruptPin21; // Store the pin that triggered the interrupt
Serial.println("21");
}
void printTriggeredPin() {
switch (triggeredPin) {
case 2:
Serial.println("Interrupt triggered on Pin D2");
break;
case 3:
Serial.println("Interrupt triggered on Pin D3");
break;
case 18:
Serial.println("Interrupt triggered on Pin D18");
break;
case 19:
Serial.println("Interrupt triggered on Pin D19");
break;
case 20:
Serial.println("Interrupt triggered on Pin D20");
break;
case 21:
Serial.println("Interrupt triggered on Pin D21");
break;
default:
Serial.println("Unknown interrupt pin");
break;
}
}