hello everyone i have made the following code allowing me to save my data in excels table files.
The problem is the following:
If I plug in to save and then unplug my SD card to read the data and then plug it back in, there are already files inside and my code can't write to the next file or to a new file.
I'm looking for a way to get back to my previous table and continue writing in it.
The code :
//********************* Program SETUP *****************************
void setup()
{
Serial.begin(115200);
// Initialize MCP2515 running at 16MHz with a baudrate of 500kb/s and the masks and filters disabled.
if(CAN0.begin(MCP_ANY, CAN_500KBPS, MCP_16MHZ) == CAN_OK)
Serial.println("MCP2515 Initialized Successfully!");
else
Serial.println("Error Initializing MCP2515...");
CAN0.setMode(MCP_NORMAL); // Set operation mode to normal so the MCP2515 sends acks to received data.
pinMode(CAN0_INT, INPUT); // Configuring pin for /INT input
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
Serial.println("MCP2515 Library Receive Example...");
while(!SD.begin(SD_CS_PIN));
SERIAL_PORT_MONITOR.println("SD card is present & ready");
sprintf(filename, "can_%.03d.csv", i); //initialise filename
myFile = SD.open(filename, FILE_WRITE);
if (myFile) // it opened OK
{
myFile.println("Time,Pression,Temperature,co2_hydrogen,humidity"); //add header to file
myFile.close();
SERIAL_PORT_MONITOR.println("File created!");
}
else
SERIAL_PORT_MONITOR.println("Error opening file");
}
//********************* Write into Excel ***********************
void writeToFile(float press_pascal, float temp_degC, float co2_true, float humi_pourcent)
{
myFile = SD.open(filename, FILE_WRITE);
if (myFile) // it opened OK
{
myFile.print(String(minute()));
myFile.print(":");
myFile.print(String(second()));
myFile.print(";");
myFile.print(String(press_pascal, 2));
myFile.print(";");
myFile.print(String(temp_degC, 2));
myFile.print(";");
myFile.print(String(co2_true, 2));
myFile.print(";");
myFile.println(String(humi_pourcent, 2));
myFile.close();
}
else
SERIAL_PORT_MONITOR.println("Error opening file");
}
//********************* Program LOOP ***************************
void loop()
{
if (digitalRead(buttonPin)== HIGH)
{
digitalWrite(ledPin, HIGH); // turn LED on
if(!digitalRead(CAN0_INT)) // If CAN0_INT pin is low, read receive buffer
{
CAN0.readMsgBuf(&rxId, &len, rxBuf); // Read data: len = data length, buf = data byte(s)
if((rxId & 0x80000000) == 0x80000000) // Determine if ID is standard (11 bits) or extended (29 bits)
sprintf(msgString, "Extended ID: 0x%.8lX DLC: %1d Data:", (rxId & 0x1FFFFFFF), len);
else
sprintf(msgString, "Standard ID: 0x%.3lX DLC: %1d Data:", rxId, len);
Serial.print(msgString);
if((rxId & 0x40000000) == 0x40000000){ // Determine if message is a remote request frame.
sprintf(msgString, " REMOTE REQUEST FRAME");
Serial.print(msgString);}
else
{
for(byte i = 0; i<len; i++)
{
sprintf(msgString, " 0x%.2X", rxBuf[i]);
Serial.print(msgString);
}
}
Serial.println();
Serial.println("_____Pression_____");
uint16_t press_raw;
memcpy(&press_raw, &rxBuf[0], 2); //buf[0] is the first byte in buf, 2 because you want 2 bytes
float press_pascal = ((press_raw * 0.0078125) - 250);
Serial.println(press_pascal, 2); // value to copy on excel
Serial.println("_____Temperature_____");
uint16_t temp_raw;
memcpy(&temp_raw, &rxBuf[2], 2); //buf[2] is the third byte in buf, 2 because you want 2 bytes
float temp_degC = ((temp_raw * 0.03125)-273)-3 ;
Serial.println(temp_degC, 2);
Serial.println("_____CO2_Hydrogen_____");
uint16_t co2_raw;
memcpy(&co2_raw, &rxBuf[4], 2); //buf[4] is the fifth byte in buf, 2 because you want 2 bytes
float co2_true = ((co2_raw * 1) - 0) ;
Serial.println(co2_true, 2);
Serial.println("_____Humidity_%_____");
uint16_t humi_raw;
memcpy(&humi_raw, &rxBuf[6], 2); //buf[6] is the six byte in buf, 1 because you want 1 bytes
float humi_pourcent = ((humi_raw * 0.4) - 0);
Serial.println(humi_pourcent, 2);
writeToFile(press_pascal, temp_degC, co2_true, humi_pourcent);
++reads_recorded;
}
}
else
{
digitalWrite(ledPin, LOW); // turn LED off
Serial.println("off");
}
//********************* Makes new files excel ***********************
//create new filenames for every 5400 lines written to file
if(reads_recorded == 5400) { //reset counter
reads_recorded = 0;
//increment file counter
++i;
//update finename
sprintf(filename, "can_%.03d.csv", i);
myFile = SD.open(filename, FILE_WRITE);
if (myFile) // it opened OK
{
//add header to new file
myFile.println("Time,Pression,Temperature,co2_hydrogen,humidity");
myFile.close();
SERIAL_PORT_MONITOR.println("New File created!");
}
else
SERIAL_PORT_MONITOR.println("Error opening file");
}
}