// Code generated by Arduino IoT Cloud
#include <ArduinoIoTCloud.h>
#include <Arduino_ConnectionHandler.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
Adafruit_SSD1306 display(128, 64, &Wire, -1);
const char DEVICE_LOGIN_NAME[]  = "83a3f333-4d42-4c7d-94f8-38eb1cd16037";
const char SSID[]               = "Wokwi-GUEST";
const char PASS[]               = "";
const char DEVICE_KEY[]         = "akP3R6AhVkf@803yYRazZv8bK";
void onSelect1Change();
void onSelect2Change();
void onScheduleChange();
void onControlChange();
void onSetChange();
time_t UnixTime;
unsigned long skr = 0;
String waktu;
int select_1;
int select_2;
CloudSchedule schedule;
bool control;
bool set;
bool rly_sched[6] = {false, false, false, false, false, false};
bool rly_onoff[12];
char rly_name[13] = {"ABCDEFGHIJKL"};
CloudSchedule R1, R2, R3, R4, R5, R6;
int count[6] = {0, 0, 0, 0, 0, 0};
int pin_sched[6] = {23, 19, 18, 5, 17, 16};
int pin_onoff[12] = {32, 33, 25, 26, 27, 14, 12, 13, 15, 2, 0, 4};
void initProperties() {
  ArduinoCloud.setBoardId(DEVICE_LOGIN_NAME);
  ArduinoCloud.setSecretDeviceKey(DEVICE_KEY);
  ArduinoCloud.addProperty(select_1, READWRITE, ON_CHANGE, onSelect1Change);
  ArduinoCloud.addProperty(select_2, READWRITE, ON_CHANGE, onSelect2Change);
  ArduinoCloud.addProperty(schedule, READWRITE, ON_CHANGE, onScheduleChange);
  ArduinoCloud.addProperty(control, READWRITE, ON_CHANGE, onControlChange);
  ArduinoCloud.addProperty(set, READWRITE, ON_CHANGE, onSetChange);
}
WiFiConnectionHandler ArduinoIoTPreferredConnection(SSID, PASS);
void setup() {
  Serial.begin(9600);
  if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    for (;;);
  }
  delay(1500);
  initProperties();
  ArduinoCloud.begin(ArduinoIoTPreferredConnection);
  setDebugMessageLevel(2);
  ArduinoCloud.printDebugInfo();
  for (int i = 0; i < 6; i++) {
    pinMode(pin_sched[i], OUTPUT);
  }
  for (int i = 0; i < 12; i++) {
    pinMode(pin_onoff[i], OUTPUT);
  }
}
void loop() {
  ArduinoCloud.update();
  if (set) {
    if (select_1 == 0) R1 = schedule;
    if (select_1 == 1) R2 = schedule;
    if (select_1 == 2) R3 = schedule;
    if (select_1 == 3) R4 = schedule;
    if (select_1 == 4) R5 = schedule;
    if (select_1 == 5) R6 = schedule;
  }
  rly_sched[0] = R1.isActive();
  rly_sched[1] = R2.isActive();
  rly_sched[2] = R3.isActive();
  rly_sched[3] = R4.isActive();
  rly_sched[4] = R5.isActive();
  rly_sched[5] = R6.isActive();
  if (control) {
    for (int i = 0; i < 12; i++) {
      if (select_2 == i) {
        digitalWrite(pin_onoff[i], HIGH);
        rly_onoff[i] = true;
      }
    }
  } else {
    for (int i = 0; i < 12; i++) {
      if (select_2 == i) {
        digitalWrite(pin_onoff[i], LOW);
        rly_onoff[i] = false;
      }
    }
  }
  if (millis() - skr > 1000) {
    UnixTime = ArduinoCloud.getLocalTime();
    waktu = ctime(&UnixTime);
    display.clearDisplay();
    display.setTextSize(1);
    display.setTextColor(SSD1306_WHITE);
    display.setCursor(0, 0);
    display.print(waktu.substring(0, 11));
    display.print(waktu.substring(20));
    display.print("       ");
    display.println(waktu.substring(11, 19));
    for (int i = 0; i < 6; i++) {
      display.print("R");
      display.print(i + 1);
      display.print(":");
      if (rly_sched[i]) {
        digitalWrite(pin_sched[i], true);
        display.print("ON.");
        if (count[i] < 10) display.print(".");
        if (count[i] < 100) display.print(".");
        if (count[i] < 1000) display.print(".");
        display.print(count[i]);
        count[i]++;
      } else {
        digitalWrite(pin_sched[i], false);
        count[i] = 0;
        display.print("OFF....");
      }
      if ((i + 1) % 2 == 0) display.println();
    }
    for (int i = 0; i < 12; i++) {
      display.print(rly_name[i]);
      if (rly_onoff[i]) display.print(":1, ");
      else display.print(":0, ");
      if ((i + 1) % 4 == 0) display.println();
    }
    display.display();
    skr = millis();
  }
}
void onSetChange()  {
}
void onScheduleChange()  {
}
void onSelect1Change()  {
}
void onSelect2Change()  {
}
void onControlChange()  {
}
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module
R1:Scheduled
R2:Scheduled
R3:Scheduled
R4:Scheduled
A:On/Off
B:On/Off
C:On/Off
D:On/Off
E:On/Off
F:On/Off
G:On/Off
H:On/Off
I:On/Off
J:On/Off
K:On/Off
L:On/Off
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module
R5:Scheduled
R6:Scheduled