#include <Arduino.h>
#include <EEPROM.h>
#include "Variables.h"
void setup() {
// set buttons as input
pinMode(BUTTON1, INPUT_PULLUP);
pinMode(BUTTON2, INPUT_PULLUP);
pinMode(BUTTON3, INPUT_PULLUP);
pinMode(BUTTON4, INPUT_PULLUP);
pinMode(BUTTON5, INPUT_PULLUP);
// set relays as output
pinMode(RELAY1_PIN, OUTPUT);
pinMode(RELAY2_PIN, OUTPUT);
pinMode(RELAY3_PIN, OUTPUT);
pinMode(RELAY4_PIN, OUTPUT);
pinMode(RELAY5_PIN, OUTPUT);
// set learn LED as output
pinMode(LEARN_LED_PIN, OUTPUT);
}
void loop() {
handleButtons(); // handle the button events including single press and both press detection
if (learn_mode_enable) { // check the learn mode status
blinkLearnLED(); // start blink the learn LED
if (program_slot == 0) { // check the program slot last value, if the value is 0, then select the program slots depending on buttons status
if (button1_pressed) {
program_slot = 1; // slot 1 is selected
button1_pressed = false; // reset button 1 is press status
} else if (button2_pressed) {
program_slot = 2; // slot 2 is selected
button2_pressed = false; // reset button 2 is press status
} else if (button3_pressed) {
program_slot = 3; // slot 3 is selected
button3_pressed = false; // reset button 3 is press status
} else if (button4_pressed) {
program_slot = 4; // slot 4 is selected
button4_pressed = false; // reset button 4 is press status
} else if (button5_pressed) {
program_slot = 5; // slot 5 is selected
button5_pressed = false; // reset button 5 is press status
}
} else {
// check if both buttons (button 1 & button 3) is press
if (both_button_pressed) {
if (program_slot != 0) {
saveRelayChanges(program_slot); // save the last relays configuration
}
program_slot = 0; // set the program slot value is 0
learn_mode_enable = false; // exit from the learn mode
// set all relays status value is 0
relay1_status = false;
relay2_status = false;
relay3_status = false;
relay4_status = false;
relay5_status = false;
digitalWrite(LEARN_LED_PIN, LOW); // turn off the learn LED
both_button_pressed = false; // reset the both button pressed status
}
// check the buttons individual status to set the relays status for save in the slot
if (button1_pressed) {
relay1_status = changeRelayState(RELAY1_PIN); // change the relay 1 state value
button1_pressed = false;
} else if (button2_pressed) {
relay2_status = changeRelayState(RELAY2_PIN); // change the relay 2 state value
button2_pressed = false;
} else if (button3_pressed) {
relay3_status = changeRelayState(RELAY3_PIN); // change the relay 3 state value
button3_pressed = false;
} else if (button4_pressed) {
relay4_status = changeRelayState(RELAY4_PIN); // change the relay 4 state value
button4_pressed = false;
} else if (button5_pressed) {
relay5_status = changeRelayState(RELAY5_PIN); // change the relay 5 state value
button5_pressed = false;
}
}
// check both button press
if (both_button_pressed) {
turnOffAll();
digitalWrite(LEARN_LED_PIN, LOW);
learn_mode_enable = false;
both_button_pressed = false;
}
} else {
// check again both button pressed
if (both_button_pressed) {
turnOffAll();
learn_mode_enable = true;
both_button_pressed = false;
}
// load relay configuration
if (button1_pressed) {
if (button1_slot_toggle) {
turnOffAll();
button1_slot_toggle = false;
button2_slot_toggle = false;
button3_slot_toggle = false;
button4_slot_toggle = false;
button5_slot_toggle = false;
} else {
loadRelayChanges(1);
button1_slot_toggle = true;
}
button1_pressed = false;
} else if (button2_pressed) {
if (button2_slot_toggle) {
turnOffAll();
button1_slot_toggle = false;
button2_slot_toggle = false;
button3_slot_toggle = false;
button4_slot_toggle = false;
button5_slot_toggle = false;
} else {
loadRelayChanges(2);
button2_slot_toggle = true;
}
button2_pressed = false;
} else if (button3_pressed) {
if (button3_slot_toggle) {
turnOffAll();
button1_slot_toggle = false;
button2_slot_toggle = false;
button3_slot_toggle = false;
button4_slot_toggle = false;
button5_slot_toggle = false;
} else {
loadRelayChanges(3);
button3_slot_toggle = true;
}
button3_pressed = false;
} else if (button4_pressed) {
if (button4_slot_toggle) {
turnOffAll();
button1_slot_toggle = false;
button2_slot_toggle = false;
button3_slot_toggle = false;
button4_slot_toggle = false;
button5_slot_toggle = false;
} else {
loadRelayChanges(4);
button4_slot_toggle = true;
}
button4_pressed = false;
} else if (button5_pressed) {
if (button5_slot_toggle) {
turnOffAll();
button1_slot_toggle = false;
button2_slot_toggle = false;
button3_slot_toggle = false;
button4_slot_toggle = false;
button5_slot_toggle = false;
} else {
loadRelayChanges(5);
button5_slot_toggle = true;
}
button5_pressed = false;
}
}
}
Button 1
Button 2
Button 3
Button 4
Button 5
Relay 1
Relay 2
Relay 3
Relay 4
Relay 5
Button 1 & Button 3
Button 1 Connector Switch
Button 2 Connector Switch
Learn LED