Hi everyone,
I am working on a project to read T and RH and send the data on an SD card. The values have been also read on an LCD screen.
After the 2nd cycle of the loop, I have the message on the LCD "file not open". So the Arduino creates the reading.csv file but the file is actually empty on the SD card.
How is it possible that on the 1st cycle of the main loop the file is opening and then on the 2nd cycle is not open anymore?
Here is my code:
// Libraries
#include <dht.h> // Includes the libraries for the sensor
#include <LiquidCrystal.h> // Includes the libraries for the LCD
#include <DS3231.h>; // Includes the libraries for the clock
#include <SPI.h>; // Includes the libraries for the Serial Peripheral Interface that allows the Master (Arduino) to control a slave device
#include <SD.h>; // Includes the libraries for the SD card
// Interface and Objects definitions
DS3231 rtc(SDA, SCL); // Initialises the DS3231 using the hardware interface
#define dataPin 2 // Defines pin number to which the sensor is connected (avoid D4-9 and A0 used by LCD)
dht DHT; // Creates a DHT object
LiquidCrystal lcd(8,9,4,5,6,7); // Creates a LCD LiquidCrystal object
const int chipSelect = 10; // Assigns the pin for the SPI-SD chip selection
void setup()
{
pinMode(chipSelect, OUTPUT); // Ensures that the SPI-SD selection pin is an output
rtc.begin(); // Initialises the rtc object
// rtc.setDOW(TUESDAY); // Sets Day-of-Week to TUESDAY
rtc.setTime(14, 54, 0); // Sets the time to 12:00:00 (24hr format)
rtc.setDate(6, 1, 2017); // Sets the date to January 1st, 2017
lcd.begin(16, 2); // Initialises the interface to the LCD screen, and specifies the 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 to the LCD
delay (4000); // Waits 4 seconds
}
void loop()
{
// Open the file on the SD card
delay(2000);
File readings = SD.open("readings.csv", FILE_WRITE);
// Reading the data
int readData = DHT.read22(dataPin); // Reads the data from the sensor
float t = DHT.temperature; // Gets the values of the temperature
float h = DHT.humidity; // Gets the values of the humidity
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t))
{
lcd.println("DHT FAIL!");
return;
}
// If the file opened OK, write to it on SD card
lcd.clear();
if (readings)
{
lcd.print("file OPEN");
delay(1000);
readings.print(t);
readings.print(",");
readings.print(h);
// close the file:
readings.close();
lcd.clear();
lcd.println("file CLOSE");
delay(1000);
}
else
{
// if the file didn't open, print an error:
lcd.println("file not open");
delay(4000);
}
// Printing the results on the LCD screen with time and date
delay(2000);
lcd.clear(); // Clears the LCD screen and positions the cursor in the upper-left corner
lcd.print(rtc.getDateStr()); // Prints date
lcd.setCursor(8,1); // Moves the cursor on the second line of the LCD
lcd.println(rtc.getTimeStr()); // Prints time
delay (4000); // Waits 4 seconds
lcd.clear(); // Clears the LCD screen and positions the cursor in the upper-left corner
char tempF[6]; // Defines a character variable
char humF[6];
dtostrf(t, 5, 1, tempF); // Converts a float to a char array so it can then be printed easily
dtostrf(h, 2, 0, humF);
lcd.print("T:"); lcd.print(tempF); lcd.print((char)223); lcd.print("C ");
// NOTE: char(223) identifies the degree symbol
lcd.print("H: "); lcd.print(humF); lcd.print("%");
delay(2000); // Delays 2 seconds, as the DHT22 minimum sampling rate is 0.5Hz
}
Of course, this post will also help me to discuss and improve my coding skill, considering that I am a completely new to the Arduino.
This is my very first project and I would like to thanks all the people that wrote articles and posts from where I took "bits" of my complete code
Thanks a lot for everyone's support.
Mario