Estoy haciendo un proyecto para la escuela con ESP32-S3 donde un servomotor gira cuando llega uno de 3 horarios seleccionados en un TFT pero al momento de alimentarlo con una fuente de 5V el servo empieza a girar solo y no para
#include "FS.h"
#include <SPI.h>
#include <TFT_eSPI.h>
#include <Wire.h>
TFT_eSPI tft = TFT_eSPI();
#include <Time.h>
#include <TimeAlarms.h>
#include <EEPROM.h>
#include "RTClib.h"
RTC_DS3231 rtc;
#include <pwmWrite.h>
Pwm pwm = Pwm();
// Este es el nombre del archivo utilizado para almacenar los datos de calibración
// de las coordenadas táctiles. Cambiar el nombre para iniciar una nueva calibración.
#define CALIBRATION_FILE "/TouchCalData7"
// Establecer REPEAT_CAL en verdadero en lugar de falso para ejecutar la calibración
// nuevamente, de lo contrario solo se hará una vez.
// Repetir la calibración si cambia la rotación de la pantalla.
#define REPEAT_CAL false
int X; // Variables que almacenaran la coordenada
int Y; // X, Y donde presionemos y la variable Z
int menu = 0;
boolean grafico0 = false;
boolean grafico1 = false; //para no recarga display innecesariamente
boolean grafico2 = false;
boolean grafico3 = false;
boolean grafico4 = false;
int alarmaSeleccionada = 0;
int hora;
int minuto;
String input = "";
bool isEnteringHour = true;
String alarmTimeInput = "";
bool rectanguloDibujado = false;
bool SwitchOn[4]; // Seguimiento del estado de 4 botones MENU 2
// Dimensiones del botón MENU 2
const int BUTTON_WIDTH = 90; // ANCHO de cada boton
const int BUTTON_HEIGHT = 30; // ALTO de cada boton
// Posiciones de los botones MENU 2
const int BUTTON_X = 130; // Posición X ajustada
const int BUTTON_Y[4] = {56, 116, 176, 236}; // Posicion Y botones
const int EEPROM_ADDR[4] = {10, 11, 12, 13}; // Alojamiento de los valores de los botones en la EPPROM
// Pines Servos
const int servoPin1 = 40;
//-------------alarma------------------------
time_t getTime() {
DateTime now = rtc.now();
return now.unixtime(); // Convierte DateTime a time_t
}
int hora1, hora2, hora3;
int minuto1, minuto2, minuto3;
bool alarma1 = true; // variable de control para la primera alarma
bool alarma2 = true; // variable de control para la segunda alarma
bool alarma3 = true; // variable de control para la tercera alarma
const int EEPROM_SIZE = 16;
//------------------------------------------------------------------------------------------
void setup()
{
Serial.begin(9600); // Abre comunicacion al puerto serial
Wire.begin(19,20); //Pines SDA , SCL para DS3231
EEPROM.begin(EEPROM_SIZE); // Inicializa la biblioteca EEPROM y prepara la memoria para ser leída o escrita
if (! rtc.begin())
{
Serial.println("No se encontro el RTC");
Serial.flush(); // asegura que el mensaje anterior se envio
// while (1)
delay(10);
}
//rtc.adjust(DateTime(2023, 10, 06, 12, 16, 59));
//if (rtc.lostPower())
//{
//Serial.println("RTC lost power, let's set the time!");
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
//rtc.adjust(DateTime(2023, 10, 05, 23, 28, 30));
delay(5000);
//}
setSyncProvider(getTime); // Configura getTime como el proveedor de sincronización
if (timeStatus() != timeSet) {
Serial.println("Unable to sync with the RTC");
} else {
Serial.println("RTC has set the system time");
}
tft.begin(); // Inicia la pantalla
touch_calibrate(); // Calibra la pantalla touch con esta funcion
tft.setRotation(0); // Define como rotacion de la pantalla en vertical
menu0(); // Se ejecuta el menu0 al iniciar el sistema
pwm.writeServo(servoPin1, 0);
}
//-------------------------------------MENU 0 (MENU INICIAL)-----------------------------------------------------
void menu0() { // menu inicio
if (grafico0 == false){
// menu colores
tft.fillScreen(TFT_DARKGREY); // FONDO DE PANTALLA
tft.setTextSize(3);tft.setTextColor(TFT_BLACK);
tft.setCursor(15, 15);tft.print("Dispenser de medicamentos");
tft.drawRoundRect(10,130,218,72,5, TFT_WHITE);
tft.drawRoundRect(10,210,218,72,5, TFT_WHITE);
tft.fillRoundRect(11, 132,216, 70, 5, TFT_BLUE);
tft.fillRoundRect(11, 212,216, 70, 5, TFT_BLUE);
tft.setTextSize(3);tft.setTextColor(TFT_WHITE);
tft.setCursor(25, 143);tft.print("Configurar Alarma"); //Para el boton "configurar alarma"
tft.setCursor(22, 235);tft.print("Informacion"); // Para el boton "Informacion"
grafico0 = true;
}
}
void horaMenu0() { // Funcion que mostrara la Hora:Minutos:Segundos en tiempo real
DateTime now = rtc.now();
// Establecer el color del texto
tft.setTextColor(TFT_YELLOW, TFT_DARKGREY);
tft.setCursor(15, 90);
tft.setTextSize(3);
// Imprimir la hora
tft.print(String(now.hour()));
// Imprimir los minutos con dos dígitos
tft.print(":" + String(now.minute() < 10 ? "0" + String(now.minute()) : String(now.minute())));
// Imprimir los segundos con dos dígitos
tft.print(":" + String(now.second() < 10 ? "0" + String(now.second()) : String(now.second())));
delay(1000); // Espera 1 segundo antes de actualizar de nuevo
}
// FUNNCION PARA LEER EL MENU ININCIAL
void lecturaMenu0() {
uint16_t t_x = 0, t_y = 0; // Variables para guardar las coordenadas
bool pressed = tft.getTouch(&t_x, &t_y); // Lee las coordenadas de la pantalla táctil
if (pressed) {
// Verifica si las coordenadas están dentro del área del botón "Configurar Alarma"
if ((t_x > 10 && t_x < 228) && (t_y > 130 && t_y < 202)) {
// Cambiar el estado del menú a 1 para indicar que se debe abrir otro menú
menu = 1;
}
// Verifica si las coordenadas están dentro del área del botón "Informacion"
else if ((t_x > 10 && t_x < 228) && (t_y > 210 && t_y < 282)) {
// Cambiar el estado del menú a 2 para indicar que se debe abrir otro menú
menu = 2;
}
}
}
//-------------------------------------MENU 1 (MENU SELECCION PASTILLA)-----------------------------------------------------
void menu1() {
if (!grafico1) {
tft.fillScreen(TFT_DARKGREY);
tft.drawRoundRect(10, 10, 218, 50, 5, TFT_WHITE);
tft.drawRoundRect(10, 68, 218, 50, 5, TFT_WHITE);
tft.drawRoundRect(10, 126, 218, 50, 5, TFT_WHITE);
tft.drawRoundRect(10, 184, 218, 50, 5, TFT_WHITE);
tft.fillRoundRect(11, 11, 216, 48, 5, TFT_BLUE);
tft.fillRoundRect(11, 69, 216, 48, 5, TFT_BLUE);
tft.fillRoundRect(11, 127, 216, 48, 5, TFT_BLUE);
tft.fillRoundRect(11, 185, 216, 48, 5, TFT_BLUE);
tft.setTextSize(2);
tft.setTextColor(TFT_WHITE);
tft.setCursor(16, 17);
tft.print("Configurar");
tft.setCursor(16, 37);
tft.print("Pastilla 1");
tft.setCursor(16, 76);
tft.print("Configurar");
tft.setCursor(16, 97);
tft.print("Pastilla 2");
tft.setCursor(16, 134);
tft.print("Configurar");
tft.setCursor(16, 154);
tft.print("Pastilla 3");
tft.drawRoundRect(10, 300, 80, 20, 5, TFT_WHITE);
tft.fillRoundRect(11, 301, 78, 18, 5, TFT_GREEN);
tft.setTextSize(2);
tft.setTextColor(TFT_BLACK);
tft.setCursor(14, 305);
tft.print("VOLVER");
grafico1 = true;
}
}
// FUNCION PARA LEER EL MENU SELECCION
void lecturaMenu1() {
delay(1000);
uint16_t t_x = 0, t_y = 0;
bool pressed = tft.getTouch(&t_x, &t_y);
if (pressed) {
if ((t_x > 11 && t_x < 227) && (t_y > 11 && t_y < 59)) {
alarmaSeleccionada = 1;
//drawKeypad();
menu = 3;
Serial.println("Configurar Pastilla 1 seleccionada");
} else if ((t_x > 11 && t_x < 227) && (t_y > 69 && t_y < 117)) {
alarmaSeleccionada = 2;
//drawKeypad();
menu = 3;
Serial.println("Configurar Pastilla 2 seleccionada");
} else if ((t_x > 11 && t_x < 227) && (t_y > 127 && t_y < 175)) {
alarmaSeleccionada = 3;
//drawKeypad();
menu = 3;
Serial.println("Configurar Pastilla 3 seleccionada");
} else if ((t_x > 10 && t_x < 90) && (t_y > 300 && t_y < 320)) {
menu = 0;
Serial.println("Volver presionado");
}
}
}
//-------------------------------------MENU 2 (MENU CONFIGURACION)-----------------------------------------------------
void menu2() { // DIBUJA EL MENU
if (grafico2 == false) {
tft.fillScreen(TFT_DARKGREY);
tft.setTextSize(2);tft.setTextColor(TFT_WHITE);
tft.setCursor(77, 7); tft.print("Alarmas");
tft.setTextSize(1); tft.setTextColor(TFT_WHITE);
tft.setCursor(16, 41); tft.print("Pastilla 1");
tft.setCursor(16, 101); tft.print("Pastilla 2");
tft.setCursor(16, 161); tft.print("Pastilla 3");
tft.drawLine(16, 91, 224, 91, TFT_BLACK);
tft.drawLine(16, 151, 224, 151, TFT_BLACK);
tft.drawLine(16, 211, 224, 211, TFT_BLACK);
tft.drawRoundRect(10,300, 80,20, 5, TFT_WHITE);
tft.fillRoundRect(11,301, 78,18, 5, TFT_GREEN);
tft.setTextSize(2); tft.setTextColor(TFT_BLACK);
tft.setCursor(14, 305);tft.print("VOLVER");
for (int i = 0; i < 4; i++) {
SwitchOn[i] = EEPROM.read(EEPROM_ADDR[i]);
drawButton(i, SwitchOn[i]);
}
grafico2 = true;
}
}
void drawButton(int index, bool state) // Dibuja los botones para activar/descativar Alarmas
{
int y = BUTTON_Y[index]; // Posición Y basada en el índice
if (state) {
tft.fillRoundRect(BUTTON_X + BUTTON_WIDTH / 2, y, BUTTON_WIDTH / 2, BUTTON_HEIGHT,5, TFT_GREEN);
tft.fillRect(BUTTON_X, y, BUTTON_WIDTH / 2, BUTTON_HEIGHT, TFT_DARKGREY);
tft.setTextColor(TFT_WHITE);
tft.setTextSize(2);
tft.setTextDatum(MC_DATUM);
tft.drawString("OFF", BUTTON_X + BUTTON_WIDTH / 4, y + BUTTON_HEIGHT / 2);
} else {
tft.fillRoundRect(BUTTON_X, y, BUTTON_WIDTH / 2, BUTTON_HEIGHT,5, TFT_RED);
tft.fillRect(BUTTON_X + BUTTON_WIDTH / 2, y, BUTTON_WIDTH / 2, BUTTON_HEIGHT, TFT_DARKGREY);
tft.setTextColor(TFT_WHITE);
tft.setTextSize(2);
tft.setTextDatum(MC_DATUM);
tft.drawString("ON", BUTTON_X + 3 * BUTTON_WIDTH / 4, y + BUTTON_HEIGHT / 2);
}
tft.drawRoundRect(BUTTON_X, y, BUTTON_WIDTH, BUTTON_HEIGHT, 5, TFT_BLACK);
}
bool checkButton(uint16_t x, uint16_t y, int index)
{
int btnY = BUTTON_Y[index];
return (x > BUTTON_X) && (x < BUTTON_X + BUTTON_WIDTH) && (y > btnY) && (y <= btnY + BUTTON_HEIGHT);
}
void menu2Alarmas(){
tft.setTextSize(3);tft.setTextColor(TFT_WHITE);
tft.setCursor(16, 61); tft.print(hora1); tft.setCursor(52, 61); tft.print(":"); tft.setCursor(70, 61); tft.print(minuto1); // alarma 1
tft.setCursor(16, 121); tft.print(hora2); tft.setCursor(52, 121); tft.print(":"); tft.setCursor(70, 121); tft.print(minuto2); // alarma 2
tft.setCursor(16, 181); tft.print(hora3); tft.setCursor(52, 181); tft.print(":"); tft.setCursor(70, 181); tft.print(minuto3); // alarma 3
}
// FUNCION PARA LEER EL MENU INFORMACION
void lecturaMenu2(){
uint16_t t_x = 0, t_y = 0;
bool pressed = tft.getTouch(&t_x, &t_y);
if (pressed) {
if ((t_x > 10 && t_x < 90) && (t_y > 300 && t_y < 320)) {
menu = 0;
Serial.println("Volver presionado");
} else { for (int i = 0; i < 4; i++) {
if (checkButton(t_x, t_y, i)) {
Serial.print("Botón ");
Serial.print(i + 1);
Serial.println(SwitchOn[i] ? " OFF presionado" : " ON presionado");
SwitchOn[i] = !SwitchOn[i];
drawButton(i, SwitchOn[i]);
EEPROM.write(EEPROM_ADDR[i], SwitchOn[i]);
EEPROM.commit();
delay(300); // Retraso de 300ms después de presionar un botón
}
}
}
}
}
//-------------------------------------MENU KEYPAD (MENU CONFIGURAR ALARMA)-----------------------------------------------------
void menuKeypad() {
if (grafico3 == false){
tft.fillScreen(TFT_BLACK); // Clear screen
tft.drawRect(0, 0, 240, 320, TFT_WHITE); // Draw border
tft.drawRect(0, 0, 240, 20, TFT_WHITE);
tft.drawLine(140, 0, 140, 19, TFT_WHITE);
tft.setTextSize(2); // Set text size
tft.setTextColor(TFT_WHITE); // Set text color
//tft.setCursor(143, 2); // Set cursor position for RTC time (adjust as needed)
// tft.print("00:00:00"); // Placeholder for RTC time
tft.setCursor(2, 2); // Set cursor position
tft.print("Alarma"); // Print string
tft.setCursor(82, 2);
tft.print(alarmaSeleccionada);
grafico3 = true;
}}
void horaMenuKeypad() {
DateTime now = rtc.now();
tft.setTextSize(2); // Tamaño del texto
tft.setTextColor(TFT_WHITE, TFT_BLACK); // Color del texto
tft.setCursor(143, 2); // Posición del texto
// Imprimir la hora
tft.print(String(now.hour()));
// Imprimir los minutos con dos dígitos
tft.setCursor(168, 2);
tft.print(":" + String(now.minute() < 10 ? "0" + String(now.minute()) : String(now.minute())));
// Imprimir los segundos con dos dígitos
tft.setCursor(202, 2);
tft.print(":" + String(now.second() < 10 ? "0" + String(now.second()) : String(now.second())));
delay(1000); // Espera 1 segundo antes de actualizar de nuevo
}
void drawKeypad() {
int squareSize = 55;
int padding = 10;
tft.drawRect(27, 37, 185, 25, TFT_WHITE);
int startX = (tft.width() - (3 * squareSize + 2 * padding)) / 2;
int startY = tft.height() - (4 * squareSize + 3 * padding) - 3;
for (int row = 0; row < 4; row++) {
for (int col = 0; col < 3; col++) {
tft.setTextColor(TFT_BLACK); // Set text color
int x = startX + col * (squareSize + padding);
int y = startY + row * (squareSize + padding);
tft.fillRect(x, y, squareSize, squareSize, TFT_WHITE);
if (row == 3 && col == 0) {
// Draw "X" button
tft.setTextSize(3);
tft.setCursor(x + (squareSize - 12) / 2, y + (squareSize - 16) / 2);
tft.setTextColor(TFT_RED); // Set text color
tft.print("X");
} else if (row == 3 && col == 2) {
// Draw "OK" button
tft.setTextSize(2);
tft.setCursor(x + (squareSize - 18) / 2, y + (squareSize - 16) / 2);
tft.setTextColor(TFT_GREEN); // Set text color
tft.print("OK");
} else {
// Draw number button
int number = col + row * 3 + 1;
if (number == 11) {
number = 0;
}
tft.setTextSize(3);
tft.setCursor(x + (squareSize - 12) / 2, y + (squareSize - 16) / 2);
tft.print(number);
}
}
}
tft.setTextColor(TFT_WHITE);
}
// Función para leer el Keypad
void lecturaKeypad() {
uint16_t t_x = 0, t_y = 0; // Variables para guardar las coordenadas
bool pressed = tft.getTouch(&t_x, &t_y); // Lee las coordenadas de la pantalla táctil
if (pressed) {
delay(200);
int squareSize = 55;
int padding = 10;
int startX = (tft.width() - (3 * squareSize + 2 * padding)) / 2;
int startY = tft.height() - (4 * squareSize + 3 * padding) - 3;
for (int row = 0; row < 4; row++) {
for (int col = 0; col < 3; col++) {
int x = startX + col * (squareSize + padding);
int y = startY + row * (squareSize + padding);
if ((t_x > x && t_x < x + squareSize) && (t_y > y && t_y < y + squareSize)) {
String key;
if (row == 3 && col == 0) {
key = "C"; // "X" button
} else if (row == 3 && col == 2) {
key = "OK"; // "OK" button
} else {
int number = col + row * 3 + 1;
if (number == 11) {
number = 0;
}
key = String(number);
}
// Manejar la entrada del keypad aquí
Serial.println("Tecla presionada: " + key);
handleKeypadInput(key);
}
}
}
}
}
void handleKeypadInput(String key) {
if (key == "C") {
alarmTimeInput = ""; // Borrar la entrada
} else if (key == "OK") {
if (alarmTimeInput.length() == 5 && alarmTimeInput.charAt(2) == ':') {
int hour = alarmTimeInput.substring(0, 2).toInt();
int minute = alarmTimeInput.substring(3, 5).toInt();
if (hour >= 0 && hour < 24 && minute >= 0 && minute < 60) {
configurarAlarma(hour, minute);
mostrarMensajeConfiguracion("Alarma configurada");
} else {
mostrarMensajeError("Hora no valida");
}
} else {
mostrarMensajeError("Formato de hora no valido");
}
alarmTimeInput = ""; // Borrar la entrada
} else {
if (alarmTimeInput.length() < 5) { // Limitar a 5 caracteres (HH:MM)
if (alarmTimeInput.length() == 2) {
alarmTimeInput += ":";
}
alarmTimeInput += key;
}
}
updateKeypadDisplay();
}
void updateKeypadDisplay() {
tft.fillRect(27, 37, 185, 25, TFT_BLACK); // Clear the input area
tft.setCursor(30, 40);
tft.setTextSize(2);
tft.setTextColor(TFT_WHITE);
tft.print(alarmTimeInput);
}
void mostrarMensajeError(String mensaje) {
tft.fillRect(27, 70, 185, 25, TFT_RED); // Clear the error area
tft.setCursor(30, 73);
tft.setTextSize(2);
tft.setTextColor(TFT_WHITE);
tft.print(mensaje);
delay(2000); // Esperar 2 segundos antes de limpiar el mensaje
tft.fillRect(27, 70, 185, 25, TFT_BLACK); // Clear the error area
}
void mostrarMensajeConfiguracion(String mensaje) {
tft.fillScreen(TFT_GREEN);
tft.setTextSize(2);
tft.setTextColor(TFT_BLACK);
tft.setCursor(20, tft.height() / 2 - 10);
tft.print(mensaje);
delay(2000); // Mostrar el mensaje por 2 segundos
menu = 0; // Volver al menú principal después de mostrar el mensaje
grafico1 = false; // Resetear el gráfico para que se redibuje el menú principal
}
void mostrarMensajeAlarmaActivada(String mensaje) {
tft.fillScreen(TFT_GREEN);
tft.setTextSize(2);
tft.setTextColor(TFT_BLACK);
tft.setCursor(20, tft.height() / 2 - 10);
tft.print(mensaje);
delay(2000); // Mostrar el mensaje por 2 segundos
menu = 0; // Volver al menú principal después de mostrar el mensaje
grafico1 = false; // Resetear el gráfico para que se redibuje el menú principal
}
//------------------------------------------------------------------------------------------------------------------------------------
void loop() {
Alarm.delay(0); // wait one second between clock display
hora1 = EEPROM.read(1); hora2 = EEPROM.read(3); hora3 = EEPROM.read(5); // Para ir actualizando las horas y minutos
minuto1 = EEPROM.read(2); minuto2 = EEPROM.read(4); minuto3 = EEPROM.read(6); // de la EEPROM todo el tiempo.
/*for (int i = 0; i < 4; i++) {
SwitchOn[i] = EEPROM.read(i); // Leer el estado de cada botón desde EEPROM
}*/
checkAlarmas(); // Funcion para chequear Alarmas
Alarm.timerRepeat(60, mostrarDirecciones); // timer for every 60 seconds
if (menu == 0) {
grafico1 = false; //para no recarga display innecesariamente
grafico2 = false;
grafico3 = false;
grafico4 = false;
menu0(); // Muestra el menú principal
horaMenu0();// Muestra la hora actualizandose
lecturaMenu0(); // Lee las coordenadas y cambia el estado del menú según la entrada del usuario
} else if (menu == 1) {
grafico0 = false; //para no recarga display innecesariamente
grafico2 = false;
grafico3 = false;
grafico4 = false;
menu1(); // Muestra el menú de CONFIGURAR ALARMA
lecturaMenu1();
} else if (menu == 2) { // menu INFORMACION
grafico0 = false;
grafico1 = false; //para no recarga display innecesariamente
grafico3 = false;
grafico4 = false;
menu2(); // Muestra el menú de información
menu2Alarmas();
lecturaMenu2();
} else if (menu == 3) { // menu KEYPAD
grafico0 = false;
grafico1 = false; //para no recarga display innecesariamente
grafico2 = false;
grafico4 = false;
menuKeypad();
drawKeypad();
horaMenuKeypad();
lecturaKeypad();
}
}
void configurarAlarma(int hour, int minute) {
DateTime now = rtc.now();
if (alarmaSeleccionada == 1) {
EEPROM.write( 1, hour );
EEPROM.write( 2, minute );
EEPROM.commit();
Serial.println("Alarma 1 configurada para las " + String(hour) + ":" + String(minute));
} else if (alarmaSeleccionada == 2) {
EEPROM.write( 3, hour );
EEPROM.write( 4, minute );
EEPROM.commit();
Serial.println("Alarma 2 configurada para las " + String(hour) + ":" + String(minute));
} else if (alarmaSeleccionada == 3) {
EEPROM.write( 5, hour );
EEPROM.write( 6, minute );
EEPROM.commit();
Serial.println("Alarma 3 configurada para las " + String(hour) + ":" + String(minute));
}
}
void checkAlarmas(){ // Funcion para chequear la hora actual con las horas de las alarmas y activa las alarmas.
DateTime fecha = rtc.now(); // obtiene la fecha y hora actual en formato DateTime y la asigna a la variable fecha
// Primera alarma
if (fecha.hour() == hora1 && fecha.minute() == minuto1 && fecha.second() == 0 && SwitchOn[0]) {
if (alarma1 == true) {
primeraAlarma();
//mostrarMensajeAlarmaActivada("Alarma 1 ACTIVADA! Pastilla Dispensada"); // Muestra mensaje de la activacion de alarma 1
//menu = 0; // Volver al menú principal después de mostrar el mensaje
//grafico1 = false; // Resetear el gráfico para que se redibuje el menú principal
alarma1 = false; // desactiva la primera alarma
}
}
// Segunda alarma
if (fecha.hour() == hora2 && fecha.minute() == minuto2 && fecha.second() == 0 && SwitchOn[1]) {
if (alarma2 == true) {
segundaAlarma();
//mostrarMensajeAlarmaActivada("Alarma 2 ACTIVADA! Pastilla Dispensada"); // Muestra mensaje de la activacion de alarma 2
//menu = 0; // Volver al menú principal después de mostrar el mensaje
//grafico1 = false; // Resetear el gráfico para que se redibuje el menú principal
alarma2 = false; // desactiva la segunda alarma
}
}
// Tercera alarma
if (fecha.hour() == hora3 && fecha.minute() == minuto3 && fecha.second() == 0 && SwitchOn[2]) {
if (alarma3 == true) {
terceraAlarma();
//mostrarMensajeAlarmaActivada("Alarma 3 ACTIVADA! Pastilla Dispensada"); // Muestra mensaje de la activacion de alarma 3
//menu = 0; // Volver al menú principal después de mostrar el mensaje
//grafico1 = false; // Resetear el gráfico para que se redibuje el menú principal
alarma3 = false; // desactiva la tercera alarma
}
}
if (fecha.hour() == 2 && fecha.minute() == 0) {
alarma1 = true;
alarma2 = true;
alarma3 = true;
}
}
void drawButton(int x, int y, int w, int h, String label) {
tft.drawRoundRect(x, y, w, h, 5, TFT_WHITE);
tft.fillRoundRect(x + 1, y + 1, w - 2, h - 2, 5, TFT_BLUE);
tft.setCursor(x + w / 4, y + h / 4);
tft.setTextSize(2);
tft.setTextColor(TFT_WHITE);
tft.print(label);
}
void primeraAlarma(){
Serial.println("Alarma 1 activada");
pwm.writeServo(servoPin1, 90);
delay(200);
pwm.writeServo(servoPin1, 0);
delay(5000);
Serial.println("Alarma Desactivada");
}
void segundaAlarma(){
Serial.println("Alarma 2 activada");
pwm.writeServo(servoPin1, 90);
delay(200);
pwm.writeServo(servoPin1, 0);
delay(5000);
Serial.println("Alarma Desactivada");
}
void terceraAlarma(){
Serial.println("Alarma 3 activada");
pwm.writeServo(servoPin1, 90);
delay(200);
pwm.writeServo(servoPin1, 0);
delay(5000);
Serial.println("Alarma Desactivada");
}
void mostrarDirecciones(){
Serial.print("hora1: "); Serial.println(hora1);
Serial.print("minuto1: "); Serial.println(minuto1);
Serial.print("hora2: "); Serial.println(hora2);
Serial.print("minuto2: "); Serial.println(minuto2);
Serial.print("hora3: "); Serial.println(hora3);
Serial.print("minuto3: "); Serial.println(minuto3);
}
//------------------------------------------------------------------------------------------
void touch_calibrate() // FUNCION PARA CALIBRAR EL TOUCH DE LA PANTALLA
{
uint16_t calData[5];
uint8_t calDataOK = 0;
// check file system exists
if (!SPIFFS.begin()) {
Serial.println("Formatting file system");
SPIFFS.format();
SPIFFS.begin();
}
// check if calibration file exists and size is correct
if (SPIFFS.exists(CALIBRATION_FILE)) {
if (REPEAT_CAL)
{
// Delete if we want to re-calibrate
SPIFFS.remove(CALIBRATION_FILE);
}
else
{
File f = SPIFFS.open(CALIBRATION_FILE, "r");
if (f) {
if (f.readBytes((char *)calData, 14) == 14)
calDataOK = 1;
f.close();
}
}
}
if (calDataOK && !REPEAT_CAL) {
// calibration data valid
tft.setTouch(calData);
} else {
// data not valid so recalibrate
tft.fillScreen(TFT_BLACK);
tft.setCursor(20, 0);
tft.setTextFont(2);
tft.setTextSize(1);
tft.setTextColor(TFT_WHITE, TFT_BLACK);
tft.println("Touch corners as indicated");
tft.setTextFont(1);
tft.println();
if (REPEAT_CAL) {
tft.setTextColor(TFT_RED, TFT_BLACK);
tft.println("Set REPEAT_CAL to false to stop this running again!");
}
tft.calibrateTouch(calData, TFT_MAGENTA, TFT_BLACK, 15);
tft.setTextColor(TFT_GREEN, TFT_BLACK);
tft.println("Calibration complete!");
// store data
File f = SPIFFS.open(CALIBRATION_FILE, "w");
if (f) {
f.write((const unsigned char *)calData, 14);
f.close();
}
}
}
//------------------------------------------------------------------------------------------