I am making a simple datalogger with a SD card.
The idea is that every time the Arduino starts it will make a new logfile fx. sernsor1.txt, sensor2.txt and so on.
Therefore I have a file containing the last number used, but the problem is that something is wrong in the way I make the filename.
In the sketch below i use a String concatenating including the nummber, but the file.open fails because the format of the filename is wrong.
Any hints ?
#include <SdFat.h>
#include <SdFatUtil.h>
#include <ctype.h>
//Create the variables to be used by SdFat Library
Sd2Card card;
SdVolume volume;
SdFile root;
SdFile file;
String stringOne = "Sensor";
const uint8_t SdChipSelect =5;
char name[] = "Test.txt"; //Create an array that contains the name of our file.
char numname[] = "number.txt"; //name of file containing last number
char contents[256]; //This will be a data buffer for writing contents to the file.
char in_char=0;
void setup(void)
{
Serial.begin(9600); //Start a serial connection.
pinMode(5, OUTPUT); //Pin 10 must be set as an output for the SD communication to work.
card.init(); //Initialize the SD card and configure the I/O pins.
volume.init(card); //Initialize a volume on the SD card.
root.openRoot(volume); //Open the root directory in the volume.
file.open(root, numname,O_READ); //Read the number
char num=file.read();
Serial.println(num);
file.close();
num++;
file.open(root, numname,O_WRITE); //save new number
file.print(num);
file.close();
Serial.println("Number:");
Serial.println(num);
//Create new filename and file
stringOne=stringOne+num+".txt";
Serial.println(stringOne);
int strlen=stringOne.length()+1;
char charBuf[strlen];
stringOne.toCharArray(charBuf, strlen);
Serial.println(stringOne);
//Open and write to new file
file.open(root,name , O_CREAT | O_APPEND | O_WRITE); // Tested OK
// file.open(root,stringOne , O_CREAT | O_APPEND | O_WRITE); //Problems
file.print("testing");
file.close();
}
void loop(void){
file.open(root, numname, O_READ); //Read the number from numfile
in_char=file.read(); //Get the first byte in the file.
while(in_char >=0){ //If the value of the character is less than 0 we've reached the end of the file.
Serial.print(in_char); //Print the current character
in_char=file.read(); //Get the next character
}
file.close(); //Close the file
Serial.println("Delay");
delay(1000); //Wait 1 second before repeating the process.
}