because of timestamp. Arduino crash

Dear,

i'am working with arduino mega ADK and wireless schield for mine thesis. I try to logging data from a inductive sensore to the SD-card (memory).

If I don't use timestamp in my data string. Then I can run the program without any delay and my sensor testing without the arduino crash continuously. But I place my timestamp string in my datastring, and let my inductive sensor 1 time switching and my program crash. even with a delay(2000).

Do you know where my structural problems are? C is very new for me.

#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 [200]="";

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 [16];
    itoa(Nummer,Cijfer,16);
    char Logging [8]= "indu";
    char txt [5]=".txt";
    strcat (SdBestand, Logging);
    strcat (SdBestand, Cijfer);
    strcat (SdBestand, txt); 
   while (SD.exists(SdBestand)==true) {
      Nummer++;
      itoa(Nummer,Cijfer,4);
      strcpy (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);
  
  float distance = voltage / aref_voltage * 7;
  char afstand [8] = "";
  dtostrf(distance,6,2,afstand);
  
  String ledState = "";
  if(voltage <= 2.5)  {digitalWrite(ledPin,LOW);
                     ledState = "LOW - 0";
  }
  else {digitalWrite(ledPin,HIGH);
       ledState = "HIGH - 5";
  
  }
  int ms,s,m,h = 0;  
  Tijd = millis();
  h = Tijd/3600000;
  m = (Tijd/60000)-(h*60);
  s = (Tijd/1000)-(m*60)-(h*3600);
  ms = Tijd -(s*1000)-(m*60000)-(h*3600000);

   
  dataString = sensor + " lezen = " + Sensor1Reading + "  ;" + volt + "- Volt - " + afstand +"mm ; "+ ledState  +" ; " + h + "uur "+ m + "min" + s + "s "+ ms +" ms";  

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

This is de code where the problem is.

  int ms,s,m,h = 0;  
  Tijd = millis();
  h = Tijd/3600000;
  m = (Tijd/60000)-(h*60);
  s = (Tijd/1000)-(m*60)-(h*3600);
  ms = Tijd -(s*1000)-(m*60000)-(h*3600000);

   
  dataString = sensor + " lezen = " + Sensor1Reading + "  ;" + volt + "- Volt - " + afstand +"mm ; "+ ledState  +" ; " + h + "uur "+ m + "min" + s + "s "+ ms +" ms";  

  File dataFile = SD.open(SdBestand,FILE_WRITE);
  if (dataFile){
   dataFile.println(dataString);
   dataFile.println(Tijd);
   dataFile.close();
   Serial.println(dataString);
   Serial.println(SdBestand);
   Serial.println(Tijd);
   delay(1000); //1000ms

Thank you for your effort.

Tim Matthijs

If I don't use timestamp in my data string. Then I can run the program without any delay and my sensor testing without the arduino crash continuously. But I place my timestamp string in my datastring, and let my inductive sensor 1 time switching and my program crash. even with a delay(2000).

It is most certainly not necessary to create a huge String to write to the file. You can write one value at a time, followed by writing the field separator.

If you do find it easier to understand what is going on, using sprintf() to populate a char array will use fewer resources than the String object you are using now.

Dynamic String creation consumes a lot of memory, of which your Arduino has very little. Static string manipulation uses the same memory over and over.

I think you are running out of memory.