#include <PCF8574.h>
#include <Wire.h>
PCF8574 pcf8574a(0x24);
PCF8574 pcf8574b(0x21);
// uncomment the applicable line if using Uno or ESP8266
#define intPin D3 // interrupt input Uno
//#define intPin D4 // interrupt input NodeMCU ESP8266
volatile bool buttonPressed = false; // button interrupt flag
byte buttonReg; // button read register
void setup() {
// initialize PCF8574 with an interrupt pin and set all outputs to '1' (Relays off)
Wire.begin();
pinMode(intPin, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(intPin), buttonISR, FALLING);
pcf8574a.begin(0xFF); // turn off all Relays
}
void loop() {
// if a button press was detected via interrupt, check if the button press
// occurred on one of the "toggle" control buttons and toggle the relay if so
if (buttonPressed) {
delay(50); // crude debounce
buttonReg = pcf8574b.read8();
// toggle a relay if button pressed
if (!bitRead(buttonReg, 0)) {
pcf8574a.toggle(1);
}
// toggle a relay if button pressed
if (!bitRead(buttonReg, 1)) {
pcf8574a.toggle(2);
}
}
// clear interrupt flag
buttonPressed = false;
}
// interrupt service routine
void buttonISR() {
buttonPressed = true;
}