Go Down

Topic: Read variables from SD card then print in SD other data (Read 219 times) previous topic - next topic

francesco01

Jun 08, 2018, 04:21 pm Last Edit: Jun 08, 2018, 05:57 pm by francesco01
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.
Code: [Select]

#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);
}

MorganS

Quote
Code: [Select]
delay(1000);    //multithreading, waiting to see if the problem is the multithreading
Which Arduino are you using that has multithreading? Note that nearly all Arduino libraries are not written for multithreading so they will fail. Specifically, the SD library cannot have multiple files open in different threads.

Show us an example of the file that fails parsing.
"The problem is in the code you didn't post."

francesco01

Thanks for your reply @MorganS :) , i'm using Arduino uno.
My teacher said me to try to put a delay to see if it was a multithreading problem.

The file is a simple .txt file which has in it:

10000
18
30


Sorry for the late response.

Delta_G

Quote
My teacher said me to try to put a delay to see if it was a multithreading problem.
On an UNO?  You need a new teacher. 
|| | ||| | || | ||  ~Woodstock

Please do not PM with technical questions or comments.  Keep Arduino stuff out on the boards where it belongs.

francesco01

#4
Jun 11, 2018, 05:57 pm Last Edit: Jun 11, 2018, 05:58 pm by francesco01
My teacher isn't an Arduino teacher lol :) , he's a programmer, i asked him for some advice.

francesco01

#5
Jun 12, 2018, 10:29 am Last Edit: Jun 12, 2018, 11:02 am by francesco01 Reason: Wrong word
I tried to remove this part of the code in void setup() :

Code: [Select]
 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();


I don't know why but if i remove this part everything works fine, but i'd like to have the heading.

Go Up