Arduino is not opening the file on SD card (from 2nd loop cycle)


hi everyone,

i working on project read t , rh , send data on sd card. values have been read on lcd screen.
after 2nd cycle of loop, have message on lcd "file not open". arduino creates reading.csv file file empty on sd card.

how possible on 1st cycle of main loop file opening , on 2nd cycle not open anymore?

here code:

code: [select]

// libraries
  #include <dht.h> // includes libraries sensor
  #include <liquidcrystal.h> // includes libraries lcd
  #include <ds3231.h>; // includes libraries clock
  #include <spi.h>; // includes libraries serial peripheral interface allows master (arduino) control slave device
  #include <sd.h>; // includes libraries sd card
   
// interface , objects definitions
  ds3231 rtc(sda, scl); // initialises ds3231 using hardware interface
  #define datapin 2 // defines pin number sensor connected (avoid d4-9 , a0 used lcd)
  dht dht; // creates dht object
  liquidcrystal lcd(8,9,4,5,6,7); // creates lcd liquidcrystal object
  const int chipselect = 10; // assigns pin spi-sd chip selection
 
void setup()
{
  pinmode(chipselect, output); // ensures spi-sd selection pin output
  rtc.begin(); // initialises rtc object
  // rtc.setdow(tuesday); // sets day-of-week tuesday
  rtc.settime(14, 54, 0); // sets time 12:00:00 (24hr format)
  rtc.setdate(6, 1, 2017); // sets date january 1st, 2017
  lcd.begin(16, 2); // initialises interface lcd screen, , specifies dimensions
  lcd.print("initialising sd");
  delay (2000);
  lcd.clear();
    if (!sd.begin(chipselect))
    {
      lcd.println("sd failed!");
      return;
    }
    lcd.println("sd ok");
    delay(2000);
    lcd.clear();
  lcd.print("reading sensor"); // prints lcd
  delay (4000); // waits 4 seconds
}

void loop()
{
// open file on sd card
  delay(2000);
  file readings = sd.open("readings.csv", file_write);

// reading data
  int readdata = dht.read22(datapin); // reads data sensor
  float t = dht.temperature; // gets values of temperature
  float h = dht.humidity; // gets values of humidity

// check if reads failed , exit (to try again).
    if (isnan(h) || isnan(t))
    {
      lcd.println("dht fail!");
      return;
    }

// if file opened ok, write on sd card
lcd.clear();
if (readings)
  {
    lcd.print("file open");
    delay(1000);
    readings.print(t);
    readings.print(",");
    readings.print(h);
    // close file:
    readings.close();
    lcd.clear();
    lcd.println("file close");
    delay(1000);
  }
  else
  {
    // if file didn't open, print error:
    lcd.println("file not open");
    delay(4000);
  }

// printing results on lcd screen time , date
  delay(2000);
  lcd.clear(); // clears lcd screen , positions cursor in upper-left corner
  lcd.print(rtc.getdatestr()); // prints date
  lcd.setcursor(8,1); // moves cursor on second line of lcd
  lcd.println(rtc.gettimestr()); // prints time
  delay (4000);  // waits 4 seconds
  lcd.clear(); // clears lcd screen , positions cursor in upper-left corner
  char tempf[6]; // defines character variable
  char humf[6];
  dtostrf(t, 5, 1, tempf); // converts float char array can printed easily
  dtostrf(h, 2, 0, humf);
  lcd.print("t:"); lcd.print(tempf); lcd.print((char)223); lcd.print("c ");
  // note: char(223) identifies degree symbol
  lcd.print("h: "); lcd.print(humf); lcd.print("%");

delay(2000); // delays 2 seconds, dht22 minimum sampling rate 0.5hz
 
}


of course, post me discuss , improve coding skill, considering new arduino.
this first project , people wrote articles , posts took "bits" of complete code

thanks lot everyone's support.

mario

quote
the values have been read on lcd screen.
how read values lcd screen?

code: [select]
  dtostrf(t, 5, 1, tempf); // converts float char array can printed easily
  dtostrf(h, 2, 0, humf);
  lcd.print("t:"); lcd.print(tempf); lcd.print((char)223); lcd.print("c ");
  // note: char(223) identifies degree symbol
  lcd.print("h: "); lcd.print(humf); lcd.print("%");

why?

from liquidcrystal.h:
code: [select]
class liquidcrystal : public print {
the print class knows how print float.

if want humidity int, why storing value in float?

what kind of sd reader/writer have? how connected arduino?

if disconnect lcd, still have problems writing sd card?


Arduino Forum > Using Arduino > Programming Questions > Arduino is not opening the file on SD card (from 2nd loop cycle)


arduino

Comments

Popular posts from this blog

DHT11 Time out error using v0.4.1library

Sketch upload fails with Java error (___REMOVE___/bin/avrdude)!

Arduino Uno + KTY81/210 temperature sensor