// ok. červené start (otevreni klapky) + přidávání času po pul hod
// fialový (manualni rezim bez ohledu na cas) otevreni a zavreni a pri zavreni reset casu

// dalsi tlacitko - mezi pevne polohy 25 50 75 procent
// na displej info o otevreno v procentech
// esp soft reset, pokuod nepobezi casovani 1x denně
// po resetu s klapkou nehybat

#include <LiquidCrystal_I2C.h>
#include <Servo.h>
Servo servo1;

LiquidCrystal_I2C lcd(0x27, 16, 2); // set LDC address
unsigned long time1;

double secondsRaw = 3;

double hours;
int hoursToShow;

double minutes;
int minutesToShow;

double seconds;
int secondsToShow;

String timeToPrint;

String H;
String m;
String s;

int openedPosition = 90;
int closedPosition = 0;

void setup(){

Serial.begin(9600);
Serial.println("BEGIN");
Serial.println();

pinMode(5, INPUT_PULLUP);
pinMode(7, INPUT_PULLUP);
pinMode(6,OUTPUT);
analogWrite(6, 255); //LCD brightness (does not work in WOKWI simulation)

delay(50);

lcd.init();               // initialize the lcd
lcd.backlight();          // backlight ON

lcd.setCursor(3, 0);      // cursor on column 3, row 0
lcd.print("ARDUINO UNO"); // print on display

lcd.setCursor(4, 1);      // cursor on column 4, row 1
lcd.print("Wellcome");    // print on display

delay(100);

lcd.clear();

// set PIN and range in us. Standard servo range is 500 - 2500 us
servo1.attach(6, 500, 2500);

//servo1.write(180);

} // setup end

void loop(){

// ****** red button start ****** //
if (digitalRead(5) == LOW) {
  secondsRaw = secondsRaw + 1800;
  if (secondsRaw > 10800) {
    secondsRaw = 1800;
  }
  delay(300);
}

if (secondsRaw == 0) {
  servo1.write(0);
  obrazovka2();
  secondsRaw--;
  servo1.write(closedPosition);
}
// ****** red button end ****** //

// ****** violet button start ****** //
if (secondsRaw <= 0) {
  if (digitalRead(7) == LOW) {
    Serial.println("xxx");
    secondsRaw = secondsRaw + 1800;
    servo1.write(openedPosition);
  }
}
// ****** violet button end ****** //

// time counting
if (secondsRaw > 0) {
  if (millis() > time1) {
    time1 = millis() + 1000;
    secondsRaw--;
    seconds_to_time ();
    obrazovka1();
  }
}

} // loop end

void obrazovka1(){
lcd.clear();

lcd.setCursor(1, 0);
lcd.print("Cas do zavreni");

lcd.setCursor(1, 1);
lcd.print(timeToPrint);
}

void obrazovka2(){
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Klapka uzavrena");
//lcd.setCursor(1, 1);
//lcd.print(timeStr);
//lcd.setCursor(3, 1);
//lcd.print("h");
}


void seconds_to_time(){
 hours = fmod(secondsRaw / 3600.0, 1);
 hoursToShow = secondsRaw / 3600;
 minutes = hours * 3600.0 / 60;
 minutesToShow = minutes;
 seconds = fmod(minutes, 1);
 secondsToShow = seconds * 60;

 //save int as a String to be able to format it
 H = hoursToShow;
 m = minutesToShow;
 s = secondsToShow;

 // Add leading zeroes if necessary
 if (H.length() == 1){
   H = "0" + H;
 } 
 if (m.length() == 1){
   m = "0" + m;
 } 
 if (s.length() == 1){
   s = "0" + s;
 } 

 timeToPrint = H + ":" + m + ":" + s;
 Serial.println(timeToPrint);
} //seconds_to_time end
uno:A5.2
uno:A4.2
uno:AREF
uno:GND.1
uno:13
uno:12
uno:11
uno:10
uno:9
uno:8
uno:7
uno:6
uno:5
uno:4
uno:3
uno:2
uno:1
uno:0
uno:IOREF
uno:RESET
uno:3.3V
uno:5V
uno:GND.2
uno:GND.3
uno:VIN
uno:A0
uno:A1
uno:A2
uno:A3
uno:A4
uno:A5
lcd1:GND
lcd1:VCC
lcd1:SDA
lcd1:SCL
btn1:1.l
btn1:2.l
btn1:1.r
btn1:2.r
btn2:1.l
btn2:2.l
btn2:1.r
btn2:2.r
servo1:GND
servo1:V+
servo1:PWM
btn3:1.l
btn3:2.l
btn3:1.r
btn3:2.r