I am trying to log sensor data to a SD card, however, no matter how i tried, it can only write to the SD card once and never again. Have been debugging for 2 days and no progress ![]()
There is no error message (SD card is successfully opened), and seems no memory leak (FreeRam: 6689 constantly after .txt file on SD card is opened). if i use the "datalog" sample code to write string constant, it works well. i am using MEGA2560.
Is there anything thing wrong with my code?
Comments and suggestions are appreciated!
#include <String.h>
// for LCD
#include <LiquidCrystal.h>
// for DHT sensor and RTC
#include <Wire.h>
#include <Time.h>
#include <DS1307RTC.h>
#include <DHT.h>
// for SD card
#include <SPI.h>
#include <SD.h>
#define DHTPIN 6 // what Arduino pin the DHT signal PIN is connected to
#define DHTTYPE DHT11 // DHT 11
int sensorPin_sound = A0; // select the input pin for the sound meter
int sensorValue_sound = 0; // variable to store the value coming from the sensor
int sensorPin_light = A15; // select the input pin for the light meter
int sensorValue_light = 0; // variable to store the value coming from the sensor
DHT dht(DHTPIN, DHTTYPE);
const int chipSelect = 4; // for SD card
LiquidCrystal lcd(12, 11, 10, 9, 8, 7); // initialize the library with the numbers of the interface pins
volatile int LoggingState = HIGH; // state flag for ISR: LOW/HIGH: Logging disabled/ Logging enabled
const int Log_freq = 5; // frequency devision for display refreshing and logging
volatile int Log_count = 0; // count
File sensorLog;
// --------------- SETUP ------------------------
void setup() {
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print("Temp/Humi Demo");
// set up interrupt from button press
attachInterrupt(0,ISR_Button,RISING); // digital pin 2 // up
// start dht sensor
dht.begin();
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
Serial.print("Initializing SD card...");
// see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
// don't do anything more:
return;
}
Serial.println("card initialized.");
sensorLog = SD.open("datalog.txt", FILE_WRITE);
// if the file is available, write to it:
if (sensorLog) {
sensorLog.println("success opend file");
sensorLog.close();
Serial.println("success opening txt file");
}
// if the file isn't open, pop up an error:
else {
Serial.println("error opening txt file");
lcd.setCursor(0, 0);
lcd.print("SD card read error! ");
}
}
// ----------------- ISR ------------------------
void ISR_Button(){
LoggingState = !LoggingState;
lcd.clear(); //clear current lcd display
lcd.begin(16, 2);
if(LoggingState == HIGH){
lcd.print("Logging Enabled");
}
else{
lcd.print("Logging Disabled");
}
delay(1000);
}
// --------------- LOOP ------------------------
void loop() {
if(LoggingState == HIGH) // Only read and log when in Logging Started State - can be changed using button press
{
// set the cursor to column 0, line 1 (note: line 1 is the second row, since counting begins with 0):
lcd.setCursor(0, 1);
lcd.display();
// display Temperature and Humidity
// float h = dht.readHumidity();
int h = dht.readHumidity();
// Read temperature as Celsius
// float t = dht.readTemperature();
int t = dht.readTemperature();
// display sound and light level
sensorValue_sound = analogRead (sensorPin_sound);
sensorValue_light = analogRead (sensorPin_light);
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t)
) {
Serial.println("Failed to read from DHT sensor!");
return;
}
delay(1000);
/// Logging into SD card
Log_count++;
if(Log_count == Log_freq) // time to log
{
Log_count =0;
sensorLog = SD.open("datalog.txt", FILE_WRITE);
Serial.print(F("FreeRam: "));Serial.println(FreeRam());
// if the file is available, write to it
if (sensorLog) {
sensorLog.println("success open file");
sensorLog.print(t);
sensorLog.print(";");
sensorLog.print(h);
sensorLog.print(";");
sensorLog.print(sensorValue_sound);
sensorLog.print(";");
sensorLog.print(sensorValue_light);
sensorLog.println(";");
sensorLog.close();
Serial.print(t);
Serial.print(";");
Serial.print(h);
Serial.print(";");
Serial.print(sensorValue_sound);
Serial.print(";");
Serial.print(sensorValue_light);
Serial.println(";");
Serial.println("finish write txt file");
}
// if the file isn't open, pop up an error:
else {
Serial.println("error opening txt file");
lcd.setCursor(0, 0);
lcd.print("SD card read error! ");
}
}
} // LoggingState == HIGH
}