Hello, i'm trying to read some data values from a .txt file which is used as a Config file.
I have done some other programs and everything was working fine, but with this i can't figure out what's wrong.
Everything works, but when i add an if and some instructions inside of it arduino can't read or write anything, even the code before that always worked.
Here's my sketch: some words are in italian, i've added some comments next to them.
p.s. i just registered to arduino forum to fix this.
#include <SD.h>
#include <SPI.h>
#include <DHT.h>
#include <Wire.h>
#include <TimeLib.h>
#include <DS1307RTC.h>
File myFile1;
File myFile2;
DHT dht;
String nomefile; //nameFile lol
int TIMING,TEMP_MIN_ALARM,TEMP_MAX_ALARM;
void setup() {
tmElements_t tm;
Serial.begin(9600);
//SPI.begin();
while(!Serial)
{
; //Aspetto le porte native, wait for serial port to connect. Needed for native USB port only
}
Serial.print("Inizializzo la scheda SD..."); //Inizializing sd card
if(!SD.begin(4))
{
Serial.println("Inizializzazione fallita."); //Inizialization failed
return;
}
Serial.println("Inizializzazione completata."); //inizialization completed
myFile2 = SD.open("OPZIONI.txt"); //open the configuration file saved as 'OPZIONI.txt'
if(myFile2)
{
Serial.println("Sto leggendo 'OPZIONI.txt' sulla SD..."); //reading 'OPZIONI.txt' in the SD card...
while(myFile2.available())
{
int TIMING=myFile2.parseInt();
Serial.print("TIMING:"); //variable TIMING contains the milliseconds for the function delay()
Serial.println(TIMING);
int TEMP_MIN_ALARM=myFile2.parseInt(); //if the value is lower then a print alarm is activated
Serial.print("TEMP_MIN_ALARM:");
Serial.println(TEMP_MIN_ALARM);
int TEMP_MAX_ALARM=myFile2.parseInt(); //if the value is exceeded a print alarm is activated
Serial.print("TEMP_MAX_ALARM:");
Serial.println(TEMP_MAX_ALARM);
}
} else {
Serial.println("Errore mentre cercavo di aprire il file."); //error while trying to open the file...
}
myFile2.close();
delay(1000); //multithreading, waiting to see if the problem is the multithreading
if(RTC.read(tm))
{
nomefile += (tmYearToCalendar(tm.Year));
nomefile += tm.Month;
nomefile += tm.Day;
nomefile += ".csv";
}
Serial.print("Il file creato: "); //the created file:
Serial.println(nomefile); //example 201868.csv
myFile1=SD.open(nomefile, FILE_WRITE);
Serial.println(myFile1);
if(myFile1)
{
Serial.println("Sto scrivendo l'intestazione..."); //i'm writing the heading of the file
myFile1.println("datetime;status;humidity;fahrenheit;celsius"); //the heading
} else {
Serial.println("Errore mentre cercavo di scrivere l'intestazione nel file creato."); //error while trying to write
}
myFile1.close();
Serial.println("Stato\tUmidità (%)\tTemperatura (F)\t(C)"); //status humidity temperature
dht.setup(2);
}
void loop() {
float umidita = dht.getHumidity();
float temperatura = dht.getTemperature();
tmElements_t tm;
if(RTC.read(tm))
{
Serial.print(dht.getStatusString());
Serial.print("\t\t");
Serial.print(umidita,1);
Serial.print(" \t\t");
Serial.print(dht.toFahrenheit(temperatura), 1);
Serial.print("\t");
Serial.print(temperatura, 1);
if(temperatura<TEMP_MIN_ALARM)
{
Serial.print("Temperatura sotto il livello minimo."); //temperature below minimum
}
if(dht.toCelsius(temperatura)>TEMP_MAX_ALARM)
{
Serial.print("Temperatura sopra il livello massimo."); //temperature above maximum
}
Serial.println();
}
delay(900);
myFile1=SD.open(nomefile, FILE_WRITE);
Serial.println(myFile1);
if(myFile1==1)
{
Serial.println("Sto scrivendo nel file creato..."); //writing in the created file
myFile1.print(tm.Hour);
myFile1.print(":");
myFile1.print(tm.Minute);
myFile1.print(":");
myFile1.print(tm.Second);
myFile1.print(";");
myFile1.print(dht.getStatusString());
myFile1.print(";");
myFile1.print(umidita);
myFile1.print(";");
myFile1.print(temperatura);
myFile1.print(";");
myFile1.print(dht.toFahrenheit(temperatura));
myFile1.println();
} else {
Serial.println("Non sono riuscito a scrivere i dati."); //i could not write the data
}
myFile1.close();
delay(TIMING);
}