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();
}