Bonjour a tous ,
Dans le cadre d'un projet je cherche a enregistrer des données dans ma carte SD. Ces données son enregistrer sous formes de tableau Excel appelée CAN_000 CAN_001 ainsi de suite des que 5400 ligne sont enregistrer.
Mon probleme : J'effectue un enregistrement tout fonctionne , je deconnecte la carte SD pour lire mes donnée et si je souhaite rebrancher ma carte SD a l'arduino acutellement je ne suis pas capable de poursuivre un enregistrement si des fichier son deja present dans la carte.
Je cherche donc a faire la chose suivant : ( Cest la partie 2 qui me pause probleme )
1 -Verifier si le fichier existe
if (SD.exists("example.txt")) {
Serial.println("example.txt exists.");
} else {
Serial.println("example.txt doesn't exist.");
}
2 - Si le fichier par exemple CAN_002 existe alors on creer un nouveau fichier en incrementant du precedant ici CAN_003 ****
comment detecter le fichier le plus rescent dans la carte SD / le dernier creer ? puis trouver son nom et l'incrementer.
3 - Ecriture dans le fichier
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");
Si besoin voici mon code actuel compilé et fonctionnant sur mon projet
#include <SD.h>
#include <TimeLib.h>
#include <mcp_can.h>
#include <SPI.h>
//*********************CAN Sheild declarations********************
long unsigned int rxId;
unsigned char len = 0;
unsigned char rxBuf[8];
char msgString[128]; // Array to store serial string
#define CAN0_INT 2 // Set INT
MCP_CAN CAN0(53); // Set CS
//*********************SD Sheild declarations**********************
File myFile;
const int SD_CS_PIN = 9;
//*********************Record Excel declarations*******************
char filename[16];
unsigned long reads_recorded = 0;
uint16_t i = 0;
//*********************Switch and Led declarations*****************
const int buttonPin = 40; // the number of the pushbutton pin
const int ledPin = 41; // the number of the LED pin
//********************* Program SETUP *****************************
void setup()
{
Serial.begin(115200);
// Initialize MCP2515 running at 16MHz with a baudrate of 500kb/s and the masks and filters disabled.
while(!CAN0.begin(MCP_ANY, CAN_500KBPS, MCP_16MHZ) == CAN_OK);
Serial.println("MCP2515 Initialized Successfully!");
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);
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");
}
}
en vous remerciant pour l'aide