#include "TinyDebug.h"

#define buzzer             0                   // sumbador
#define RL1                4                   //RELÉ 1
#define RL2                5                   //RELÉ 2
#define te                 3                  //entrada termistor
#define ADC               1023                // Multiplo de entrada ADC
#define THERMISTOR        992                 // VALOR OHM DEL SENSOR 25 °C(992 spark)(2500 optra)
#define NUMSAMPLES        10                  // NUMERO DE MUESTRAS TOMADAS NTC
#define BCOEFFICIENT      3680                // beta coefficient 3000-4000 (3680 spark) 
#define SERIESRESISTOR    974                 // resistor en serie 10k (974 para spark)

unsigned char sample_count=0;
int samples[NUMSAMPLES];
float steinhart;
unsigned long timer;
static bool RL1s, RL2s;

void termistor()
{
  float Rth, average=0;  char i;                                                 
  for (i=0; i< NUMSAMPLES; i++) { samples[i] = analogRead(te); } 
  for (i=0; i< NUMSAMPLES; i++) { average += samples[i]; }
  average /= NUMSAMPLES;                                                        
  Rth = (ADC * float(THERMISTOR)/average)-float(THERMISTOR);      //Valor de Rth en funcion del conexionado de nuestro termistor
  steinhart = (1/((1/298.15)+(1/float(BCOEFFICIENT))*log(SERIESRESISTOR/Rth)))-273.15; //Aplicamos directamente Steinhart-hart para NTC                                           
  /* el  circuito     Analog pin A2
  *                         |
  *    GND |-----/\/\/\-----+-----/\/\/\-----| 5V
  *               ^                ^ 
  *           R termistor        SERIESRESISTOR */
}
void setup() 
{
  Debug.begin();
  Debug.println(F("Control Temp Vent"));
  pinMode(te,INPUT); pinMode(buzzer, OUTPUT);             //Configurados entradas analogicas
  pinMode(RL1, OUTPUT); pinMode(RL2, OUTPUT);             //Configurados como salidas digitales
  pinMode(1, OUTPUT);
  timer = millis();                                    //tiempos igualados al contador millis
}

void loop() 
{ 
  if (millis() > timer) 
  {
   termistor();                                         // Activa la función termistor
   Debug.print(steinhart,1); Debug.println(F(" °C"));
   timer = millis() + 3000UL;                           // actualización cada 2s
  }
  if (steinhart>=95) 
  {RL1s=true; digitalWrite(RL1,HIGH); digitalWrite(1,HIGH);}         // Vent 1 alcanza 95°C
  if (steinhart<=90)
  {RL1s=false;  digitalWrite(RL1,LOW); digitalWrite(1,LOW);}  // si es menor igual a 90 y posterior a 8seg
  if (steinhart>=103){RL2s=true; digitalWrite(RL2,HIGH); }                          //RL1s=true; digitalWrite(RL2,HIGH); //Vent 2 104°C         
  if (steinhart<=98) {RL2s=false;  digitalWrite(RL2,LOW);}                         //RL2s=false; 
  if (steinhart>=112 )
  {  
    digitalWrite(buzzer,HIGH); delay(1);                 // Alarma Alta temperatura 112
    digitalWrite(buzzer,LOW); delay(1);  
   }
}
//El Sketch usa 2688 bytes (44%) del espacio de almacenamiento de programa. El máximo es 6012 bytes.
//Variables globales usan 34 bytes de memoria dinamica.
//La libreria "TinyDebug.h" a veces da error en Digispark 16.5Mhz y se debe comentar todo lo asociado (Debug.) para funcionar

ATTINY8520PU
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module
ScopeBreakout