// https://programmersqtcpp.blogspot.com/2022/07/termostato-con-arduino-2-puntata.html
#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 10, 9, 8, 7);

const byte g_relayPin = 13;

int16_t g_temperature;
int16_t g_setpoint;
int16_t g_isteresi = 3 * 100; 

bool checkThermostate(bool state) {
  switch (state) {
    case LOW:
    if (g_temperature <= g_setpoint - g_isteresi) {
        state = HIGH;
    }
    break;
    case HIGH:
    if (g_temperature > g_setpoint) {
        state = LOW;
    }
    break;
     
  } // end switch (thermostateState)
  return state;
} // end checkThermostate()


/** analog16Samples() colleziona 16 samples, 1 ogni 62ms 
    Somma tutti i 16 samples e divide questa per 16.
    Durante il collezionamento restituisce 0, mentre
    dopo avere tutti i 16 samples, calcola la media e la restituisce.
*/
uint16_t analog16Samples() {
    static uint16_t somma;
    static uint32_t timer62ms;
    static byte nSamples;

    if (millis() - timer62ms >= 62) {
        
        timer62ms = millis();
        somma += analogRead(A0);    // accumula i campioni
        nSamples++;
        if (nSamples == 16) {
            nSamples = 0;
            uint16_t media = somma / 16;  // calcola la media
            somma = 0;
            return media; // restituisce la media
        }
          
    }
    return 0;
}

float calcTemperature(uint16_t a0) {
    float average = a0;
    average = 1023 / average - 1;
    average = 10000.0 / average;
    //float steinhart;
    average = average / 10000.0;      // (R/Ro)
    average = log(average);           // ln(R/Ro)
    average /= 3950;                  // 1/B * ln(R/Ro)
    average += 1.0 / (25 + 273.15);   // + (1/To)
    average = 1.0 / average;          // Invert
    average -= 273.15;  
    return average;           
}

void setup()
{
    Serial.begin(115200);
    lcd.begin(16, 2);
        
    pinMode(g_relayPin, OUTPUT);
       
} // end void setup()

constexpr char *onoff[] = { "OFF", "ON " };
char temperatureBuffer[7];
// struttura di supporto
struct Setpoint {
  uint16_t _old;
  uint16_t _new = 1024;   // new != old
};

Setpoint mySetpoint;


void loop() {
  // a0 contiene una lettura aggiornata ogni 62ms x 16 = 992ms
  uint16_t a0 = analog16Samples();
  // entra nella if (a0 != 0) ogni 992ms
  if (a0 != 0) {
      //Serial.println(millis());
      // converte a0 in tf.
      float tf = calcTemperature(a0);
      // converte tf nella C string temperatureBuffer
      dtostrf(tf, 6, 1, temperatureBuffer);
      // tronca e converte tf in int16_t
      g_temperature = tf * 100;
      // visualizza la C string temperatureBuffer sul display
      lcd.setCursor(0, 1);
      lcd.print(temperatureBuffer);
      // chiama la funzione termostato
      bool thState = checkThermostate(digitalRead(g_relayPin));
      digitalWrite(g_relayPin, thState);
      // visualizza ON/OFF sul display
      lcd.setCursor(10, 0);
      lcd.print(onoff[thState]);
  }

  // usa struttura di supporto 
  mySetpoint._old = analogRead(A1);
  if (mySetpoint._old != mySetpoint._new) {
      mySetpoint._new = mySetpoint._old;
     
      float stpf = mySetpoint._new / 13.3;
      g_setpoint = stpf * 100;    // da float a uint16_t
      dtostrf(stpf, 6, 1, temperatureBuffer);
     
      lcd.setCursor(0, 0);
      lcd.print(temperatureBuffer);
  }
   
    
} // end void loop()