// https://programmersqtcpp.blogspot.com/2022/07/termostato-con-dht22.html

// Sketch valido per AVR con ADC 10-bit
#include <LiquidCrystal.h>
#include "config.h"
IMPORT_LCD_SYMB();

//#include <max6675.h>
//extern MAX6675 th1;
//https://github.com/adafruit/MAX6675-library/blob/master/examples/lcdthermocouple/lcdthermocouple.ino
// This site is in read only mode. Please continue to browse, but replying, likes, and other actions are disabled for now.

extern void sscreen();
extern void showTemperature(float t);
extern void showSetpoint(float stp);
extern void showOnOff(bool of);
extern void showIsNan();
extern void showShutdown();
extern float readTh1();

#define TIME_INTERVAL   250 // espressa in millesimi di secondo
#define T_HYSTERESIS    2    // espressa in °C 

const byte g_relayPin = PIN_RELAY1;
float g_tck = 24;
int32_t g_temperature;
int16_t g_setpoint;
int16_t g_isteresi = T_HYSTERESIS * 10;
uint32_t g_timer1sec;
byte g_1sCounter;

// 1023.75
// struttura di supporto
struct Setpoint {
  uint16_t _old;
  uint16_t _new = 1024;   // new != old
};


byte anomalia;
Setpoint mySetpoint;

float thTemp;
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()


void setup() {
    Serial.begin(115200);
    lcd.begin(16, 2);
    float tamb = 25;
    float tres = 25;
    float pot = 2200;
    /*while(true) {
      //delay(1);
      float dt = tres - tamb;
      tres += (dt/2) / 1200;
      //tamb = dt * (dt / tres*0.8);
      tamb += (dt/2) * (dt / tres*0.8);
      //tamb += dt / 800;
      showTemperature(tres);
      if (tres > 300)
          break;
    }*/
    Serial.println(tamb);

    pinMode(g_relayPin, OUTPUT);
    pinMode(A5, OUTPUT);
    sscreen();
    // sscreen() impiega circa 1 secondo
    delay(500); // 1/2 secondo di attesa
    lcd.clear();
    

} // end void setup()

uint32_t tblk;
bool blink;
float collect[17];

void printCollect() {
    for (byte i = 0; i < 17; i++) {
        Serial.print(collect[i]);
        if (i < 16)
            Serial.print(", ");
    }
    Serial.println();
}

void safetyCheck(bool thState, float tfloat) {
    static float media;
    static byte nSample;
    static byte counter;
    static float t0;
    if (!thState) {
        media = 0;
        counter = 0;
        nSample = 0;
        anomalia = 0;
    } else {

        if (counter == 3) {
            if (nSample == 0) {
                t0 = tfloat;
                collect[16] = t0;
            }
            collect[nSample] = tfloat;
            media += tfloat;
            nSample++;
            if (nSample == 16) {
                printCollect();
                nSample = 0;
                media /= 16;
                if (media <= t0) {
                    anomalia++;

                    if (anomalia == 4) {
                        digitalWrite(g_relayPin, LOW);
                        showOnOff(LOW);
                        delay(1000);
                        showShutdown();
                        while (true);
                    }
                    Serial.println("anomalia");
                } else if (anomalia) {
                    anomalia = 0;
                    Serial.println("no anomalia");
                }
                media = 0;
            }
            counter = 0;
        }
        counter++;
    }
}

void blinkWarning() {
    if (anomalia && millis() - tblk > 250) {
        tblk = millis();
        blink = !blink;
        digitalWrite(A5, blink);
    } else {
        digitalWrite(A5, LOW);
    }
}


void loop() {
    blinkWarning();

    if (millis() - g_timer1sec >= TIME_INTERVAL) {

        bool thState = LOW;
        float tfloat = readTh1();

        showTemperature(tfloat);
        if (isnan(tfloat)) {
            // IS NAN error
            digitalWrite(g_relayPin, LOW);
            delay(1000);
            showIsNan();
            while (true);
            //Serial.println("error temperature is nan");

        } else {

            // da floart x 10 a int16_t
            g_temperature = tfloat * 10;
            //Serial.println(g_temperature);

            // chiama la funzione termostato
            thState = checkThermostate(digitalRead(g_relayPin));
            digitalWrite(g_relayPin, thState);
        }

        safetyCheck(thState, tfloat);
        // visualizza ON/OFF sul display
        showOnOff(thState);
        g_timer1sec = millis();

    }

    // usa struttura di supporto
    mySetpoint._old = analogRead(A1);
    if (mySetpoint._old < 68) {
        mySetpoint._old = 68;
    }
    if (mySetpoint._old != mySetpoint._new) {
        mySetpoint._new = mySetpoint._old;

        float stpf = mySetpoint._new / 3.41; //13.3;
        g_setpoint = stpf * 10;    // da float x 10 a uint16_t
        showSetpoint(stpf);
    }
} // end void loop()
nano:12
nano:11
nano:10
nano:9
nano:8
nano:7
nano:6
nano:5
nano:4
nano:3
nano:2
nano:GND.2
nano:RESET.2
nano:0
nano:1
nano:13
nano:3.3V
nano:AREF
nano:A0
nano:A1
nano:A2
nano:A3
nano:A4
nano:A5
nano:A6
nano:A7
nano:5V
nano:RESET
nano:GND.1
nano:VIN
nano:12.2
nano:5V.2
nano:13.2
nano:11.2
nano:RESET.3
nano:GND.3
lcd:VSS
lcd:VDD
lcd:V0
lcd:RS
lcd:RW
lcd:E
lcd:D0
lcd:D1
lcd:D2
lcd:D3
lcd:D4
lcd:D5
lcd:D6
lcd:D7
lcd:A
lcd:K
r1:1
r1:2
pot2:GND
pot2:SIG
pot2:VCC
relay1:NO2
relay1:NC2
relay1:P2
relay1:COIL2
relay1:NO1
relay1:NC1
relay1:P1
relay1:COIL1
led1:A
led1:C
thkBreakout
chip1:VCC
chip1:GND
chip1:BRK
chip1:SCK
chip1:SO
chip1:CS
led2:A
led2:C