#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
const int relayPin = 7; // Pino do relé
const int sensorPin = A0; // Pino do sensor ACS712
LiquidCrystal_I2C lcd(0x27, 16, 2); // Substitua 0x27 pelo endereço I2C correto
const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {9, 8, 7, 6};
byte colPins[COLS] = {5, 4, 3, 2};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
float weight = 0.0;
int mils = 0;
float current = 0.0;
float timeToRun = 0.0;
unsigned long endTime = 0;
bool running = false;
bool inputWeight = true;
String weightStr = "000,0";
String milsStr = "";
unsigned long lastCalculationTime = 0;
void setup() {
pinMode(relayPin, OUTPUT);
digitalWrite(relayPin, LOW);
lcd.begin(16, 2); // Inicializa o LCD com 16 colunas e 2 linhas
lcd.backlight();
displayInitialScreen();
}
void loop() {
if (!running) {
handleInput();
} else {
unsigned long currentTime = millis();
if (currentTime - lastCalculationTime >= 1000) {
measureCurrent();
calculateTime();
lastCalculationTime = currentTime;
}
controlRelay();
displayRemainingTime();
}
}
void handleInput() {
char key = keypad.getKey();
if (key) {
if (inputWeight) {
if (key == '#') {
inputWeight = false;
lcd.setCursor(0, 1);
lcd.print("Mls: ");
lcd.setCursor(6, 1);
lcd.print(milsStr);
} else if (key == 'B') {
cancelOperation();
} else if (key == 'D') {
weightStr = "000,0";
lcd.setCursor(6, 0);
lcd.print(weightStr);
} else {
weightStr = updateWeightString(weightStr, key);
lcd.setCursor(6, 0);
lcd.print(weightStr);
}
} else {
if (key == 'A') {
mils = milsStr.toInt();
startOperation();
} else if (key == 'B') {
cancelOperation();
} else if (key == 'D') {
milsStr = "";
lcd.setCursor(6, 1);
lcd.print(" ");
} else if (key == '#') {
inputWeight = true;
lcd.setCursor(6, 0);
lcd.print(weightStr);
} else {
milsStr += key;
lcd.setCursor(6, 1);
lcd.print(milsStr);
}
}
}
}
String updateWeightString(String current, char newChar) {
current.remove(0, 1); // Remove o primeiro caractere
int commaIndex = current.indexOf(',');
current.remove(commaIndex, 1); // Remove a vírgula
current += newChar; // Adiciona o novo caractere no final
current = current.substring(0, commaIndex) + ',' + current.substring(commaIndex); // Reposiciona a vírgula
return current;
}
void measureCurrent() {
int sensorValue = analogRead(sensorPin);
current = ((float)sensorValue - 512) * (5.0 / 1023.0);
}
void calculateTime() {
if (current != 0) {
timeToRun = (weight * mils) / (68.0 * current);
endTime = millis() + (unsigned long)(timeToRun * 1000); // Tempo final em milissegundos
}
}
void controlRelay() {
if (millis() >= endTime) {
digitalWrite(relayPin, LOW);
running = false;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Operacao");
lcd.setCursor(0, 1);
lcd.print("Concluida");
delay(3000); // Aguardar 3 segundos antes de reiniciar
displayInitialScreen(); // Exibir a tela inicial novamente
} else {
digitalWrite(relayPin, HIGH);
}
}
void displayRemainingTime() {
unsigned long remainingTime = (endTime - millis()) / 1000;
int minutes = remainingTime / 60;
int seconds = remainingTime % 60;
lcd.setCursor(0, 0);
lcd.print("Tempo restante: ");
lcd.setCursor(0, 1);
lcd.print(minutes);
lcd.print(":");
if (seconds < 10) {
lcd.print("0");
}
lcd.print(seconds);
lcd.print(" "); // Espaços para garantir que não há resíduos de caracteres anteriores
}
void startOperation() {
weight = weightStr.toFloat();
mils = milsStr.toInt();
running = true;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Em Execucao");
measureCurrent(); // Medir a corrente imediatamente antes de iniciar a operação
calculateTime(); // Calcular o tempo de operação com base na corrente medida
}
void cancelOperation() {
running = false;
digitalWrite(relayPin, LOW);
weight = 0.0;
mils = 0;
weightStr = "000,0";
milsStr = "";
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Operacao");
lcd.setCursor(0, 1);
lcd.print("Cancelada");
delay(3000); // Aguardar 3 segundos antes de reiniciar
displayInitialScreen(); // Exibir a tela inicial novamente
}
void displayInitialScreen() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Peso: 000,0");
lcd.setCursor(0, 1);
lcd.print("Mls: ");
inputWeight = true;
}