problems with interrupts
Posted: Thu Jan 21, 2021 8:19 am
Hello everyone, sorry but I have a problem and I have not been able to get out of it for months, I am trying to manage the lights, and I would like to control 8 relays with 8 buttons, the system works but, using the interrupts, I cannot stabilize the program, so if I repeatedly press the buttons the Sketch stops, maybe the problem is my wrong way to use IRAM, the configuration is the following ESP32, mcp23017 and under the Sketch, could someone tell me what am I wrong? thank you very much p.s. sorry my english, i use google translator
Code: Select all
#include <Wire.h>
#include "Adafruit_MCP23017.h"
#include <SPI.h>
byte IntPin = 23; // collegare entrata interrupt mcp A------------------------
volatile boolean awakenByInterrupt = false;
static uint16_t ledState = 0;
boolean led_state[8];
byte pinLed[] = {7, 6, 5, 4, 3, 2, 1, 0};
byte buttons[] = {8, 9, 10, 11, 12, 13, 14, 15};
int i ;
int test;
Adafruit_MCP23017 mcp;
void setup() {
Serial.begin(115200);
mcp.begin();
pinMode(IntPin, INPUT);
for (i = 0; i < (8); i++) {
mcp.pinMode(buttons[i], INPUT);
mcp.pinMode(pinLed[i], OUTPUT);
mcp.digitalWrite(pinLed[i], LOW);
led_state[i] = false;
mcp.setupInterruptPin(buttons[i], RISING);
}
mcp.readGPIOAB();
mcp.setupInterrupts(true, false, LOW);
attachInterrupt(digitalPinToInterrupt(IntPin), intCallBack, FALLING);
}
ICACHE_RAM_ATTR void intCallBack() {
awakenByInterrupt = true;
}
void handleInterrupt() { // gestione pulsanti da interrupt************************************************************************************
noInterrupts();
uint8_t pin = mcp.getLastInterruptPin();
//uint8_t val = mcp.getLastInterruptPinValue();
led_state[pin - 8] = ! led_state[pin - 8];
if (led_state[pin - 8]) mcp.digitalWrite(pinLed[pin - 8], HIGH);
else mcp.digitalWrite(pinLed[pin - 8], LOW);
delay(35);
//mcp.readGPIOAB();
while ( !(!mcp.digitalRead(buttons[0]) && ! mcp.digitalRead(buttons[1]) && !mcp.digitalRead(buttons[2]) && !mcp.digitalRead(buttons[3]) && !mcp.digitalRead(buttons[4]) && !mcp.digitalRead(buttons[5]) && !mcp.digitalRead(buttons[6]) && !mcp.digitalRead(buttons[7])));
awakenByInterrupt = false;
interrupts();
}
void loop() {
if (awakenByInterrupt) handleInterrupt();
}