Hello!
I have a simple SD card logging script that reads temperature value from LM-35 sensor and writes it to SD card.
#include <SD.h>
File file;
const int pin = 0;
int tempc = 0;
void setup() {
pinMode(13, OUTPUT);
Serial.begin(9600);
}
void loop() {
file = SD.open("TEST_SD.TXT", FILE_WRITE);
for(int i = 0; i < 8; i++) {tempc += analogRead(pin);}
tempc = (5.0 * tempc * 100.0) / (8.0 * 1024.0);
file.println(tempc,DEC);
file.close();
}
How can i do this without the "FILE CLOSE" in loop?
The problem is that when i add a clock module to my project, it stops logging when the log-file is about 100 kb (although the memory card is 2 Gb).
I think it is because Arduino Duemilanove saves something into its memory (RAM ?) and it just gets full... also it might be because of the clock module - arduino keeps the time values in memory?
The code for SD + CLOCK + TEMP:
#include <SD.h>
#include "Wire.h"
#define DS1307_I2C_ADDRESS 0x68
File file;
const int pin = 0;
int tempc = 0;
byte decToBcd(byte val)
{
return ( (val/10*16) + (val%10) );
}
byte bcdToDec(byte val)
{
return ( (val/16*10) + (val%16) );
}
void setDateDs1307(byte second, // 0-59
byte minute, // 0-59
byte hour, // 1-23
byte dayOfWeek, // 1-7
byte dayOfMonth, // 1-28/29/30/31
byte month, // 1-12
byte year) // 0-99
{
Wire.beginTransmission(DS1307_I2C_ADDRESS);
Wire.send(0);
Wire.send(decToBcd(second));
Wire.send(decToBcd(minute));
Wire.send(decToBcd(hour));
Wire.send(decToBcd(dayOfWeek));
Wire.send(decToBcd(dayOfMonth));
Wire.send(decToBcd(month));
Wire.send(decToBcd(year));
Wire.endTransmission();
}
void getDateDs1307(byte *second,
byte *minute,
byte *hour,
byte *dayOfWeek,
byte *dayOfMonth,
byte *month,
byte *year)
{
Wire.beginTransmission(DS1307_I2C_ADDRESS);
Wire.send(0);
Wire.endTransmission();
Wire.requestFrom(DS1307_I2C_ADDRESS, 7);
*second = bcdToDec(Wire.receive() & 0x7f);
*minute = bcdToDec(Wire.receive());
*hour = bcdToDec(Wire.receive() & 0x3f);
*dayOfWeek = bcdToDec(Wire.receive());
*dayOfMonth = bcdToDec(Wire.receive());
*month = bcdToDec(Wire.receive());
*year = bcdToDec(Wire.receive());
}
void setup() {
pinMode(13, OUTPUT);
byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
Wire.begin();
Serial.begin(9600);
second = 00;
minute = 39;
hour = 00;
dayOfWeek = 6;
dayOfMonth = 19;
month = 11;
year = 11;
if (!SD.begin()) {
Serial.println("begin failed");
return;
}}
void loop() {
byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
getDateDs1307(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year);
file = SD.open("TEST_SD.TXT", FILE_WRITE);
for(int i = 0; i < 8; i++) {tempc += analogRead(pin);}
tempc = (5.0 * tempc * 100.0) / (8.0 * 1024.0);
file.print(tempc,DEC);
file.print(" deg C ");
file.print(hour, DEC);
file.print(":");
file.print(minute, DEC);
file.print(":");
file.print(second, DEC);
file.print(" ");
file.print(dayOfMonth, DEC);
file.print("/");
file.print(month, DEC);
file.print("/");
file.print(year, DEC);
file.print(" Day_of_week:");
file.println(dayOfWeek, DEC);
file.close();
}