#include <Arduino.h>
#include <EEPROM.h>
#include "Variables.h"

void setup() {
  Serial.begin(115200);
  pinMode(BUTTON1, INPUT_PULLUP);
  pinMode(BUTTON2, INPUT_PULLUP);
  pinMode(BUTTON3, INPUT_PULLUP);
  pinMode(BUTTON4, INPUT_PULLUP);
  pinMode(BUTTON5, INPUT_PULLUP);

  pinMode(RELAY1_PIN, OUTPUT);
  pinMode(RELAY2_PIN, OUTPUT);
  pinMode(RELAY3_PIN, OUTPUT);
  pinMode(RELAY4_PIN, OUTPUT);
  pinMode(RELAY5_PIN, OUTPUT);

  pinMode(LEARN_LED_PIN, OUTPUT);
}

void loop() {
  handleButtons();
  if (learn_mode_enable) {
    blinkLearnLED();
    if (program_slot == 0) {
      if (button1_pressed) {
        program_slot = 1;
        Serial.println("Slot 1 is selected!");
        button1_pressed = false;
      } else if (button2_pressed) {
        program_slot = 2;
        Serial.println("Slot 2 is selected!");
        button2_pressed = false;
      } else if (button3_pressed) {
        program_slot = 3;
        Serial.println("Slot 3 is selected!");
        button3_pressed = false;
      } else if (button4_pressed) {
        program_slot = 4;
        Serial.println("Slot 4 is selected!");
        button4_pressed = false;
      } else if (button5_pressed) {
        program_slot = 5;
        Serial.println("Slot 5 is selected!");
        button5_pressed = false;
      }
    } else {
      if (both_button_pressed) {
        if (program_slot != 0) {
          saveRelayChanges(program_slot);
          Serial.println("Slot configuration is saved!");
        }
        program_slot = 0;
        learn_mode_enable = false;

        relay1_status = false;
        relay2_status = false;
        relay3_status = false;
        relay4_status = false;
        relay5_status = false;

        digitalWrite(LEARN_LED_PIN, LOW);
        both_button_pressed = false;
      }

      if (button1_pressed) {
        relay1_status = changeRelayState(RELAY1_PIN);
        button1_pressed = false;
      } else if (button2_pressed) {
        relay2_status = changeRelayState(RELAY2_PIN);
        button2_pressed = false;
      } else if (button3_pressed) {
        relay3_status = changeRelayState(RELAY3_PIN);
        button3_pressed = false;
      } else if (button4_pressed) {
        relay4_status = changeRelayState(RELAY4_PIN);
        button4_pressed = false;
      } else if (button5_pressed) {
        relay5_status = changeRelayState(RELAY5_PIN);
        button5_pressed = false;
      }
    }
    if (both_button_pressed) {
      turnOffAll();
      digitalWrite(LEARN_LED_PIN, LOW);
      learn_mode_enable = false;
      both_button_pressed = false;
      Serial.println("learn mode off");
    }
  } else {
    if (both_button_pressed) {
      turnOffAll();
      learn_mode_enable = true;
      both_button_pressed = false;
      Serial.println("learn mode on");
    }

    if (button1_pressed) {
      loadRelayChanges(1);
      Serial.println("Configures loaded for slot 1");
      button1_pressed = false;
    } else if (button2_pressed) {
      loadRelayChanges(2);
      Serial.println("Configures loaded for slot 2");
      button2_pressed = false;
    } else if (button3_pressed) {
      loadRelayChanges(3);
      Serial.println("Configures loaded for slot 3");
      button3_pressed = false;
    } else if (button4_pressed) {
      loadRelayChanges(4);
      Serial.println("Configures loaded for slot 4");
      button4_pressed = false;
    } else if (button5_pressed) {
      loadRelayChanges(5);
      Serial.println("Configures loaded for slot 5");
      button5_pressed = false;
    }
  }
}
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module
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