I have a Uno R3 with an Adafruit data logger shield running an on/off time equipment monitor. The program (using SD.h) works well, but requires a restart when the card is removed to be read in a computer and then reinstalled in the shield. I would like to remove,read and replace the card when my equipment is in a predictable state of not logging any change.
I followed this previous thread
(Writing to SD Card after removing and replacing it - Storage - Arduino Forum)
and believed that I could do what I wanted using SdFat.h. I can run the SdFat example "bench" on my hardware, and indeed can remove and replace the SD card with the new test appended to the end of the old after replacing O_TRUNC with O_AT_END in the open/write command in the sketch.
I successfully ported my existing code to SdFat from SD but have not been successful in getting the restart behavior of the bench test with SdFat.
I have written a simple test program to log some intervals, and if I can get this working, I should be OK with my actual program. I would appreciate some guidance as to how to get this sketch to append new data to the end of the file after a removal, read, and replace.
#include <SdFat.h>;
SdFat sd;
SdFile myFile;
unsigned long start_time;
unsigned long current_time;
unsigned long delta_time;
int delta_hours;
int delta_minutes;
int delta_seconds;
//set pin for data logger and UnoR3
const int chipSelect = 10;
// set pin number for contact input
const int contactPin = 2;// the number of the switched pin
// Initialize variables for contacts open, inverted logic from PULLUP
int reading = HIGH;
int contactState= HIGH;
int lastContactState = HIGH;
//debounce variables
unsigned long lastDebounceTime = 0;
long debounceDelay = 200;
void setup () {
pinMode(contactPin, INPUT_PULLUP);
pinMode(chipSelect, OUTPUT);
sd.begin(chipSelect);
start_time=millis()/1000;
//Setup data for first line of SD card file
myFile.open("ST_log.csv",O_RDWR|O_CREAT|O_AT_END);
myFile.println();//line break on setup
myFile.println("IS , WAS , DELTA_HR , DELTA_MIN , DELTA_SEC");
//will read OPEN OPEN and 0:0:0 elapsed time on setup
write_card(); //call function to write SD card
// close the file:
myFile.close();
}
void loop ()
{
current_time= millis()/1000;
delta_time=current_time - start_time;
delta_hours=delta_time/3600;
delta_minutes=(delta_time % 3600)/60;
delta_seconds=delta_time % 60;
// check contact state and debounce interval
reading = digitalRead(contactPin);
// If the state has changed
if (reading != lastContactState) {
// reset the debounce timer
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay ) {
// if longer than the debounce delay
// if the reading has changed
if (reading != contactState) {
//open the file
myFile.open("ST_log.csv",O_RDWR|O_CREAT|O_AT_END);
write_card();
// close the file:
myFile.close();
contactState = reading;//assign changed reading to contactState
//reset the elapsed time for new state
start_time=current_time;
}
}
lastContactState = reading;
}
void write_card (){
//contact state and transitions
if (reading==1) {
myFile.print("OPEN");
myFile.print(" , ");
}
if (reading==0){
myFile.print("CLOSED");
myFile.print(" , ");
}
if (contactState==1){
myFile.print("OPEN");
myFile.print(" , ");
}
if (contactState==0){
myFile.print("CLOSED");
myFile.print(" , ");
}
myFile.print(delta_hours);
myFile.print(" , ");
myFile.print(delta_minutes);
myFile.print(" , ");
myFile.println(delta_seconds);
}
Thank you