#include <Bounce2.h>
#define NUM_RELAYS 3
#define INITIAL_STATE LOW // Initial state = HIGH to active LOW relay model.
struct INFOS
{
const byte relayPin;
bool relayState;
const byte buttonPin;
};
INFOS info[NUM_RELAYS] =
{
{2, INITIAL_STATE, 18}, // Relay 0 pin, relay 0 state, button 0 pin
{4, INITIAL_STATE, 19},
{5, INITIAL_STATE, 21}
};
Bounce2::Button button[NUM_RELAYS] = {Bounce2::Button()};
void printDebug(byte i)
{
Serial.print("Button ");
Serial.print(i);
Serial.print(" pressed, relayState ");
Serial.print(i);
Serial.print(": ");
Serial.println(info[i].relayState);
}
void setup()
{
Serial.begin(9600);
for (byte i = 0; i < NUM_RELAYS; i++)
{
button[i].attach(info[i].buttonPin, INPUT_PULLUP);
button[i].setPressedState(LOW);
button[i].interval(5);
pinMode(info[i].relayPin, OUTPUT);
digitalWrite(info[i].relayPin, info[i].relayState);
}
}
void loop()
{
for (byte i = 0; i < NUM_RELAYS; i++)
{
button[i].update();
if (button[i].pressed())
{
info[i].relayState = !info[i].relayState;
digitalWrite(info[i].relayPin, info[i].relayState);
printDebug(i);
break;
}
}
}