/*
-------------------------------------------------------------------------
Programa de controle de Robô limpador de placa solar
Empresa: SUN CLEAR
Autor: Gabriel Luz
Versão: alpha 3.0 (Refatorado)
-------------------------------------------------------------------------
*/
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <RTClib.h>
#include <SPI.h>
#include <SD.h>
// Definição dos pinos
const int M_ROBO = 13;
const int M_ESCOV = 12;
const int M_BOMBA = 26;
const int FC_INI = 14;
const int FC_END = 27;
const int BT_A = 5;
const int BT_B = 16;
const int BT_C = 17;
const int BT_D = 4;
const int CS_PIN = 25; // Pino CS do microSD
// Inicialização de componentes
LiquidCrystal_I2C lcd(0x27, 20, 4);
RTC_DS3231 rtc;
File myFile;
// Variáveis globais
int H_agenda = 0, M_agenda = 0, intervalo = 1, menu = 0, sel = 0;
bool bt_s1 = false, bt_s2 = false, bt_s3 = false, bt_s4 = false;
void setup() {
Serial.begin(115200);
Wire.begin();
lcd.begin(20, 4);
rtc.begin();
SD.begin(CS_PIN);
// Configuração de pinos
pinMode(M_ROBO, OUTPUT);
pinMode(M_ESCOV, OUTPUT);
pinMode(M_BOMBA, OUTPUT);
pinMode(FC_INI, INPUT);
pinMode(FC_END, INPUT);
pinMode(BT_A, INPUT);
pinMode(BT_B, INPUT);
pinMode(BT_C, INPUT);
pinMode(BT_D, INPUT);
lcd.setCursor(5, 1);
lcd.print("SUN CLEAR");
delay(2000);
lcd.clear();
}
void loop() {
if (menu == 1 && sel == 0) editarHora();
else if (menu == 1 && sel == 1) configurarAgenda();
else if (menu == 1 && sel == 2) iniciarLimpeza();
else mostrarMenu();
delay(10);
}
void verificarBotao(int botao, bool &bt_status, int &valor, int min, int max, bool incrementar) {
if (digitalRead(botao) == HIGH && !bt_status) {
bt_status = true;
valor = incrementar ? (valor < max ? valor + 1 : min) : (valor > min ? valor - 1 : max);
}
if (digitalRead(botao) == LOW) bt_status = false;
}
void configurarAgenda() {
lcd.clear();
lcd.print("Hora: ");
lcd.setCursor(8, 0); lcd.print(":");
lcd.setCursor(0, 1); lcd.print("Intervalo (dias):");
int select = 0;
while (menu == 1) {
lcd.setCursor(6, 0); lcd.print(H_agenda);
lcd.setCursor(9, 0); lcd.print(M_agenda);
lcd.setCursor(9, 1); lcd.print(intervalo);
verificarBotao(BT_A, bt_s1, select, 0, 2, true);
verificarBotao(BT_B, bt_s2, select, 0, 2, false);
verificarBotao(BT_C, bt_s3, menu, 0, 1, true);
verificarBotao(BT_D, bt_s4, menu, 0, 1, false);
delay(10);
}
myFile = SD.open("/dados.txt", FILE_WRITE);
if (myFile) {
myFile.printf("%02d:%02d, %d dias\n", H_agenda, M_agenda, intervalo);
myFile.flush();
myFile.close();
Serial.println("Dados salvos no SD!");
} else {
Serial.println("Erro ao abrir o arquivo!");
}
}
void editarHora() {
lcd.clear();
DateTime now = rtc.now();
int H = now.hour(), M = now.minute(), S = now.second();
int DAY = now.day(), MONTH = now.month(), Y = now.year();
int select = 0;
while (menu == 1) {
lcd.setCursor(6, 0); lcd.print(H);
lcd.setCursor(9, 0); lcd.print(M);
lcd.setCursor(12, 0); lcd.print(S);
lcd.setCursor(6, 1); lcd.print(DAY);
lcd.setCursor(9, 1); lcd.print(MONTH);
lcd.setCursor(12, 1); lcd.print(Y);
verificarBotao(BT_A, bt_s1, select, 0, 5, true);
verificarBotao(BT_B, bt_s2, select, 0, 5, false);
verificarBotao(BT_C, bt_s3, menu, 0, 1, true);
verificarBotao(BT_D, bt_s4, menu, 0, 1, false);
delay(10);
}
rtc.adjust(DateTime(Y, MONTH, DAY, H, M, S));
}
void iniciarLimpeza() {
lcd.clear();
lcd.print("Limpando...");
digitalWrite(M_ESCOV, HIGH);
digitalWrite(M_BOMBA, HIGH);
delay(5000);
digitalWrite(M_ESCOV, LOW);
digitalWrite(M_BOMBA, LOW);
menu = 0;
lcd.clear();
}
void mostrarMenu() {
lcd.setCursor(0, 0); lcd.print(sel == 0 ? "> Editar Hora" : " Editar Hora");
lcd.setCursor(0, 1); lcd.print(sel == 1 ? "> Configurar Agenda" : " Configurar Agenda");
lcd.setCursor(0, 2); lcd.print(sel == 2 ? "> Iniciar Limpeza" : " Iniciar Limpeza");
verificarBotao(BT_A, bt_s1, sel, 0, 2, true);
verificarBotao(BT_B, bt_s2, sel, 0, 2, false);
verificarBotao(BT_C, bt_s3, menu, 0, 1, true);
verificarBotao(BT_D, bt_s4, menu, 0, 1, false);
}