//En este la bomba se apaga desde el nivel 10 y empieza a decir
//Desbordamiento desde el 11
// LCD pins details: https://docs.wokwi.com/parts/wokwi-lcd1602
// Includes the library to allow the I2C communication from the micro to the LCD
#include <Wire.h> // https://www.arduino.cc/reference/en/language/functions/communication/wire/
// Includes the library used to manage the LCD through I2C (Inter-Integrated Circuit) communication protocol
#include <LiquidCrystal_I2C.h>
/*
https://www.arduino.cc/reference/en/libraries/liquidcrystal-i2c/
The LiquidCrystal_I2C.h Library configures the pin D22 of ESP32 as SDA (Serial DAta lne) to send the data to the LCD,
while pin D21 is configured as SCL (Serial CLock line) to sinchronize the serial data transmission between ESP32 & LCD.
https://youtu.be/CAvawEcxoPU
*/
#define PinAnalogo 35
#define pinbomba 33
int Valor, ValorDigital, nivel,i; // Variable que obtendrá el valor digital después de la conversión
float volts;
LiquidCrystal_I2C I2C_LCD1(0x27, 16, 2); // set the LCD address as the device 0x27 (010 0111) with a 16 chars and 2 line display
LiquidCrystal_I2C I2C_LCD2(0x28, 16, 2); // set the LCD address as the device 0x28 (010 1000) with a 16 chars and 2 line display
byte flechas[8] = {
B11111, // ◼︎◼︎◼︎◼︎◼︎
B11111, // ◼︎◼︎◼︎◼︎◼︎
B11111, // ◼︎◼︎◼︎◼︎◼︎
B11111, // ◼︎◼︎◼︎◼︎◼︎
B11111, // ◼︎◼︎◼︎◼︎◼︎
B11111, // ◼︎◼︎◼︎◼︎◼︎
B11111, // ◼︎◼︎◼︎◼︎◼︎
B11111 // ◼︎◼︎◼︎◼︎◼︎
};
void setup()
{
pinMode(pinbomba,OUTPUT);
// Establecer resolución de 12 bits (0-4095 -> 4096), 10 bits (0-1023 -> 1024), 8 bits (0-255 -> 256)
analogReadResolution(8);
// Initialize I2C communication with the LCD one, two and three
I2C_LCD1.init();
I2C_LCD2.init();
I2C_LCD2.createChar(0, flechas);
// Turn ON the backlight of the LCD one, two and three
I2C_LCD1.backlight();
I2C_LCD2.backlight();
}
void loop()
{
ValorDigital = analogRead(PinAnalogo);
Valor = ValorDigital;
// Convertir el equivalente en volts que se convirtieron
volts = ValorDigital * (3.3/255);
//Calcular el valor del nivel del agua del 0 al 9
for (nivel=0; nivel<16 && Valor>0; nivel++, Valor-=255/16); //nivel
// Clear the LCD one, two and three of any previous message
I2C_LCD1.clear();
I2C_LCD2.clear();
// Display the first message in the first line of the LCD one, two and three
I2C_LCD1.print("NIVEL:");
I2C_LCD1.print(nivel);
for (i =0;i<nivel;i++){
I2C_LCD2.setCursor(i,0);
I2C_LCD2.write(byte(0));
}
if (nivel<14){
I2C_LCD1.setCursor(0,1);
I2C_LCD1.print("BOMBA:ACTIVA");
digitalWrite(pinbomba,HIGH);
}
if (nivel>=14){
I2C_LCD1.setCursor(0,1);
I2C_LCD1.print("BOMBA:INACTIVA");
digitalWrite(pinbomba,LOW);
}
if (nivel>14){
I2C_LCD2.setCursor(0,1);
I2C_LCD2.print("DESBORDAMIENTO!!");
}
delay(2000);
}