Created
February 4, 2026 00:31
-
-
Save yeffrimic/aa2bdb72626f28044456b9d2f613e185 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <Wire.h> | |
| #include <RTClib.h> | |
| #include <SD.h> | |
| #include <SPI.h> | |
| #define SD_CS_PIN 10 | |
| #define BUTTON_PIN 2 | |
| RTC_DS3231 rtc; | |
| File file; | |
| bool lastButtonState = HIGH; | |
| unsigned long pressStartTime = 0; | |
| uint16_t dailyCount = 0; | |
| uint8_t lastDay = 0; | |
| const char filename[] = "eventos.csv"; | |
| // ===================== | |
| // Guardar evento en SD | |
| // ===================== | |
| void saveEvent(DateTime now, unsigned long durationMs) { | |
| Serial.println(F("[SD] Abriendo archivo para escritura")); | |
| file = SD.open(filename, FILE_WRITE); | |
| if (!file) { | |
| Serial.println(F("[ERROR] No se pudo abrir el archivo")); | |
| return; | |
| } | |
| Serial.println(F("[SD] Escribiendo evento")); | |
| file.print(now.year()); file.print("-"); | |
| if (now.month() < 10) file.print("0"); | |
| file.print(now.month()); file.print("-"); | |
| if (now.day() < 10) file.print("0"); | |
| file.print(now.day()); file.print(","); | |
| if (now.hour() < 10) file.print("0"); | |
| file.print(now.hour()); file.print(":"); | |
| if (now.minute() < 10) file.print("0"); | |
| file.print(now.minute()); file.print(":"); | |
| if (now.second() < 10) file.print("0"); | |
| file.print(now.second()); file.print(","); | |
| file.print("PRESIONADO,"); | |
| file.print(durationMs); | |
| file.print(" ms,conteo_dia="); | |
| file.println(dailyCount); | |
| file.close(); | |
| Serial.println(F("[SD] Evento guardado correctamente")); | |
| } | |
| // =========================== | |
| // Cargar último estado guardado | |
| // =========================== | |
| void loadLastState() { | |
| Serial.println(F("[SD] Cargando último estado")); | |
| file = SD.open(filename, FILE_READ); | |
| if (!file) { | |
| Serial.println(F("[SD] Archivo no existe, se creará")); | |
| return; | |
| } | |
| String line; | |
| while (file.available()) { | |
| line = file.readStringUntil('\n'); | |
| int idx = line.indexOf("conteo_dia="); | |
| if (idx != -1) { | |
| dailyCount = line.substring(idx + 11).toInt(); | |
| lastDay = line.substring(8, 10).toInt(); // DD | |
| } | |
| } | |
| file.close(); | |
| Serial.print(F("[SD] Último conteo diario: ")); | |
| Serial.println(dailyCount); | |
| Serial.print(F("[SD] Último día registrado: ")); | |
| Serial.println(lastDay); | |
| } | |
| void setup() { | |
| Serial.begin(9600); | |
| Serial.println(F("[INIT] Iniciando sistema")); | |
| pinMode(BUTTON_PIN, INPUT_PULLUP); | |
| // RTC DS3231 | |
| Serial.println(F("[RTC] Inicializando DS3231")); | |
| Wire.begin(); | |
| if (!rtc.begin()) { | |
| Serial.println(F("[ERROR] DS3231 no detectado")); | |
| while (1); | |
| } | |
| if (rtc.lostPower()) { | |
| Serial.println(F("[RTC] Pérdida de energía detectada, ajustando hora")); | |
| rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); | |
| } | |
| // SD | |
| Serial.println(F("[SD] Inicializando SD")); | |
| if (!SD.begin(SD_CS_PIN)) { | |
| Serial.println(F("[ERROR] Fallo al inicializar SD")); | |
| while (1); | |
| } | |
| Serial.println(F("[SD] SD inicializada")); | |
| loadLastState(); | |
| DateTime now = rtc.now(); | |
| if (now.day() != lastDay) { | |
| Serial.println(F("[INFO] Día nuevo detectado, reiniciando contador")); | |
| dailyCount = 0; | |
| lastDay = now.day(); | |
| } | |
| Serial.println(F("[INIT] Sistema listo")); | |
| } | |
| void loop() { | |
| bool buttonState = digitalRead(BUTTON_PIN); | |
| DateTime now = rtc.now(); | |
| // Cambio de día | |
| if (now.day() != lastDay) { | |
| Serial.println(F("[INFO] Cambio de día detectado")); | |
| dailyCount = 0; | |
| lastDay = now.day(); | |
| } | |
| // Botón presionado | |
| if (lastButtonState == HIGH && buttonState == LOW) { | |
| pressStartTime = millis(); | |
| Serial.println(F("[BOTON] Presionado")); | |
| } | |
| // Botón liberado | |
| if (lastButtonState == LOW && buttonState == HIGH) { | |
| unsigned long duration = millis() - pressStartTime; | |
| dailyCount++; | |
| Serial.print(F("[BOTON] Liberado | Duración: ")); | |
| Serial.print(duration); | |
| Serial.print(F(" ms | Conteo diario: ")); | |
| Serial.println(dailyCount); | |
| saveEvent(now, duration); | |
| } | |
| lastButtonState = buttonState; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment