Create new file.

Hi there.
I am building A datalogger. I Want it to initialize THE program (no problem), i have made a while loop( when digitalpin=High ) start logging( no problem) but i Want it to create THE new file each time THE digital pin is turned on. As it is right now, THE making of THE new file is done i void setup. Thus it just continues writing in THE same file.

Please anyone WHO know What to do?

Best regards bastian

What is the problem ?

You obviously have the code to create a new file so why not use it when you need to ?
The problem is possibly how to derive a unique new filename each time, but there are ways round that. How often do you expect to have to create a new file ?

Why all THE capitals IN your Post ?

Sorry about the capital letters. Iphone with Danish vocabulary sucks.
I need the arduino to eb turned on all the time to show current temps and pressures on a LCD screen. And then by the switch turn on datalogging to the sd card. And then stop when turned of. Then i enedes it to save the file and create a new file. That i Can start logging to when i turn it on again.

Something like this perhaps.

set recording to false

start of loop()
  if button becomes pressed and recording is false
    read previous filename from a file on the SD card
    create new filename
    open new file
    close new file
    write current filename to a file on the SD card
    set recording to true
  end if
   
  if data received and recording is true
    open file for writing
    write data to file
    close file
  end if
end of loop()

If you have an RTC the filename could be based on date and time. If not then give it a new number each time.

Thank you very much. I managed to get i work. It is actually pretty simple. I think a better upload the working file so others can enjoy the community.

 /*
  SD card datalogger
 
 This example shows how to log data from three analog sensors 
 to an SD card using the SD library.
 	
 The circuit:
 * analog sensors on analog ins 0, 1, and 2
 * SD card attached to SPI bus as follows:
 ** MOSI - pin 11
 ** MISO - pin 12
 ** CLK - pin 13
 ** CS - pin 4
 
 created  24 Nov 2010
 modified 9 Apr 2012
 by Tom Igoe
 
 This example code is in the public domain.
 	 
 */

#include <SD.h>
#include <Wire.h>

// On the Ethernet Shield, CS is pin 4. Note that even if it's not
// used as the CS pin, the hardware CS pin (10 on most Arduino boards,
// 53 on the Mega) must be left as an output or the SD library
// functions will not work.
const int chipSelect = 53;
int controlPin0 = 7; // digtital input pin to control start and stop of datalogger
int controlPin1 = 12; // digtital output pin to make false/true 
int controlPin2 = 13; // digtital input pin to make false/true 
int analogPin5 = A5;
int analogPin6 = A6;
int analogPin7 = A7;
int analogPin8 = A8;
int analogPin9 = A9;
int analogPin10 = A10;
int analogPin11 = A11;
int analogPin12 = A12;
int analogPin13 = A13;
int analogPin14 = A14;
int analogPin15 = A15;

 


// the logging file
File logfile;

void setup()
{
 
 // Open serial communications and wait for port to open:
  Serial.begin(9600);
  Serial.println();
  
  Serial.print("Initializing SD card...");
  // make sure that the default chip select pin is set to
  // output, even if you don't use it:
  pinMode(53, OUTPUT);
  pinMode(controlPin0, INPUT);
  pinMode(controlPin1, OUTPUT);
  pinMode(controlPin2, INPUT);
  
  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    return;
  }
  Serial.println("card initialized.");
 }

void loop()
{
  if (digitalRead(controlPin0)==LOW)
  {
     digitalWrite(controlPin1, LOW);
  }
  
if (digitalRead(controlPin0)==HIGH&&digitalRead(controlPin2)==LOW)
  {
   
  // create a new file
  char filename[] = "LOGGER00.CSV";
  for (uint8_t i = 0; i < 100; i++) {
    filename[6] = i/10 + '0';
    filename[7] = i%10 + '0';
    if (! SD.exists(filename)) {
      // only open a new file if it doesn't exist
      logfile = SD.open(filename, FILE_WRITE); 
      break;  // leave the loop!
    }
  }
 
  
  if (! logfile) {
    Serial.println("couldnt create file");
    return;
  }
  
  Serial.println("card initialized.");
  
  Serial.print("Logging to: ");
  Serial.println(filename);
  
  logfile.println("Temperature0,temperature1,temperature2,Temperature3,temperature4,temperature5,temperature6,temperature7,pressure0,pressure1,pressure2");    

  Serial.println("Temperature0,temperature1,temperature2,Temperature3,temperature4,temperature5,temperature6,temperature7,pressure0,pressure1,pressure2");
 
 digitalWrite(controlPin1, HIGH);
 
 delay (1000);

  }
  
  
  if (digitalRead(controlPin0)==HIGH && digitalRead(controlPin2)==HIGH)
  {

  // make a string for assembling the data to log:
File logfile = SD.open("filename", FILE_WRITE);

  // read 11 sensors and append to the string:
  float tempPin0 = (2*(analogRead(analogPin8)*5.0 / 1023)- 2.05) / 0.005;
  float tempPin1 = (2*(analogRead(analogPin9)*5.0 / 1023)- 2.05) / 0.005;
  float tempPin2 = (2*(analogRead(analogPin10)*5.0 / 1023)- 2.05) / 0.005;
  float tempPin3 = (2*(analogRead(analogPin11)*5.0 / 1023)- 2.05) / 0.005;
  float tempPin4 = (2*(analogRead(analogPin12)*5.0 / 1023)- 2.05) / 0.005;
  float tempPin5 = (2*(analogRead(analogPin13)*5.0 / 1023)- 2.05) / 0.005;
  float tempPin6 = (2*(analogRead(analogPin14)*5.0 / 1023)- 2.05) / 0.005;
  float tempPin7 = (2*(analogRead(analogPin15)*5.0 / 1023)- 2.05) / 0.005;
  float pressurePin0 = analogRead(analogPin5)*5.0 / 1023;
  float pressurePin1 = analogRead(analogPin6)*5.0 / 1023;
  float pressurePin2 = analogRead(analogPin7)*5.0 / 1023;
  

  // if the file is available, write to it:
  if (logfile) {
    
    logfile.print(tempPin0);
    logfile.print(", ");
    logfile.print(tempPin1);
    logfile.print(", ");
    logfile.print(tempPin2); 
    logfile.print(", ");
    logfile.print(tempPin3);
    logfile.print(", ");
    logfile.print(tempPin4);
    logfile.print(", ");
    logfile.print(tempPin5);
    logfile.print(", ");
    logfile.print(tempPin6);
    logfile.print(", ");
    logfile.print(tempPin7);
    logfile.print(", ");
    logfile.print(pressurePin0);
    logfile.print(", ");
    logfile.print(pressurePin1);
    logfile.print(", ");
    logfile.print(pressurePin2);
 

   
    // print to the serial port too:
   
    Serial.print(tempPin0);
    Serial.print(", ");
    Serial.print(tempPin1);
    Serial.print(", ");
    Serial.print(tempPin2);
    Serial.print(", ");
    Serial.print(tempPin3);
    Serial.print(", ");
    Serial.print(tempPin4);
    Serial.print(", ");
    Serial.print(tempPin5);
    Serial.print(", ");
    Serial.print(tempPin6);
    Serial.print(", ");
    Serial.print(tempPin7);
    Serial.print(", ");
    Serial.print(pressurePin0);
    Serial.print(", ");
    Serial.print(pressurePin1);
    Serial.print(", ");
    Serial.print(pressurePin2);
   
  }  
 
  // if the file isn't open, pop up an error:
  else {
    Serial.println("error opening file");
  } 
  
  logfile.println();

  Serial.println();
  
  delay(1000);  
}
}

best regards Bastian

I'm glad that you got it working but how about improving it ?

Much of your code is repetitive and would lend itself to the use of arrays, particularly the 8 temperature readings. You could read the sensors and write the values in a single for loop, for instance.

How long does it take to find the next filename when there are already many, say 90, of them ? If you saved the name of the current file in a file then you would not need to check the name of the next one.