#include "OneButton.h" //we need the OneButton library
OneButton button(A0, true); //attach a button on pin A0 to the library
#define RELAY_PIN_1 2
int relayState1 = LOW;
#define RELAY_PIN_2 3
int relayState2 = LOW;
#define RELAY_PIN_3 4
int relayState3 = LOW;
void setup() {
pinMode(RELAY_PIN_1, OUTPUT); // sets the digital pin as output
pinMode(RELAY_PIN_2, OUTPUT); // sets the digital pin as output
pinMode(RELAY_PIN_3, OUTPUT); // sets the digital pin as output
button.attachDoubleClick(onDoubleClick); // link the function to be called on a doubleclick event.
button.attachClick(onSingleClick); // link the function to be called on a singleclick event.
button.attachLongPressStop(onLongClick); // link the function to be called on a longpress event.
}
void loop() {
button.tick(); // check the status of the button
delay(10); // a short wait between checking the button
}
void onDoubleClick() { // what happens when button is double-clicked
toggleRelayState(RELAY_PIN_1, relayState1);
}
void onSingleClick() { // what happens when the button is clicked
toggleRelayState(RELAY_PIN_2, relayState2);
}
void onLongClick() { // what happens when buton is long-pressed
toggleRelayState(RELAY_PIN_3, relayState3);
}
void toggleRelayState(int relayPin, int &relayState) {
relayState = ~relayState;
digitalWrite(relayPin, relayState);
delay(250);
// turn off the blue Relay
}