Hello everyone
I’m on this problem now for 2 weeks and since not only the file opening doesn’t work but also now the program crashes, when it tries to log the data, I finally ask you for help!
My project is to construct a temperature regulator for a box to transport bees at a specific temperature setpoint(35°C possibly). I want to log the measured temperature and humidity to a file in the SD-Card.
I use an Arduino UNO, a Relay Shield v2.0,
a Memoire Shield Data Logger(http://snootlab.com/shields-snootlab/86-memoire-fr.html),
an RGB LCD Shield (Overview | RGB LCD Shield | Adafruit Learning System)
and a digital Temperature/humidity sensor DHT44.
For the data logger code I used the example on Snootlab and adapted it to my sensor:
http://forum.snootlab.com/viewtopic.php?f=30&t=72
In fact, all my code works perfectly (I am very happy since it was a fight
), a Menu is displayed on the LCD and the time and temperature set point can be changed.
In the setup() the SD-Card is initialized and then the file is opened and a first datastring is written in the file(“Capture and timestamp of values from the sensor, according RTC”).
Everything works until it tries to open the file again in the DataLogger() function which is called in the loop(). There it doesn’t manage to open it. Serial monitor: “error opening the file during configuration”
Then the whole program crashes and the execution stops so that also the LCD Display freezes and the Serial monitor stops his outputs.
The magical thing is that with the example code from snootlab the Data Logger works :~ I have checked so many times to find an important difference to my code...
My code is divided in several tabs for the different functions. The first code block is the “main” with the setup and void and all the includes etc.
Then I have other 5 tabs (I’m really sorry for this bomb of code!! If you want me to take away parts, just inform me. The 1st, 2nd, 4th and 5th tabs I’m not posting since it’s a lot of code. But I will put the entire codes attached.):
1st tab to manage the button controls and navigating the menu.
2nd tab to calculate the temperature&humidity from the DHT44 and storing in the variables TempDHTInt/TempDHTDec(part before and after the comma) and HumidDHT.
3rd tab probably the most interesting function for you DataLogger(). It should do the same thing as the loop() function in the example from snootlab.
4th tab is the function which displays everything important as the menu and the temp/humidity on the LCD.
5th tab and last one
should regulate the relays which later on control the peltier element and ventilator.
// include the library codes:
#include <DHT.h> // DHT library for DHT44
#include <Wire.h> // Libraries for RGB-LCD Shield
#include <Adafruit_MCP23017.h> // "
#include <Adafruit_RGBLCDShield.h> // "
#include <SD.h> // SD library for data logger
#include <RTClib.h> // RealTimeClock library for data logger
// Variable and pin definitions DHT
#define DHT44_PIN 3 // DHT44 Temperature & Humidity Sensor (RHT05)
int TempDHTInt; // Variable to store the values before the comma of the temperature measured by DHT44
int TempDHTDec; // Variable to store the decimal values after the comma of the temperature measured by DHT44
int HumidDHT; // Variable to store the humidity measured by DHT44
// Variable and pin definitions RGB LCD Shield
Adafruit_RGBLCDShield lcd = Adafruit_RGBLCDShield();
#define YELLOW 0x3
// Variable and pin definitions Regulation
int TempSPInt = 35;
int TempSPDec = 0;
int hysteresis = 10 * (1); // Replace the value in brackets with the desired Hysteresis(in °C).
const int ventilator = 7; // Relay 1 that controls the ventilator is on pin 7
const int peltier1 = 5; // Relay 3 that controls the Peltier is on pin 5
const int peltier2 = 4; // Relay 4 that controls the Peltier is on pin 4
// Variable and pin definitions DataLogger Mémoire Shield
const char pin_chipSelect = 10; // pin 10 is used by the shield mémoire for the ChipSelect signal
RTC_DS1307 RTC;
File loggerFile;
String datastring;
int value[14] = {0, 0, TempSPInt, TempSPDec, 0, 0, 0, 0, 99, 99, 99, 99, 9999, 0};
int maxvalue[14] = {0, 0, 50, 9, 0, 0, 0, 0, 23, 59, 31, 12, 9999, 0};
int minvalue[14] = {0, 0, 20, 0, 0, 0, 0, 0, 0, 0, 1, 1, 2000, 0};
int screen = 1;
int oldScreen = 0;
int dummy = 0;
bool state = false;
bool prevButtons = false;
unsigned long timer;
void setup()
{
Serial.begin(9600); // start serial communication at 9600 bits of data per second
// Set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// set backlight color
lcd.setBacklight(YELLOW);
// Regulation pins
pinMode(ventilator, OUTPUT);
pinMode(peltier1, OUTPUT);
pinMode(peltier2, OUTPUT);
// DataLogger
Serial.print("Initialisation of SD-Card...");
pinMode(pin_chipSelect, OUTPUT); // set pin to output
Wire.begin();
RTC.begin();
if(! RTC.isrunning()){ // if RTC is not configured, compile clock time with compuer values
RTC.adjust(DateTime(__DATE__, __TIME__));
}
if (!SD.begin(pin_chipSelect)){ // if the SD-Card is not present...
Serial.println("SD-Card false or not available");
lcd.print("SD-Card false or");
lcd.setCursor(0, 1);
lcd.print("not available");
delay(3000);
return; // exit the execution
}
Serial.println("SD-Card OK.");
datastring = "\nCapture and timestamp of values from the sensor, according to RTC\n---------\n ";
loggerFile = SD.open("log1.txt", FILE_WRITE); // Open the file in writing mode
if (loggerFile){ // If the file already exists, on écrira à la suite des données présentes.
loggerFile.println(datastring);
loggerFile.close();
Serial.println(datastring); // dupliquer la donnée sur port série
}
else{ // if fail to open, display the error
Serial.println("error dopening the file during configuration");
lcd.print("Failed to open");
lcd.setCursor(0, 1);
lcd.print("the file(config)");
delay(3000);
}
timer = millis();
}
void loop()
{
RGBLCDShield();
ButtonControls();
if(millis() - timer >= 1000UL){ // before "UL" tipe in the desired frequence of refreshment for the sensor, in milliseconds
timer = millis();
DHT44();
if(state){ // if the regulation is ON
regulate();
DataLogger();
}
}
}
tab with the DataLogger()
/*
***SD-Card Data Logger***
This function controls the data logging to a SD-Card.
It writes the temperature and humidity measured by DHT44 and the temperature measured by DS18B20.
Additionally it also writes down the real time when logging the data.
Hardware:
Power connected to +5V
Gnd connected to ground
A5: SCL I2C clock (for RTC with DS1307 chip)
A4: SDA I2C data (for RTC with DS1307 chip)
Pin 9: user available Indicator Led
Pin 10: SS (SPI Bus - Slave Select)
Pin 11: MOSI (SPI Bus - Master Output, Slave Input)
Pin 12: MISO (SPI Bus - Master Input, Slave Output)
Pin 13:SCK (SPI Bus - Clock)
(Just put the shield on the top of the relay shield, so that it fits)
*/
void DataLogger()
{
DateTime moment = RTC.now(); //Init de l'objet temporel
datastring=String(moment.day(),DEC);
datastring+='/';
datastring+=String(moment.month(),DEC);
datastring+='/';
datastring+=String(moment.year(),DEC);
datastring+=' ';
datastring+=String(moment.hour(),DEC);
datastring+=':';
datastring+=String(moment.minute(),DEC);
datastring+=':';
datastring+=String(moment.second(),DEC);
datastring += ("\t Temperature DHT44 = "); // préalablement remplie avec les données temporelles
datastring += TempDHTInt;
datastring += (".");
datastring += TempDHTDec;
datastring += ("C\t Humidity DHT44 = "); // préalablement remplie avec les données temporelles
datastring += HumidDHT;
datastring += ("%");
loggerFile = SD.open("log1.txt", FILE_WRITE);
if (loggerFile){
loggerFile.println(datastring);
loggerFile.close();
Serial.println(datastring); // dupliquer la donnée sur port série
}
else{
Serial.println("erreur d'ouverture du fichier");
}
datastring =0;
}
I'm using the Arduino IDE 1.0.5 on Windows 7.
I hope someone can give me a hint where I made the mistakes!
Thank you very much!
Iris
Additional Libraries: DHT, Adafruit_RGBLCDShield, RTC
BienenTempRegulatorALP_neu.zip (6.31 KB)


