/*
  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
const int breath_pin = 8;  //nastavení pinu vstupu z optopřevodníku ze simulátoru dýchání

int switch3_state=0; //nastavení počátečního stavu proměnné tlačítka 3
int switch3_state_last = LOW;
int relay_state = HIGH;
long casLastSwitch=0;
long casLastSwitchON=0;
int breath_state=1;
int breath_state_last=1;
long breath_time=0;
int breath_delay=0;
int button_ON=LOW;
long nebulization_start_time=0;
long duration=0;

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


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_state == HIGH) {
        relay_state=LOW;  //zapne relé
        button_ON=HIGH; //zapnuto tlačítkem
        casLastSwitchON=millis();
    }
    else if (switch3_state == HIGH && relay_state == LOW) {
        relay_state=HIGH; //vypne relé
        button_ON=LOW;  //vypnuto tlačítkem
    }
    casLastSwitch=millis();
    switch3_state_last=switch3_state;
  }
  digitalWrite(relay_pin, relay_state);
}


void breath_action(){
    breath_state=digitalRead(breath_pin);

  if (breath_state==LOW && breath_state != breath_state_last){  
    breath_time=millis();
    }
  breath_state_last=breath_state;
}



    
void loop(){
  breath_action();
  button3_action();
  nebulization_start_time = breath_time + breath_delay;

  if(button_ON==LOW){ //pokud nebylo relé zapnuto tlačítkem může relé ovládat simulátor
    if (breath_state==LOW && millis()>=nebulization_start_time){
      relay_state=LOW;
      casLastSwitchON=millis();
      }
    else if(breath_state==HIGH){
      relay_state=HIGH;
      }
  }
  
  lcd.setCursor(0, 0);  // set the cursor to column X, line Y 
  lcd.print("TA:"); // time Actual
  lcd.print(millis() / 1000); // print the number of seconds since reset
  lcd.print("s Delay:");  //Time Last breath start
  lcd.print(breath_delay);
//  lcd.print(" TN:");  //Time Last breath start
//  lcd.print(nebulization_start_time/1000);
  lcd.setCursor(0, 1);
  lcd.print("TB:");  //Time Last breath start
  lcd.print(breath_time/1000.0,3);
  lcd.print("s Duration:");  //Time Last breath start
  lcd.print(duration);
//  lcd.print("BS:"); //Breath state
//  lcd.print(breath_state); // možnost !inverze hodnoty
//  lcd.print(" TL:");  // time last 
//  lcd.print(casLastSwitchON / 1000); // print the number of seconds since reset
//  lcd.print(" RS:");  //Relay state
//  lcd.print(relay_state); //možnost !inverze hodnoty
  
//Serial.print(millis());
//Serial.print("; ");
//Serial.println(millis()/1000.0,3);
//  Serial.println(casLastSwitch);


 
}
