he tenido problemas con este código, ya logre que todo se inicialice correctamente, sin embargo al llegar a la línea 96 "cretaeNewFile()" el programa se queda colgado, no hace nada
más, y ya revise varias cosas pero sigo sin encontrar la solución, no se si es por la forma en que tengo redactada la creación del archivo. Este es el código en cuestión.
#include <SPI.h>
#include <SD.h>
#include <Wire.h>
#include <RTClib.h>
#include <DHT.h>
#include <MAX6675.h>
#include <LiquidCrystal_I2C.h>
// Pines para termopares MAX6675
const int thermoDO = 6;
const int thermoCS1 = 3;
const int thermoCS2 = 4;
const int thermoCS3 = 7;
const int thermoCS4 = 8;
const int thermoCLK = 5;
MAX6675 thermocouple1(thermoCS1, thermoDO, thermoCLK);
MAX6675 thermocouple2(thermoCS2, thermoDO, thermoCLK);
MAX6675 thermocouple3(thermoCS3, thermoDO, thermoCLK);
MAX6675 thermocouple4(thermoCS4, thermoDO, thermoCLK);
#define DHTPIN 2
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
RTC_DS3231 rtc;
LiquidCrystal_I2C lcd(0x27, 16, 2);
const int chipSelect = 10;
String currentDataFile = "";
unsigned long lastMeasurement = 0;
const unsigned long measurementInterval = 30000; // 30s
void setup() {
Serial.begin(9600);
Serial.println("Arrancando...");
SPI.begin(); // Primero SPI
Wire.begin(); // Luego I2C
delay(100); // Pequeña pausa para estabilizar
lcd.init();
lcd.backlight();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Iniciando...");
Serial.println("LCD inicializada.");
delay(10);
pinMode(chipSelect, OUTPUT);
pinMode(thermoCS1, OUTPUT);
pinMode(thermoCS2, OUTPUT);
pinMode(thermoCS3, OUTPUT);
pinMode(thermoCS4, OUTPUT);
digitalWrite(thermoCS1, HIGH);
digitalWrite(thermoCS2, HIGH);
digitalWrite(thermoCS3, HIGH);
digitalWrite(thermoCS4, HIGH);
if (!initializeSDCard()) {
lcdDisplayError("Error en tarjeta SD");
}
delay(300);
bool error = false;
if (!rtc.begin()) {
Serial.println("ERROR: No se detectó el RTC.");
lcdDisplayError("Error RTC");
error = true;
} else {
Serial.println("RTC inicializado");
}
delay(300);
dht.begin();
Serial.println("Sensor DHT inicializado.");
delay(300);
float t1 = thermocouple1.getCelsius();
float t2 = thermocouple2.getCelsius();
float t3 = thermocouple3.getCelsius();
float t4 = thermocouple4.getCelsius();
if (isnan(t1) || isnan(t2) || isnan(t3) || isnan(t4)) {
Serial.println("ERROR: Uno o más termopares no responden.");
error = true;
} else {
Serial.println("Termopares funcionando.");
}
delay(300);
createNewDataFile();
//delay(300);
// Serial.println("el achivo fue creado");
}
bool initializeSDCard() {
lcd.clear();
lcd.print("Iniciando SD...");
Serial.println("Inicializando tarjeta SD... ");
if (!SD.begin(chipSelect)) {
Serial.println("Fallo inicialización SD");
return false;
}
delay(500);
lcd.clear();
lcd.print("SD inicializada correctamente");
digitalWrite(chipSelect, HIGH);
return true;
}
void createNewDataFile() {
digitalWrite(chipSelect, LOW);
//Serial.print("archivo creado");
// delay(20);
DateTime now = rtc.now();
currentDataFile = String(now.year()) + "_" +
String(now.month()) + "_" +
String(now.day()) + ".csv";
if (!SD.exists(currentDataFile)) {
File file = SD.open(currentDataFile, FILE_WRITE);
if (file) {
file.println("Fecha,Hora,Temp1,Temp2,Temp3,Temp4,Humedad");
file.close();
lcd.clear();
lcd.print("Archivo creado:");
lcd.setCursor(0, 1);
lcd.print(currentDataFile);
delay(2000);
}
}
}
void readTemperatures(float &temp1, float &temp2, float &temp3, float &temp4) {
digitalWrite(thermoCS1, LOW);
temp1 = thermocouple1.getCelsius();
digitalWrite(thermoCS1, HIGH);
delay(10);
digitalWrite(thermoCS2, LOW);
temp2 = thermocouple2.getCelsius();
digitalWrite(thermoCS2, HIGH);
delay(10);
digitalWrite(thermoCS3, LOW);
temp3 = thermocouple3.getCelsius();
digitalWrite(thermoCS3, HIGH);
delay(10);
digitalWrite(thermoCS4, LOW);
temp4 = thermocouple4.getCelsius();
digitalWrite(thermoCS4, HIGH);
}
void takeMeasurements() {
DateTime now = rtc.now();
float temp1, temp2, temp3, temp4;
readTemperatures(temp1, temp2, temp3, temp4);
float humidity = dht.readHumidity();
saveToSD(now, temp1, temp2, temp3, temp4, humidity);
displayMeasurements(now, temp1, temp2, temp3, temp4, humidity);
}
void saveToSD(DateTime now, float temp1, float temp2, float temp3, float temp4, float humidity) {
File file = SD.open(currentDataFile, FILE_WRITE);
if (file) {
file.print(now.year()); file.print("/");
file.print(now.month()); file.print("/");
file.print(now.day()); file.print(",");
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(temp1); file.print(",");
file.print(temp2); file.print(",");
file.print(temp3); file.print(",");
file.print(temp4); file.print(",");
file.println(humidity);
file.close();
} else {
lcdDisplayError("Error escribiendo SD");
}
}
void displayMeasurements(DateTime now, float temp1, float temp2, float temp3, float temp4, float humidity) {
static uint8_t displayState = 0;
lcd.clear();
switch (displayState) {
case 0:
lcd.setCursor(0, 0);
lcd.print("T1:"); lcd.print(temp1, 1); lcd.print("C");
lcd.setCursor(8, 0);
lcd.print("T2:"); lcd.print(temp2, 1); lcd.print("C");
lcd.setCursor(0, 1);
lcd.print("H:"); lcd.print(humidity, 0); lcd.print("%");
break;
case 1:
lcd.setCursor(0, 0);
lcd.print("T3:"); lcd.print(temp3, 1); lcd.print("C");
lcd.setCursor(8, 0);
lcd.print("T4:"); lcd.print(temp4, 1); lcd.print("C");
break;
case 2:
lcd.setCursor(0, 0);
lcd.print(now.day()); lcd.print("/");
lcd.print(now.month()); lcd.print("/");
lcd.print(now.year());
lcd.setCursor(0, 1);
lcd.print(now.hour()); lcd.print(":");
if (now.minute() < 10) lcd.print("0");
lcd.print(now.minute()); lcd.print(":");
if (now.second() < 10) lcd.print("0");
lcd.print(now.second());
break;
}
displayState = (displayState + 1) % 3;
}
void lcdDisplayError(const char* error) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("ERROR:");
lcd.setCursor(0, 1);
lcd.print(error);
Serial.print("ERROR: ");
Serial.println(error);
}
void loop() {
if (millis() - lastMeasurement >= measurementInterval) {
lastMeasurement = millis();
takeMeasurements();
DateTime now = rtc.now();
String newFileName = String(now.year()) + "_" +
String(now.month()) + "_" +
String(now.day()) + ".csv";
if (currentDataFile != newFileName) {
createNewDataFile();
}
}
delay(100);
}