Try to empty char array.

Dear,

I try to send a file to my sd card. But every time I reset I want another name with a higher number. Therefore, I must everytime make my char array is empty in my while loop.

But i fail in this. Do you know the answer or should I go in a different direction. Any suggestion is welcome.

ps. I use mega ADK with a Wireless shield

My regards, Tim Matthijs

#include <string.h>
#include <stdlib.h>
#include <SD.h>
#define aref_voltage 5.0       // De 5V word met de Aref verbonden en gecontroleerd met de multimeter.
//Sensor 1 pin Variabels
const int Sensor1Pin = A0;     // De 1e sensor word aan de analoge pin 0 verbonden. (variabel: interger)
int Sensor1Reading;     // the analog reading from the analog resistor divider (variabel: integer)
const int chipSelect = 4;
const int ledPin = 49;
unsigned long Tijd;
char SdBestand [20]="";
char SdBestandleeg [20]="";

void setup() {
    // start de serieële communicatie op aan 9600bits per seconde;
    Serial.begin(9600); 
    analogReference(EXTERNAL);
    Serial.print("Initializing SD card...");
    pinMode(10,OUTPUT);
    pinMode(ledPin,OUTPUT);
    if (!SD.begin(chipSelect)) {
    Serial.println ("Card failed, or not present");
    return;
    }
    Serial.println("card Initialized.");
    // De voltage die op de AREF word aangesloten (0-5V) word gebruikt als referentie.
    
    int Nummer = 0;
    char Cijfer [4];
    itoa(Nummer,Cijfer,4);
    char Logging [8]= "Loggekn";
    char txt [5]=".txt";
    strcat (SdBestand, Logging);
      strcat (SdBestand, Cijfer);
      strcat (SdBestand, txt); 
 while (SD.exists(SdBestand)==true) {
      Nummer++;
    //has to empty the char SdBestand!!!
      itoa(Nummer,Cijfer,4);
      strcat (SdBestand, Logging);
      strcat (SdBestand, Cijfer);
      strcat (SdBestand, txt);
     } 
      
 }
 

 

void loop() 
{
  Sensor1Reading = analogRead(Sensor1Pin);  
  String dataString = "";
  String sensor = "Sensor1 ";
  // Zet de analoge waarde om naar voltage. geberuik de referentie voltage.
  float voltage = Sensor1Reading * aref_voltage / 1024; 
   
  //dtostrf(FLOAT,WIDTH,PRECSISION,BUFFER); 
  char volt[8] = "";
  dtostrf(voltage,6,2,volt);
  
  String ledState = "";
  if(voltage <= 2.5)  {digitalWrite(ledPin,LOW);
                     ledState = "LOW";
  }
  else {digitalWrite(ledPin,HIGH);
       ledState = "HIGH";
  
  }
  Tijd = millis(); 
  dataString = sensor + " lezen = " + Sensor1Reading + "  -" + volt + " Volt" + " - "+ ledState + " - " + Tijd + "ms" ;  

  File dataFile = SD.open(SdBestand,FILE_WRITE);
  if (dataFile){
   dataFile.println(dataString);
   dataFile.close();
   Serial.println(dataString);
   Serial.println(SdBestand);
   delay(1000); //1000ms   
  }
  else {Serial.println("error opening datalog.txt");
  Serial.println(SdBestand);
  delay (1000); //1000ms
  }
}

This part is where i talk about

    int Nummer = 0;
    char Cijfer [4];
    itoa(Nummer,Cijfer,4);
    char Logging [8]= "Loggekn";
    char txt [5]=".txt";
    strcat (SdBestand, Logging);
      strcat (SdBestand, Cijfer);
      strcat (SdBestand, txt); 
 while (SD.exists(SdBestand)==true) {
      Nummer++;
    //has to empty the char SdBestand!!!
      itoa(Nummer,Cijfer,4);
      strcat (SdBestand, Logging);
      strcat (SdBestand, Cijfer);
      strcat (SdBestand, txt);
     }

Hi,
SdBestand is a global variable that is continously updated by appending new text at every cycle of the loop:

   while (SD.exists(SdBestand)==true) {
      Nummer++;
    //has to empty the char SdBestand!!!
      itoa(Nummer,Cijfer,4);
      strcat (SdBestand, Logging);
      strcat (SdBestand, Cijfer);
      strcat (SdBestand, txt);
     }

simply change the first "strcat" with a "strcpy". This function copies a string (the second argument) in another (the first argument) overwriting the initial content. Another solution is to reset the string with a terminator character:

SdBestand[0] = '\0';
strcat(...);
strcat(...);

But every time I reset I want another name with a higher number.

If you really mean "reset" then you have to store a variable in non-volatile memory, the EEPROM for example.


Rob

ea123:
simply change the first "strcat" with a "strcpy". This function copies a string (the second argument) in another (the first argument) overwriting the initial content. Another solution is to reset the string with a terminator character:

It works, Thank you very muche

If you really mean "reset" then you have to store a variable in non-volatile memory, the EEPROM for example.

I think that he simply wants to automatically create a new filename at every startup. To do this he generates a new filename by incrementing a counter until the filename does not already exists on the SD card.

ea123:
I think that he simply wants to automatically create a new filename at every startup. To do this he generates a new filename by incrementing a counter until the filename does not already exists on the SD card.

idd, Only i have now one problem. Mine couter go 3 and then jumps tot 10;

Exampel: every time i reset. cijfer counter go's:
1=>2=>3=>10=>11=>12=>13=>20=>21=>22=>23=>30=>31=>32=>33=>100=>...

By making mine char bigger. I can endless count up. But is not realy a sollution

What did i do wrong?

Hi,
the problem is here:

itoa(Nummer,Cijfer,4);

the third argument of the function is the base used to represent the number into a string, in your case it is 10 (use 2 for binary, 16 for exadecimal).
Anyway there is a better way to format a string: use the sprintf function, you can also specify the number of digits of the counter (format %03d in the example below)

sprintf(SdBestand, "%s%03d%s", Logging, Cijfer, txt);