#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;
}
}
}
nano:12
nano:11
nano:10
nano:9
nano:8
nano:7
nano:6
nano:5
nano:4
nano:3
nano:2
nano:GND.2
nano:RESET.2
nano:0
nano:1
nano:13
nano:3.3V
nano:AREF
nano:A0
nano:A1
nano:A2
nano:A3
nano:A4
nano:A5
nano:A6
nano:A7
nano:5V
nano:RESET
nano:GND.1
nano:VIN
nano:12.2
nano:5V.2
nano:13.2
nano:11.2
nano:RESET.3
nano:GND.3
led6:A
led6:C
relay1:VCC
relay1:GND
relay1:IN
relay1:NC
relay1:COM
relay1:NO
relay2:VCC
relay2:GND
relay2:IN
relay2:NC
relay2:COM
relay2:NO
relay3:VCC
relay3:GND
relay3:IN
relay3:NC
relay3:COM
relay3:NO
relay4:VCC
relay4:GND
relay4:IN
relay4:NC
relay4:COM
relay4:NO
relay5:VCC
relay5:GND
relay5:IN
relay5:NC
relay5:COM
relay5:NO
r1:1
r1:2
r2:1
r2:2
r3:1
r3:2
r4:1
r4:2
r6:1
r6:2
btn1:1.l
btn1:2.l
btn1:1.r
btn1:2.r
btn2:1.l
btn2:2.l
btn2:1.r
btn2:2.r
btn3:1.l
btn3:2.l
btn3:1.r
btn3:2.r
btn4:1.l
btn4:2.l
btn4:1.r
btn4:2.r
btn5:1.l
btn5:2.l
btn5:1.r
btn5:2.r
sw1:1
sw1:2
sw1:3
sw2:1
sw2:2
sw2:3
btn6:1.l
btn6:2.l
btn6:1.r
btn6:2.r
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