/*
  The circuit:
 * Relay GND pin to ground 
 * Relay IN1 pin digital pin 6
 * Relay GND pin to 5V
 * LCD RS pin to digital pin 12
 * LCD Enable pin to digital pin 11
 * LCD D4 pin to digital pin 5
 * LCD D5 pin to digital pin 4
 * LCD D6 pin to digital pin 3
 * LCD D7 pin to digital pin 2
 * LCD R/W pin to ground
 * LCD VSS pin to ground
 * LCD VCC pin to 5V
 * 10K resistor:
 * ends to +5V and ground
 * wiper to LCD VO pin (pin 3)
*/


#include <LiquidCrystal.h>
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

const int relay_pin = 6;  // nastavení pinu relé
const int button3_pin = 7;  //nastavení pinu tlačítka 3

int switch3_state=0; //nastavení počátečního stavu proměnné tlačítka 3
int switch3_state_last = LOW;
int Relay = HIGH;
long casLastSwitch=0;


void setup() {
  lcd.begin(16, 2);  // set up the LCD's number of columns and rows:
  pinMode(button3_pin, INPUT);  // nastavení pinu tlačítka jako vstup
  pinMode(relay_pin, OUTPUT); //nastavení pinu relé jako výstup
  digitalWrite(relay_pin, Relay);
}


void button3_action(){
  switch3_state=digitalRead(button3_pin); //  vyčte stav tlačítka3
  
  if (millis()>=casLastSwitch+100 && switch3_state != switch3_state_last)   //provede dále pokud od posledního zmáčknutí tlačítka uběhlo alespoň....ms a změnil se stav tlačítka
    {
    if (switch3_state == HIGH && Relay == HIGH) 
      {
        Relay=LOW;
      }
    else if (switch3_state == HIGH && Relay == LOW) 
      {
        Relay=HIGH;
      }
    casLastSwitch=millis();
    switch3_state_last=switch3_state;
    }
    digitalWrite(relay_pin, Relay);
  }

    
void loop(){
  button3_action();
  lcd.setCursor(0, 0);  // set the cursor to column X, line Y 
  lcd.print("Time ");
  lcd.print(millis() / 1000); // print the number of seconds since reset
  lcd.print("s");
  lcd.setCursor(0, 1);
  lcd.print("Time last ");
  lcd.print(casLastSwitch / 1000); // print the number of seconds since reset
  lcd.print("s");


 
}
