#if 0
//Regulácia ohrevu vody teplého vodného kúpeľa
#include <Arduino.h>
#include <driver/gpio.h>

template<class T> inline Print &operator <<(Print &obj, T arg) {obj.print(arg); return obj;} //Serial <<

#define OHREV_VODY GPIO_NUM_27
#define THERMISTOR_PIN GPIO_NUM_25 //R2 -> 10k NTC
#define SAMPLES_COUNT 10 // pocet vzoriek teploty
int R1 = 10000; //10k resistor
int samples[SAMPLES_COUNT];


// parametre PWM
const int freq = 5000;
const int PWM_channel = 0;
const int resolution = 8;

//PID konstanty
//Ladenie bude prebiehat na realnych zariadeniach
float kp = 1;
float ki= 0;
float kd = 0;
float PID_p = 0;
float PID_i = 0;
float PID_d = 0;

//PID premenne
int x = 0; //akcna_velicina
float e = 0; //regulacna_odchylka
float e_prev = 0; //predchadzajuca regulacna odchylka
float currentTime, prevTime, elapsedTime; //aktualny, predchadzajuci a uplynuly cas

//dalsie premenne
float omega = 0; //riadiaca velicina
float y = 0; //regulovana velicina 

void setup()
{
    Serial.begin(115200);
    pinMode(OHREV_VODY, OUTPUT);
    pinMode(THERMISTOR_PIN, INPUT);

    // konfigurácia PWM
    ledcSetup(PWM_channel, freq, resolution);
    //pripojenie kanálu k GPIO pinu, ktorý chceme ovládať
    ledcAttachPin(OHREV_VODY, PWM_channel);
}

void loop()
{
    //nastavenie riadiacej veliciny
    if(Serial.available()>0) 
    {
        omega = Serial.parseInt();
    }

    //nacitanie aktualnej regulovanej veliciny z niekolkych vzoriek-------------------------------------------
    for (int i = 0; i < SAMPLES_COUNT; i++)
    {
        samples[i] = analogRead(THERMISTOR_PIN); //napatie na odpore R2 (NTC termistor)    
    }
    //vypocet priemeru
    int average_sample = 0;
    for (int i = 0; i < SAMPLES_COUNT; i++)
    {
        average_sample += samples[i];
    }
    
    int V_out = average_sample/SAMPLES_COUNT;  
    float R2 = R1 * (1023.0 / (float)V_out - 1.0); //vypocet odporu R2 (NTC termistor)
    float logR2 = log(R2); //vypocet logaritmu pre Steinhart–Hart-ovu rovnicu
    //vypocet koeficientov pri 3 roznych teplotach, https://www.thinksrs.com/downloads/programs/therm%20calc/ntccalibrator/ntccalculator.html
    float A = 1.009249522e-03, B = 2.378405444e-04, C = 2.019202697e-07;
    float T = (1.0 / (A + B*logR2 + C*logR2*logR2*logR2)); //Steinhart–Hart-ova rovnica pre vypocet teploty polovodicov [K]
    T = T - 273.15; //prevod na [°C]
    y = T; //priradenie k regulovanej velicine  

    //vypis
    Serial << "Nastavena teplota = " << omega << "\n";
    Serial << "Aktualna teplota = " << y << "\n\n";

    e = omega - y; //Vypocet regulacnej odchylky

    PID_p = kp * e; //Vypocet P zlozky
    PID_i = PID_i + (ki * e); //vypocet I zlozky
    //if(PID_i > Max) PID_i = Max;
    //else if(PID_i < Min) PID_i = Min;
    prevTime = currentTime; //zapametanie si predchadzajuceho casu
    currentTime = millis(); //nacitanie aktualneho casu
    elapsedTime = (currentTime - prevTime); //vypocet uplynuleho casu
    PID_d = kd*((e - e_prev)/elapsedTime); //vypocet D zlozky
    x = PID_p + PID_i + PID_d; //vypocet akcnej veliciny
    e_prev = e; //zapametanie si aktualnej chyby 

    //obmedzenie akcnej veliciny na rozsah 0-255
    if(x > 255) x = 255; 
    if(x < 0) x = 0;

    ledcWrite(PWM_channel, x); //Akcny zasah

}
#endif