Files in SD card

Hi guys, I’m trying to write a code where I get a new .txt file whenever a press the reset button.
So far, I could write it deleting the old file and creating a new one, but I couldn’t make it create more files, such as datalog1.txt, datalog2.txt…
Also, if I use numbers in the file’s name (datalog1.txt) the code doesn’t create the file and I have no idea why. :cold_sweat:

This is the code:

/*

#define W_CLK 5 // Pin 8 - connect to AD9850 module word load clock pin (CLK)
#define FQ_UD 9 // Pin 9 - connect to freq update pin (FQ)
#define DATA 6 // Pin 5 - connect to serial data load pin (DATA)
#define RESET 7 // Pin 6 - connect to reset pin (RST).

#define pulseHigh(pin) {digitalWrite(pin, HIGH); digitalWrite(pin, LOW); }
#include <SD.h>
const int chipSelect = 10;

// transfers a byte, a bit at a time, LSB first to the 9850 via serial DATA line
void tfr_byte(byte data)
{
for (int i=0; i<8; i++, data>>=1) {
digitalWrite(DATA, data & 0x01);
pulseHigh(W_CLK); //after each bit sent, CLK is pulsed high
}
}

// frequency calc from datasheet page 8 = * /2^32
void sendFrequency(double frequency) {
int32_t freq = frequency * 4294967295/125000000; // note 125 MHz clock on 9850
for (int b=0; b<4; b++, freq>>=8) {
tfr_byte(freq & 0xFF);
}
tfr_byte(0x000); // Final control byte, all 0 for 9850 chip
pulseHigh(FQ_UD); // Done! Should see output
}

void setup() {
// configure arduino data pins for output
pinMode(FQ_UD, OUTPUT);
pinMode(W_CLK, OUTPUT);
pinMode(DATA, OUTPUT);
pinMode(RESET, OUTPUT);

pulseHigh(RESET);
pulseHigh(W_CLK);
pulseHigh(FQ_UD); // this pulse enables serial mode - Datasheet page 12 figure 10
Serial.begin(9600);
Serial.print(“Initializing SD Card…”);
if (!SD.begin(chipSelect)) {
Serial.println(“Card failed, or not present”);
// don’t do anything more:
return;
}
Serial.println(“card initialized.”);
if (SD.exists(“DATALOG.txt”)) {
SD.remove(“DATALOG.txt”);

}
else {
Serial.println(“DATALOG.txt doesn’t exists”);
}
}

void loop() {
for (unsigned long fq = 30000; fq<= 50000; fq+=50) {
sendFrequency(fq); // freq
//while(1);
int sensorValue = analogRead(A0);
float dataVoltage = sensorValue * (5.0/1023.0);

// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.

File dataFile = SD.open(“DATALOG.txt”, FILE_WRITE);

// if the file is available, write to it:
if (dataFile) {
dataFile.print(dataVoltage);
dataFile.print(",");
dataFile.print(fq);
dataFile.println();
dataFile.close();
// print to the serial port too:
Serial.print(dataVoltage);
Serial.print(",");
Serial.print(fq);
Serial.println();
}
// if the file isn’t open, pop up an error:
else {
Serial.println(“error opening DATALOG.txt”);
}
delay(1);
}
while (1) {}
}

Does anybody know how I can do it? I tried to define the name of the variable in the very beginning but it didn’t went through.
Thanks.

If you want to create a new file every time I would write some code that tested whether a named file exists, and write a loop to execute that for each potential new file name until you found one that doesn't exist. I would use snprintf() to format the file names.

you need to change your string to a char array in order to add numbers to the name

 currentLine ="Datafile3.txt";
 char filename[currentLine.length()+1];
 currentLine.toCharArray(filename, sizeof(filename));
 dataFile = SD.open(filename);

Jasit, I'm trying to use your code and I think that's exactly what I was trying to do. Thanks XD. I also want to send the files to a PC/Mac, but since I'm building my circuit on a breadboard, the arduino wifi-shield is not convenient. Is there any device that allows me to do it? I found this, but apparently it works communicating arduino-arduino and not arduino-pc. http://www.amazon.com/nRF24L01-Wireless-Transceiver-Arduino-Compatible/dp/B00E594ZX0/ref=sr_1_1?ie=UTF8&qid=1403120393&sr=8-1&keywords=wifi+arduino

Thanks.

I haven't used this product, and only discovered it today, but it seems like it might be good for your wireless desire.

https://www.spark.io/

Explanation of the spark core can be found here, on its kickstarter:

https://www.kickstarter.com/projects/sparkdevices/spark-core-wi-fi-for-everything-arduino-compatible

I was thinking about using XBee, but I'm not sure if I can create a clean communication between my PC and the arduino with it. I've been searching about it, and it seems like most of the users use it to communicate two arduinos. And I don't really need the internet itself. I just want to create a network to send the files.

what's the latest code here?