#include <Arduino.h>
#include <U8x8lib.h>
#ifdef U8X8_HAVE_HW_I2C
#include <Wire.h>
#endif
//U8X8_SSD1306_128X64_NONAME_HW_I2C u8x8(/* reset=*/ U8X8_PIN_NONE);
//U8X8_SSD1306_128X64_ALT0_HW_I2C u8x8(/* reset=*/ U8X8_PIN_NONE); // same as the NONAME variant, but may solve the "every 2nd line skipped" problem
U8X8_SSD1306_128X64_NONAME_SW_I2C u8x8(/* clock=*/ 2, /* data=*/ 0, /* reset=*/ U8X8_PIN_NONE); // Digispark ATTiny85
//U8X8_SSD1306_128X64_NONAME_SW_I2C u8x8(/* clock=*/ SCL, /* data=*/ SDA, /* reset=*/ U8X8_PIN_NONE); // OLEDs without Reset of the Display
#define sensor 3
#define rele 1
#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 (1000 para spark)
int samples[NUMSAMPLES];
float steinhart;
unsigned long timer;
void termistor()
{
float Rth, average=0; char i;
for (i=0; i< NUMSAMPLES; i++) { samples[i] = analogRead(sensor); }
for (i=0; i< NUMSAMPLES; i++) { average += samples[i]; }
average /= NUMSAMPLES;
Rth = (1024 * 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
}
void setup(void)
{
//pinMode(rele, OUTPUT); pinMode(sensor, INPUT);
//digitalWrite(1, 0);
u8x8_SetI2CAddress(u8x8.getU8x8(), 0x3C * 2);
u8x8.begin();
u8x8.setFont(u8x8_font_artosserif8_r);
timer = millis();
}
void loop()
{
if (millis() > timer)
{ termistor(); u8x8.clear(); timer = millis() + 3000UL; }
u8x8.setCursor(2,0); u8x8.println(F("Temperatura"));
u8x8.setCursor(5,2); u8x8.print(steinhart,0);
u8x8.setCursor(8,2); u8x8.println(F(" *C"));
if (steinhart>=92){digitalWrite(rele,HIGH); u8x8.setCursor(1,4); u8x8.println(F("Ventilador On"));}
else {digitalWrite(rele,LOW);}
}
//Codigo optimizado en memoria del microcontrolador
//El Sketch usa 6242 bytes (93%) del espacio de almacenamiento de programa. El máximo es 6650 bytes.
//Las variables Globales usan 206 bytes (40%) de la memoria dinámica
//no se evidencia en la simulacion pero funciona!