/*
https://dl.espressif.com/dl/package_esp32_index.json
*/
#include <U8g2lib.h>
#include <Arduino.h>
#include <math.h>
#include <EasyBuzzer.h>
#include <Wire.h>
/*#include "BluetoothSerial.h"
#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif
BluetoothSerial SerialBT;
*/
#include <esp_task_wdt.h>

hw_timer_t * timer = NULL;
U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);   //SDA 19, SCL 23 wemos lolin es32 lite
//U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0, /*clock=*/ 22, /*data=*/ 21, /*reset=*/ U8X8_PIN_NONE); // 
                                                                            
#define RL1                32                   //RELÉ 1
#define RL2                33                   //RELÉ 2
#define buzzer             26                  //Buzzer
#define ni1                15                   //entrada nivel +
#define te                 4                  //entrada termistor
#define vo                 36                   //entrada voltimetro
#define Vref               3.32                 //voltaje referencia vcc
#define ADC               4095                 // Multiplo de entrada ADC
#define NUM_SAMPLES        5                   // NUMERO DE MUESTRAS TOMADAS Voltaje
#define THERMISTOR         992                 // VALOR OHM DEL SENSOR 25 °C(992 spark)(2500 optra)
#define TEMPERATURENOMINAL 25                  // temp. for nominal resistance (almost always 25 C)
#define NUMSAMPLES         10                  // NUMERO DE MUESTRAS TOMADAS NTC
#define BCOEFFICIENT      3680                // beta coefficient 3000-4000 (3680 spark) 
#define SERIESRESISTOR    330                 // resistor en serie 10k (1k para spark)
#define RV1               98400               //RV1=100000 ohms
#define RV2               11480                //RV2=10000 ohms
#define niMin             0.72                // Voltaje nivel minimo
#define niMax             3.0                 // Voltaje nivel maximo
#define correccionT         1                  // - 5°C de correción de la temperatura
#define correccionV        0.47                 // + 0.7mV de corrección de voltaje
#define correccionL        0.1                  // + 0.1 V lectura nivel debido a caida en la resistencia 330

unsigned char xmax=128;          unsigned char ymax=62;                                   
unsigned char xcenter= 126;      unsigned char xcenter2= 2;                            
unsigned char ycenter=ymax/2+10; unsigned char ycenter2=ymax/2+10;                        
unsigned char arc=ymax/2;        unsigned char arc2=ymax/2;  

unsigned char sample_count=0, w, L,  m, m1A, m2A, p2, V;
int samples[NUMSAMPLES], sum=0, w2;
float steinhart, p, p3, x1, x2, Y1, Y2, volt;   
unsigned long timer2, timer3, timer4, timer5;
static bool RL1s, RL2s;
volatile bool ActualizarOLED = false;


void IRAM_ATTR onTimer()
{
ActualizarOLED = true;
}

const static unsigned char fondo [] U8X8_PROGMEM= 
{
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x18, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x18, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x18, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x18, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x18, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x0C, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x0C, 
  0x00, 0x00, 0x00, 0x00, 0xF8, 0x7F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x60, 0x06, 0x00, 0x00, 0xFE, 0x0F, 0x00, 0x80, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x60, 0x06, 0x00, 0x00, 0x0F, 0x00, 
  0x98, 0x3B, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0, 0x03, 
  0x00, 0xF0, 0x00, 0x00, 0x10, 0x2A, 0x3C, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0xC0, 0x03, 0x00, 0x1E, 0x00, 0x00, 0x10, 0x2B, 0x60, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x01, 0x00, 0x03, 0xF0, 0x00, 
  0x90, 0x28, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x01, 
  0xC0, 0x00, 0x10, 0x00, 0x90, 0x28, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x60, 0x00, 0x70, 0x00, 0xB8, 0x3B, 0x00, 0x04, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x10, 0x00, 
  0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x08, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x20, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x60, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x80, 0x01, 0x00, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 
  0x80, 0x01, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0xE0, 
  0x1F, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x80, 0x70, 
  0x00, 0x00, 0x00, 0xF8, 0x1F, 0x00, 0x00, 0x80, 0x3B, 0x01, 0x00, 0x00, 
  0x00, 0x00, 0x80, 0x10, 0x00, 0x00, 0x00, 0xF8, 0x3F, 0x00, 0x00, 0x80, 
  0x28, 0x01, 0x00, 0x00, 0x00, 0x00, 0x80, 0x10, 0x00, 0x00, 0x00, 0xFC, 
  0x3F, 0x00, 0x00, 0x80, 0x2B, 0x01, 0x00, 0x00, 0x00, 0x00, 0x80, 0x70, 
  0x00, 0x00, 0x00, 0xFC, 0x3F, 0x00, 0x00, 0x80, 0x2A, 0x01, 0x00, 0x00, 
  0x00, 0x00, 0x80, 0x10, 0x00, 0x00, 0x00, 0xFC, 0x1F, 0x00, 0x00, 0x80, 
  0x2A, 0x01, 0x00, 0x00, 0x00, 0x00, 0x80, 0x10, 0x00, 0x00, 0x00, 0xF8, 
  0x1F, 0x00, 0x00, 0x80, 0x3B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x70, 
  0x00, 0x00, 0x00, 0xF8, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xE0, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x70, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00,
};
const static unsigned char logoford [] U8X8_PROGMEM= 
{
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xE4, 0xFF, 
  0xFF, 0x2D, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0xF8, 0xFF, 0xFF, 0xFF, 0xFF, 0x1F, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0xE0, 0xFF, 0xFF, 0xDB, 0xEB, 0xFF, 0xFF, 0x07, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0x3F, 0x00, 0x00, 
  0x00, 0x00, 0xFD, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF0, 
  0xFF, 0x00, 0x00, 0xA4, 0x25, 0x00, 0x00, 0xFE, 0x0F, 0x00,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00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF0, 0xFF, 0xFF, 0xA7, 
  0xEA, 0xFF, 0xFF, 0x0F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0xFA, 0xFF, 0xFF, 0xFF, 0xFF, 0x3F, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFA, 0xFF, 0xFF, 0x5F, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00,
};
const static unsigned char helice1 [] U8X8_PROGMEM= 
{
  0x00, 0x00, 0x00, 0xF0, 0x01, 0x00, 0xF8, 0x03, 0x00, 0xF8, 0x07, 0x00, 
  0xF8, 0x07, 0x00, 0xF8, 0x07, 0x00, 0xF8, 0x07, 0x00, 0xE0, 0x07, 0x00, 
  0xC0, 0x85, 0x03, 0x80, 0xE7, 0x0F, 0x80, 0xFF, 0x0F, 0xF8, 0xFF, 0x0F, 
  0xFC, 0xFF, 0x0F, 0xFE, 0xFD, 0x0F, 0xFE, 0xF9, 0x07, 0xFE, 0xE0, 0x03, 
  0xFE, 0x00, 0x00, 0xFE, 0x00, 0x00, 0xFC, 0x00, 0x00, 0x7C, 0x00, 0x00, 
  };
const static unsigned char helice2 [] U8X8_PROGMEM= 
{
  0x00, 0x3E, 0x00, 0x00, 0x7E, 0x00, 0x00, 0xFF, 0x00, 0x00, 0xFF, 0x00, 
  0x00, 0xFF, 0x00, 0x00, 0xFE, 0x00, 0x00, 0xFE, 0x00, 0x00, 0x7C, 0x00, 
  0x00, 0x7C, 0x00, 0xF8, 0x3F, 0x00, 0xFC, 0x1E, 0x00, 0xFE, 0x7F, 0x00, 
  0xFE, 0xFF, 0x07, 0xFE, 0xF9, 0x0F, 0xFE, 0xF8, 0x0F, 0x7E, 0xF8, 0x0F, 
  0x7C, 0xF8, 0x0F, 0x00, 0xF0, 0x0F, 0x00, 0xE0, 0x03, 0x00, 0x00, 0x00,
  };
const static unsigned char helice3 [] U8X8_PROGMEM= 
{
  0x00, 0xE0, 0x03, 0x00, 0xF0, 0x03, 0x00, 0xF0, 0x07, 0x00, 0xF0, 0x07, 
  0x7C, 0xF0, 0x07, 0xFE, 0xF9, 0x07, 0xFF, 0xFB, 0x07, 0xFF, 0xFF, 0x03, 
  0xFF, 0xFF, 0x01, 0xFF, 0x1F, 0x00, 0x7F, 0x1E, 0x00, 0x1C, 0x3A, 0x00, 
  0x00, 0x7E, 0x00, 0x00, 0xFE, 0x01, 0x00, 0xFE, 0x01, 0x00, 0xFE, 0x01, 
  0x00, 0xFE, 0x01, 0x00, 0xFC, 0x01, 0x00, 0xF8, 0x00, 0x00, 0x00, 0x00,
  };
void gauge(uint8_t angle) 
{
  u8g2.drawXBMP(0,0,128,64,fondo);
   x1=sin(2*angle*2*3.14/360); Y1=cos(2*angle*2*3.14/360); 
  u8g2.drawLine(xcenter, ycenter, xcenter+arc*x1, ycenter-arc*Y1);
 u8g2.setCursor(90,60);
  if (w<14 || w > 57 ){u8g2.setFont(u8g2_font_profont10_tr); u8g2.print(F("ERROR"));}  
  else {u8g2.setFont(u8g2_font_profont22_tr); u8g2.setCursor(98,60); u8g2.print(w);}  
}
void gauge2(uint8_t angle2) 
{
 x2=sin(2*angle2*2*3.14/360); Y2=cos(2*angle2*2*3.14/360); 
  u8g2.drawLine(xcenter2, ycenter2, xcenter2+arc2*x2, ycenter2-arc2*Y2);
 u8g2.setFont(u8g2_font_profont10_tr);                   
  if (w2>99){u8g2.setCursor(37, 50); u8g2.print(F("o"));} //se desplaza el simbolo ° grados a 3 digitos
  else {u8g2.setCursor(28, 50); u8g2.print(F("o"));} 
  u8g2.setCursor(1, 60);
  if (w2<0 || w2 > 130 ){ u8g2.print(F("ERROR"));}  
  else {u8g2.setFont(u8g2_font_profont22_tr); u8g2.print(w2);}   
}
void voltimetro ()
{
  u8g2.setFont(u8g2_font_profont22_tr);             
  u8g2.setCursor(35, 14);
  u8g2.print(volt,1);                          // Imprime voltaje con un decimal en pantalla

}
void helice()
{
 static uint8_t frame = 0 ;
 if(frame==0){u8g2.drawXBMP(58,V,20,20,helice1);}
 if(frame==1){u8g2.drawXBMP(58,V,20,20,helice2);}
 if(frame==2){u8g2.drawXBMP(58,V,20,20,helice3);}
  frame++;
  if(frame>2){ frame = 0; }
}
void ventilador()
{
   u8g2.setFont(u8g2_font_unifont_t_77);
    digitalWrite(LED_BUILTIN, RL1s ? LOW:HIGH);
    if (RL1s==true ) {  V=20; helice();}
    if (RL2s==true ) { V=40; helice();}
    if (w2 > 115) { u8g2.drawGlyph(12, 30, 0x26a0);}
    if (volt > 14.8){ u8g2.drawGlyph(95, 12, 0x26a1);}
    if (w < 23){ u8g2.drawGlyph(108, 32, 0x26fd);  }
}
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)-(THERMISTOR);      //Valor de Rth en funcion del conexionado de nuestro termistor
  steinhart = ((1/((1/298.15)+(1/float(BCOEFFICIENT))*log(SERIESRESISTOR/Rth)))-273.15)-correccionT; //Aplicamos directamente Steinhart-hart para NTC                                           
  
}

void setup(void) 
{
 Serial.begin(115200);                                    //Velocidad Port Serial
 //SerialBT.begin("ESP32-FOCUS");                        //Bluetooth device name
 pinMode(ni1, INPUT); pinMode(te, INPUT); pinMode(vo, INPUT);   // Configurados entradas analogicas
 pinMode(RL1, OUTPUT); pinMode(RL2, OUTPUT);              //Configurados como salidas digitales
 pinMode(LED_BUILTIN, OUTPUT);
 digitalWrite(RL1, LOW); digitalWrite(RL2, LOW);
  
  esp_task_wdt_init( 5, true);                           //habilite el pánico para que ESP32 se reinicie 5s
  esp_task_wdt_add(NULL);                                //agregar hilo actual al reloj WDT

  timer = timerBegin(0, 80, true);                      /* 1 tic toma 1/(80MHZ/80) = 1us, así que configuramos el divisor 80 y contamos hacia arriba.*/  
  timerAttachInterrupt(timer, &onTimer, true);          /* Adjunte la función onTimer a nuestro temporizador */   
  timerAlarmWrite(timer, 1000000, true);                /* Configure la alarma para activar la función de temporizador cada segundo 1 tic es 1us => 1 segundo es 1000000us*/   
  timerAlarmEnable(timer);                              /* iniciar una alarma */
  // u8g2.setBusClock(400000);                         //velocidad 100khz modo compatible default, "fast" mode is 400Khz. 
 //  u8g2.setI2CAddress(0x3C * 2);                     //direccion de oled 0x3C predeterminado, 0x3D alternativo
  u8g2.begin();                                      //Inicializamos el dispositivo oled
  u8g2.setDrawColor(1);                              //Modo blanco y negro
  u8g2.setFlipMode(0);                               //Mantener rotación en 0° (0), 180° (1)
  u8g2.clear();                                      //Borra buffer, pantalla
  u8g2.drawXBMP(0,3,128,64,logoford);                //Ejecuta logo FORD
  u8g2.sendBuffer();                                 //Envia datos al oled
  delay(800);                                       
  u8g2.clear();                                      //Borrar pantalla
  EasyBuzzer.setPin(buzzer);                         //Selecciona el pin de salida
  EasyBuzzer.beep( 2000, 500 );                      //Ajusta Frecuencia en Hz, Duración beep en ms                           
  EasyBuzzer.stopBeep();                             //Inicia buzzer en modo off
  timer2 = millis(); timer3 = millis(); timer4 = millis(); timer5 = millis();
}

void loop()
{
  esp_task_wdt_reset();

if (millis() > timer2) 
  {
 //*****************************************************************************************//
 while (sample_count < NUM_SAMPLES){sum += analogRead(ni1); sample_count++;}  //Toma de muestras
 p =((float)sum / (float)NUM_SAMPLES * Vref)/ADC + correccionL;  // Promedio de muestras tomadas 

        // Lectura voltaje del sensor de min 0.72v a max 3.0v
 m = constrain(p,niMin,niMax)*10;                     //Solo se enfoca en un rango de voltaje
 L = map(m,(niMin*10),(niMax*10),1,100);
 w = map(m,(niMin*10),(niMax*10),15,55);               //mapea valores minimos y maximos del nivel en litros
 m1A = map(w,15,55,135,180);                          //mapea valores para ajutar la aguja 0° a 45° 
 sample_count = 0; sum = 0;                           //Reestablece los contadores
 timer2 = millis() + 10000UL;                         // Refresco cada 10s
  }
if (millis() > timer3) 
  { //****************************************************************************************///
 while (sample_count < NUM_SAMPLES)
 {sum += analogRead(vo); sample_count++;}             //Toma de muestras
  p3 =((float)sum / NUM_SAMPLES * Vref)/ADC;           // Promedio de muestras tomadas
  volt = (p3/((float)RV2/(RV1+RV2)))+correccionV;      // Lectura Voltaje final
 sample_count = 0; sum = 0;                            // Reestablece los contadores 
 termistor();                                          //ejecuta lecturas termistor
 w2= steinhart;                                        //Temperatura guardada en variable w2
 p2= constrain(steinhart,60,130);                      // limita valores entre rangos grafica
 if (p2 < 60) { p2=60;}                                // Establece p2 en 60 a todo dato que sea menor de 60
 if (p2 > 130) { p2=130;}                              // Establece p2 en 130 a todo dato que sea mayor de 130
 m2A = map(p2,60,130,45,0);                            // grafica segun los mapeos de valores angulares                                         
 timer3 = millis() + 2000UL;                           // Refresco cada 3s
  }

if (millis() > timer4) 
 {
  Serial.print(F("N:")); Serial.print(w,1);
  Serial.print(F(" N%:")); Serial.print(L,1);
  Serial.print(F(" B:")); Serial.print(volt,1);  
  Serial.print(F(" T:")); Serial.println(steinhart,1);
  if (w2>=115){Serial.println(F("Alarma alta Temperatura"));}
  /*SerialBT.print(F("Nivel:")); SerialBT.print(w,1);
  SerialBT.print(F("ADC Vol: ")); SerialBT.print(p,2);
  SerialBT.print(F(" N%:")); SerialBT.print(L,1);
  SerialBT.print(F("  Bat:")); SerialBT.print(volt,1);  
  SerialBT.print(F("  Temp:")); SerialBT.println(steinhart,1); */
  timer4 = millis() + 4000UL;                             // Refresco cada 4s
 }
if (ActualizarOLED)                                 
  {
 //*****************************************************************************************//
 gauge(m1A); gauge2(m2A); voltimetro(); ventilador();     // Preparar datos en pantalla oled    
 
 u8g2.sendBuffer();                                       // Enviar todos los datos
 //timer = millis() + 500UL;                              // Refresco cada 500ms
  }
////////////////////////////////////////////////////////////////////////////////////
  if (w2>=94) {RL1s=true; digitalWrite(RL1,HIGH); timer5=millis();}    //Ventilador baja on  97°C
  if (w2<=87 && millis()-timer5>=8000) {RL1s=false; digitalWrite(RL1,LOW);}  //Ventilador baja off luego de 8s
  if (w2>=102){RL2s=true; digitalWrite(RL2,HIGH);}                     // //Ventilador alta on 102°C         
  if (w2<97) {RL2s=false; digitalWrite(RL2,LOW);}                     // Ventilador alta off
  if (w2>=115){EasyBuzzer.update();}                                  // Alarma Alta temperatura on
  if (w2<114) {EasyBuzzer.stopBeep();}                               // Alarma Alta temperatura off
}

/*
El Sketch usa 316981 bytes (24%) del espacio de almacenamiento de programa. El máximo es 1310720 bytes.
Las variables Globales usan 23436 bytes (7%) de la memoria dinámica, dejando 304244 bytes para las variables locales. El máximo es 327680 bytes.
*/
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module