program stops after approx 4hrs

I have an application in which the loop() reads some sensors each 1 minute, does some simple calculations, sets a few relay pins, displays a line of data each 10 minutes, writes the same data to an SD card each 10 minutes, uses delay() to wait for the remainder of 1 minute. It seems to work fine nearly 4 hours (23 displays & SD Card writes) then stops.

Where should I begin looking?

The fifth line of loop()

or at String

Where should I begin looking?

I would start by looking at the code obviously. We can't help with that because you didn't post it. You gave us the symptom, but forgot to bring in the patient.

Sorry about not including code. See following (I realize that I don’t follow all formatting standards, but like to see more code on screen at one time). also I am running a Mega2560 board connected to a Windows10 laptop.

void loop() {
char buffer[5], CF1[5], CF2[5], CF3[5], CRsen1[6], CRsen2[6], CRsen3[6], CVout1[5], CVout2[5], CVout3[5];
int ThermPin1 = 1;                                                        // Analog input pin for 1st thermistor voltage (o/p evac tubes)
int ThermPin2 = 2;                                                        // Analog input pin for 2nd thermistor voltage (o/p hw tank)
int ThermPin3 = 3;                                                        // Analog input pin for 3rd thermistor voltage (b'ment air temp)
double R=10000.0, BB1=3470.0, B2=3950.0, Rsen1=0.0, Rsen2=0.0, Rsen3=0.0; // Fixed resistance in the voltage divider; "B" value; Sensor resistance
double T1=0.0, F1=0.0, T2=0.0, F2=0.0, T3=0.0, F3=0.0;                    // T is Temp Celcius  F is Temp Farenheit
double Vout1=0.0, Vout2=0.0,Vout3=0.0;                                    // Analog pin reading
dataFile = SD.open("SensorLg.txt", FILE_WRITE);                           // SensorLg.txt is the log file for sensor data readings 
// collect and display reading from 1st thermistor (NTC 10k B=3470)  (o/p evac tubes - in basement)
ThermSensor(ThermPin1, R, BB1, Vout1, Rsen1, F1, T1);
itoa((int(Rsen1)),CRsen1,10); itoa((int(Vout1)),CVout1,10);      
delay (1000);
// collect and display reading from 2nd thermistor(NTC 10k B=3470) (o/p hw tank)
ThermSensor(ThermPin2, R, BB1, Vout2, Rsen2, F2, T2);
itoa((int(Rsen2)),CRsen2,10); itoa((int(Vout2)),CVout2,10);
delay (1000);
// collect and display reading from 3rd thermistor(NTC 10k B=3470) (o/p hw tank)
ThermSensor(ThermPin3, R, BB1, Vout3, Rsen3, F3, T3);
itoa((int(Rsen3)),CRsen3,10); itoa((int(Vout3)),CVout3,10);
// Relay 2 pin 7  -- HW Heater
// Relay 3 pin 8  -- HW Loop Pump (LOW when either relay 2 or relay 4 LOW)
// relay 4 pin 9  -- SOlar Pump
// relay 5 pin ?? -- Basement space heater
// Determine relay settings HIGH=120vac power off; LOW=120vac power on;
int pin7=0, pin8=0, pin9=0;
char Cpin7[5], Cpin8[5], Cpin9[5];
// vacation time limits hot water heater use just to not freeze  105.0 winter hot water;   80.0 summer hot water;  50.0 prevent freezing
F1=F1-10;    itoa((int(F1)),CF1,10);    // adjust evac tube to actual based on test readings
F2=F2+ 6;    itoa((int(F2)),CF2,10);    // adjust HW to actual based on test readings
F3=F3+ 2;    itoa((int(F3)),CF3,10);    // adjust b'ment air to actual based on test readings
if ((F1-10.0 > F2) || (F1 > 100.0)) { digitalWrite(9, LOW);  digitalWrite(8, LOW);   }   
   else                             { digitalWrite(9, HIGH); } 
if ((F2 < 105.0) && (mm<30)) { digitalWrite(7, LOW);  digitalWrite(8, LOW);  }   //  105.0 winter hot water;   80.0 summer hot water;  50.0 prevent freezing
   else        { digitalWrite(7, HIGH);  }
if (((mm<15)&& (mm>10)) || ((mm<35) && (mm>30)) || ((mm<55) && (mm>50)) && ((hh<19) && (hh>7))) {digitalWrite(9, LOW);  digitalWrite(8, LOW); // activate Solar pump 12 min/hr to prevent overheating
   digitalWrite(7, LOW); digitalWrite(8, LOW); digitalWrite(9, LOW);}  // activate Solar & HW Heater 12 min/hr to provide some heat
if ((digitalRead(9)==HIGH) && (digitalRead(7)==HIGH)) {digitalWrite(8, HIGH);  };    
if (digitalRead(7)==HIGH) {pin7=1;} else {pin7=0;};
if (digitalRead(8)==HIGH) {pin8=1;} else {pin8=0;};
if (digitalRead(9)==HIGH) {pin9=1;} else {pin9=0;};
if (k==cycle) { k=0;   }
if (k==0) { if (dataFile) {
    dataString = int(F1) + Comma + int(F2) + Comma + int(F3) + Comma + pin7 + Comma + pin8 + Comma + pin9 + Comma + mo + Comma + dd + Comma + yy + Comma + hh + Comma + mm + Comma + ss;
    dataFile.println(dataString);
    dataFile.close();  
    // print to the serial port too:
    Serial.println(dataString);  }  }
//
  k++;           
delay (58000);
TimeOfDay();
}

Please post your code

Holy moly. I get that you like to see it one way. But if you want someone here to try to read it and work through it then you're going to have to give us something in the way WE like to read code. I'm not going to frustrate myself trying to give you FREE help. Please, try not to make this harder on us or we will simply move on to someone else.

Please post complete code. The problem might not be where you think it is. Post the whole thing, or at least a complete example that illustrates the problem.

dataString isn't a member of the String class is it? The String class, and specifically the + operator used to do concatenation tends to fragment memory since it has to allocate larger and larger pieces every time and leaves holes in the heap. Get enough holes and your heap hits the stack and all bets are off. String is great on a PC with tons of memory and garbage collection. But on a microcontroller it is a thing to be avoided and is known for causing random crashes after a program runs for some time.

The following is a global definition:

String dataString = “”, Comma = ", "; // data to write to SD Card

I am attaching the full .ino but without reformatting. Is there an automatic way to reformat?
If I do that manually, I would be concerned about making many keying errors.

sketch_TimeSensorVac_170927.ino (8.88 KB)

evanmars:
...
or at String

Delta_G:
...
dataString isn't a member of the String class is it? The String class, and specifically the + operator used to do concatenation tends to fragment memory since it has to allocate larger and larger pieces every time and leaves holes in the heap. Get enough holes and your heap hits the stack and all bets are off. String is great on a PC with tons of memory and garbage collection. But on a microcontroller it is a thing to be avoided and is known for causing random crashes after a program runs for some time.

jencliff:
If I do that manually, I would be concerned about making many keying errors.

That's why, when you think you may need someone else to ever read the code you should follow standard formatting. Try Control-T which is auto-format, but I don't know if it will sort that whole mess out.

If you really want to use your own formatting rules, then you need to accept the fact that you may be totally on your own with the code.

I'm not even going to try to open that and read it if you say it is formatted like the bit you posted. I'll wait for you to fix it to something I can read. ONE statement per line and that includes all the variable definitions. I don't want to have to wade through a bunch of long ass lines looking for where a variable was defined. ONE per line please.

This

if (digitalRead(7)==HIGH) {pin7=1;} else {pin7=0;};

is a heap of obfuscation that could be

pin7 = digitalRead(7);