#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Grupo de pinos
const int PinPot = 15;
const int CH_Back = 16;
const int CH_Forward = 17;
const int LED_Pump = 25;
const int LED_Valv1 = 26;
const int LED_Valv2 = 27;
// Grupo de valores
const float Constant_ADC1 = (float) 10000 / 4095;
int LV_Tank, LV_Dig = 0;
int Valvs_Info[3] = {0, 0, 0};
int Select_identity = 0;
float LV_Volts;
// Funções gerais
int Condicionamento(int LVL_Digital);
float Convert_Dig_to_Volts(int LVL_Digital);
void Valvs_Def(int Valvs[], int percentage);
void LED_Control(int Valvs[]);
void Serial_Control();
void LCD_Control(int Valvs[]);
void setup() {
// Inicialização do display
lcd.init(); // Use lcd.init() no lugar de lcd.begin() se estiver utilizando a biblioteca LiquidCrystal_I2C atualizada
lcd.backlight();
// Configuração da banda de leitura até 12 bits
analogReadResolution(12);
analogSetWidth(12);
// Pinos
pinMode(PinPot, INPUT);
pinMode(CH_Back, INPUT);
pinMode(CH_Forward, INPUT);
pinMode(LED_Pump, OUTPUT);
pinMode(LED_Valv1, OUTPUT);
pinMode(LED_Valv2, OUTPUT);
Serial.begin(115200); // Inicializa a comunicação serial
}
void loop() {
// Definição de valores centrais e iniciais para futuro processamento
int LV_TankMax = 10000;
LV_Dig = analogRead(PinPot); // Lê o valor do potenciômetro
LV_Tank = Condicionamento(LV_Dig);
LV_Volts = Convert_Dig_to_Volts(LV_Dig);
// Mecanismo de Válvulas
int percentage = (LV_Tank * 100) / LV_TankMax; // Calcula a porcentagem corretamente
Valvs_Def(Valvs_Info, percentage);
LED_Control(Valvs_Info);
// Tela IHM e serial
LCD_Control(Valvs_Info);
Serial_Control();
delay(680);
lcd.clear();
}
int Condicionamento(int LVL_Digital) {
float LVL_Liquid = LVL_Digital * Constant_ADC1; // Valor digital de acordo com nível do tanque
return (int) LVL_Liquid;
}
float Convert_Dig_to_Volts(int LVL_Digital) {
float LVL_Energy = LVL_Digital * 0.80586e-3; // Tensão de acordo com valor do sensor
return LVL_Energy;
}
void Valvs_Def(int Valvs[], int percentage) {
//controle das valvulas por meio da porcentagem
//armazenamento em array de 3 índices
if (percentage <= 25) {
Valvs[0] = 1;
Valvs[1] = 0;
Valvs[2] = 1;
} else if (percentage > 25 && percentage <= 50) {
Valvs[0] = 1;
Valvs[1] = 0;
Valvs[2] = 1;
} else if (percentage > 50 && percentage <= 75) {
Valvs[0] = 1;
Valvs[1] = 1;
Valvs[2] = 1;
} else if (percentage > 75) {
Valvs[0] = 0;
Valvs[1] = 1;
Valvs[2] = 0;
}
}
void LED_Control(int Valvs[]) {
//Ativação dos LEDs pelo mecanismo de indexação dos arrays
digitalWrite(LED_Pump, Valvs[2]);
digitalWrite(LED_Valv1, Valvs[0]);
digitalWrite(LED_Valv2, Valvs[1]);
}
void Serial_Control() {
Serial.print("Litros no tanque: ");
Serial.println(LV_Tank);
Serial.print("Valor digital (0-4095): ");
Serial.println(LV_Dig);
Serial.print("Valor de tensão: ");
Serial.println(LV_Volts, 3); // Define a precisão para 3 casas decimais
Serial.print("Ordem de ativação das válvulas [0, 1] e bomba [2]: ");
Serial.print(Valvs_Info[0]);
Serial.print(", ");
Serial.print(Valvs_Info[1]);
Serial.print(", ");
Serial.print(Valvs_Info[2]);
Serial.println();
}
void LCD_Control(int Valvs[]) {
//SIstema de passo com botão "pra frente" e "pra trás"
//Nivel-->Digital--> Tensao--> info. das válvulas
if (digitalRead(CH_Back) == HIGH && Select_identity > -1) {
Select_identity--;
}
if (digitalRead(CH_Forward) == HIGH && Select_identity < 4) {
Select_identity++;
}
//limitação da taxa de passo de 0-3
if (Select_identity >= 4) {
Select_identity = 3;
}
else if (Select_identity <= -1) {
Select_identity = 0;
}
//Switch simples para comutar o a identidade expressa no LCD
switch (Select_identity) {
case 0:
lcd.setCursor(0, 0);
lcd.print("Nivel: ");
lcd.print(LV_Tank);
lcd.print(" L");
break;
case 1:
lcd.setCursor(0, 0);
lcd.print("Valor dig.: ");
lcd.print(LV_Dig);
break;
case 2:
lcd.setCursor(0, 0);
lcd.print("Tensao: ");
lcd.print(LV_Volts, 2); // Define a precisão para 2 casas decimais
lcd.print(" V");
break;
case 3:
lcd.setCursor(0, 0);
lcd.print("Valvulas/bomba:");
lcd.setCursor(0, 1);
lcd.print(Valvs[0]);
lcd.print(", ");
lcd.print(Valvs[1]);
lcd.print(", ");
lcd.print(Valvs[2]);
break;
default:
break;
}
}