Ran the following code over the weekend that captures temp, optical density and pH of a cellular culture. (As a side note, I’ve ordered a RTC to alleviate the labor intensive runtime “timestamp”.) My issue is when I check the memory card this morning I have multiple new files with names such as " ╩調`pⁿb " in addition to the destination file. The destination file data starts 29hrs into the runtime.
Is the data prior to 29hrs lost or any chance it is recoverable?
Additionally, is this a fluke? I assume it isn’t but what is the cause of it/what should I look to modify to avoid this happening again?
#include <SPI.h>
#include <SD.h>
File myFile;
#define TEMP_INTERVAL 360000ul // 6min
#define OD_INTERVAL 360000ul // 6min
#define PH_INTERVAL 360000ul // 6min
#define PH_CO2_ADD_INTERVAL 60000ul // 1min
#define PH_CO2_CHECK_INTERVAL 600000ul // 10min
// * * * * pH ADDED * * * *
unsigned long int avgValue; //Store the average value of the sensor feedback
float b;
int buf[10], temp;
// Optical Density Sensor
const byte OD_Probe = A5; //
const byte Temp_Probe = A0; //
const byte pH_Probe = A4; //
const byte CO2_GateValve = 3;
const byte pH_HIGH_THRESHOLD = 16.00;
unsigned long timeOD;
unsigned long timepH;
unsigned long timeTemp;
unsigned long timeCo2Add;
unsigned long timeCo2Chk;
unsigned long timeNow;
enum { ST_INIT, ST_ADD, ST_CHK };
int state;
// -----------------------------------------------------------------------------
void setup () {
Serial.begin (9600);
Serial.print("Initializing SD card...");
pinMode(10, OUTPUT);
if (!SD.begin(4)) {
Serial.println("Initialization failed!");
return;
}
Serial.println("Initialization done.");
myFile = SD.open("08122020.txt", FILE_WRITE);
if (myFile) {
Serial.print("Writing to 08122020.txt...");
// myFile.println("testing 1, 2, 3.");
myFile.close();
Serial.println("done.");
} else {
Serial.println("error opening 08122020.txt");
}
myFile = SD.open("08122020.txt");
if (myFile) {
Serial.println("08122020.txt");
while (myFile.available()) {
Serial.write(myFile.read());
}
myFile.close();
} else {
Serial.println("error opening 08122020.txt");
}
pinMode (OD_Probe, INPUT);
pinMode (CO2_GateValve, OUTPUT);
pinMode (Temp_Probe, INPUT);
pinMode (pH_Probe, INPUT);
timeNow = millis ();
timeOD = timeNow;
timepH = timeNow;
timeTemp = timeNow;
}
void loop ()
{
timeNow = millis ();
// -----------------------------------------------------------------------------
// * * * * * Temp INTERVAL * * * * *
if ((timeNow - timeTemp) >= TEMP_INTERVAL)
{
timeTemp = timeNow;
int temp_Val = analogRead (Temp_Probe);
myFile = SD.open("08122020.txt", FILE_WRITE);
myFile.print("RT Min: ");
unsigned long Now = millis() / 60000;
myFile.print(Now);
myFile.print(". Temp: ");
myFile.println(((temp_Val) - 500) / 10);
Serial.print("Temp: ");
Serial.println(((temp_Val) - 500) / 10);
myFile.close();
}
// -----------------------------------------------------------------------------
// * * * * * OD INTERVAL * * * * *
if ((timeNow - timeOD) >= OD_INTERVAL)
{
timeOD = timeNow;
int od_Val = analogRead (OD_Probe);
myFile = SD.open("08122020.txt", FILE_WRITE);
myFile.print("RT Min: ");
unsigned long Now = millis() / 60000;
myFile.print(Now);
myFile.print(". OD: ");
myFile.println(od_Val);
Serial.print("OD: ");
Serial.println(od_Val);
myFile.close();
}
// -----------------------------------------------------------------------------
// * * * * * pH INTERVAL * * * * *
if ((timeNow - timepH) >= PH_INTERVAL)
{
int pH_Val;
switch (state) {
case ST_INIT:
for (int i = 0; i < 10; i++) //Get 10 sample value from the sensor for smooth the value
{
buf[i] = analogRead(pH_Probe);
delay(10);
}
for (int i = 0; i < 9; i++) //sort the analog from small to large
{
for (int j = i + 1; j < 10; j++)
{
if (buf[i] > buf[j])
{
temp = buf[i];
buf[i] = buf[j];
buf[j] = temp;
}
}
}
avgValue = 0;
for (int i = 2; i < 8; i++) //take the average value of 6 center sample
avgValue += buf[i];
float pH_Val = (float)avgValue * 5.0 / 1024 / 6; //convert the analog into millivolt
pH_Val = 3.5 * pH_Val; //convert the millivolt into pH value
myFile = SD.open("08122020.txt", FILE_WRITE);
myFile.print("RT Min: ");
unsigned long Now = millis() / 60000;
myFile.print(Now);
myFile.print(". pH: ");
myFile.println(pH_Val);
Serial.print("pH: ");
Serial.println(pH_Val);
myFile.close();
if (pH_Val >= pH_HIGH_THRESHOLD) {
state = ST_ADD;
timeCo2Add = timeNow;
timeCo2Chk = timeNow;
digitalWrite (CO2_GateValve, LOW);
}
else
timepH = timeNow;
break;
case ST_ADD:
if ((timeNow - timeCo2Add) >= PH_CO2_ADD_INTERVAL) {
state = ST_CHK;
digitalWrite (CO2_GateValve, HIGH);
}
break;
case ST_CHK:
default:
if ((timeNow - timeCo2Chk) >= PH_CO2_CHECK_INTERVAL) {
state = ST_INIT;
timepH = timeNow;
}
break;
}
}
}