Issues with program restarting during exe.... SOLVED

Don’t know how to flag this posted as Solved, but it is… Thx everyone!!!
…,…

Enjoying reading ideas in this forum. Hoping to get some he :slight_smile: lp that has me puzzled… I am new to Arduino programing but not with computer programming in general. (Better at PASCAL back in the 1980’s… lol)

I am writing a program for a pool heater… i have 4 dallas temperature sensors hooked up… the code is a medium size program so far… 700+ lines of code but not exceeding the arduino Mega SRAM or sketch memory… below is the memory usage of my sketch as it stands now.

Sketch uses 15030 bytes (5%) of program storage space. Maximum is 253952 bytes.
Global variables use 2166 bytes (26%) of dynamic memory, leaving 6026 bytes for local variables. Maximum is 8192 bytes.

couple of variables i use — there are many many more, but issues right now with these.

byte NumberOfSensors;
unsigned long TempReadingTime; // var used to time the loop counter
unsigned long LoopMax = 7000; // var only do temp check every 7 seconds.

I the void setup i have the following code…
NumberOfSensors = sensors.getDeviceCount(); // there are 4 sensors hooked up.

then there are the following commands

void loop()
GetAndDisplayTemps(NumberOfSensors); // this procedure reads the sensors and displays on LCD
TempReadingTime = millis();
do
{
// do stuff like check for buttons being press

}while (millis()- TempReadingTime < LoopMax); // does the loop for 7 sec, then goes back to top of main program

when i have less procedure (voids) and the program is a bit smaller, it works wonderful… then i get to a point where i start to loose things… the NumberOfSensors goes back to 0 at the start of each loop of the main program… the first TempReadingTime is around 4160 on its first read thru. Then as i have problems with adding more code (the added code is not even executed because buttons have to be pressed in the do/while loop to get to it… and i’m not pressing them)

to find out what is going on… all i do is add Serial.print(" - stuff -"); to the code and now my NumberOfSensors will change to 50, after the second loop of the main program… have seen the TempReadingTime var go into the billions, with the arduino only being on for a minute or less.

After adding a few more Serial.print("… suff…"); to see the value of more variables… now the void loop() starts to execute … nothing displays on the Serial monitor, and the entire program starts over like you hit the reset button… and all i have added are Serial.print commands.

Just want to know if anyone has any ideas as to what might be happening. As i said, to a point the code works correctly and does what i want, just adding more code (that does not even get exe) caused issues with variables changing values and the are only changed in the void Setup().

Your symptom is a sign of memory corruption, possible caused by using String library/dynamic memory allocation. Or perhaps you overstepped on array boundary.

You need to post the complete program if you want help with it.

…R

Hi, Welcome to the forum.

Please read the first post in any forum entitled how to use this forum. http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code. It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom.... :)

arduino_new: Your symptom is a sign of memory corruption, possible caused by using String library/dynamic memory allocation. Or perhaps you overstepped on array boundary.

Yeah - seconded.

when i have less procedure (voids) and the program is a bit smaller, it works wonderful

OP - are you adding new functions for each sensor? When you have N identical things, arrays of classes are your friend.

here is the full code… thx for any help.

Sorry for any confusion…before i added procedure TogglePump and ToggelValve, the program to that point would exe just fine. it was after adding this procedures that problems started… then by just adding some lines for code ie… Serial.print… things got worse. Both of those procedures are not even being executed they are just added code. the program in total is much longer than i have here… i started over by adding a few things at a time till i had issues. The setup and Variables have remained the same throughout, no changes there.

I thought about memory overflows and string issues, but the code works to a point and i am not changing the decorations at all.

PaulMurrayCbr – not adding any procedures for the sensors, it just the variable that holds the number of sensors that i am having issue with.

PoolRebuild_102.ino (48.2 KB)

It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. Just use cstrings - char arrays terminated with 0.

The problem with the use of Strings is that everything works fine until it doesn't

...R

Thanks Robin... Only problem is, I got rid of this string entirely, and chached the procedure to just a bunch of LCD.print... lines and this had no effect

Still might be worth another look. As I said I am new to Arduino stuff. Thx

ErikG1165: Thanks Robin... Only problem is, I got rid of this string entirely, and chached the procedure to just a bunch of LCD.print... lines and this had no effect

You have not posted the revised program so we cannot see exactly what you did.

...R

I will post the code this evening for the sketch that currently works. Thx for everyone that is looking into this. Not encountered string issues before but I am new to Arduino programming. The program that works still has all the same strings and var declarations...

PaulMurrayCbr: Yeah - seconded. OP - are you adding new functions for each sensor? When you have N identical things, arrays of classes are your friend.

I figured it had to be something with the strings or an array... Sounds right. I will find the previous version of the program that worked and post it this evening ... Thx

When I wrote Reply #8 I wanted you to post the program in which you replaced Strings with cstrings - and (according to your Reply #7) it did not work.

…R

Which I2C library are you using? You have a lcd.write that prints an array for the degree symbol; I suspect that that should be lcd.write(Deg); for the normal LiquidCrystal library that would be the case.

sterretje: Which I2C library are you using? You have a lcd.write that prints an array for the degree symbol; I suspect that that should be lcd.write(Deg); for the normal LiquidCrystal library that would be the case.

Yes.. correct it writes a custom char to the LCD. Implamented that code long ago in the process. Not had any issues with that part.

The library version I will have to check. Downloaded it a while back and it has always worked in a few different sketches .

Robin2:
When I wrote Reply #8 I wanted you to post the program in which you replaced Strings with cstrings - and (according to your Reply #7) it did not work.

…R

I removed the strings from the program, just used lcd.setCursor and lcd.print in a Switch/case… it had now effect on the program

Attached is the sketch… also he it the print out made from the Serial.prints…

First exe of the program…

TOP OF THE PROGRAM — START LOOP …
LightReading = 742
Active Sensors = 3 TTL # of Sensors = 4

Temp_Reading[Pool] = 74
Temp_Reading[Htr1] = 0
The Pump is = 0 POOL IS HOT FLAG = 0
Valve #1 is = 0 LIGHT FLAG = 0

Just read Temp reading time … it is set to = 4503
Get and DISLAY TEMPS… Num = 3
Millis() = 9964
Temp Reading Time = 4282187580 <=======<<<<< “what it was just 4503 a second ago???”

12789707 < Loop Max = 7000

second pass of the program loop…

TOP OF THE PROGRAM — START LOOP …
LightReading = 743
Active Sensors = 60 TTL # of Sensors = 74

Temp_Reading[Pool] = 74
Temp_Reading[Htr1] = 74
The Pump is = 0 POOL IS HOT FLAG = 255
Valve #1 is = 0 LIGHT FLAG = 60

… Current Board Temp Returned == 76 ::: Max Temp = 85

the Flags are BOOL should only be 1/0???
My Sensors went from 3&4 to 60 and 74???

PoolTempNS_11.ino.ino (59.3 KB)

When values of things are changing without your having assigned a new value to them, that almost always means you're writing off the end of an array.

C has NO bounds checking on array access, so if you have a 4 element array named myarray and write to myarray4, you will scribble over whatever happens to be located after it in memory.

This frequently leads to resets, in addition to memory corruption (when the thing corrupted is a pointer or register address)

Go over your code and make sure that everywhere you assign values to elements of an array, there's no way that it could assign to an index out of range.

Hi, If you have an array that you declare as var[4], it has 4 elements, they are located at; var[0], var[1], var[2], var[3]...

There is no var[4] in your array.

If you do look in var[4] you will see some unassigned value that may change at anytime during the execution of your code.

https://www.arduino.cc/en/Tutorial/Arrays

Tom... :)

DrAzzy: When values of things are changing without your having assigned a new value to them, that almost always means you're writing off the end of an array.

C has NO bounds checking on array access, so if you have a 4 element array named myarray and write to myarray4, you will scribble over whatever happens to be located after it in memory.

This frequently leads to resets, in addition to memory corruption (when the thing corrupted is a pointer or register address)

Go over your code and make sure that everywhere you assign values to elements of an array, there's no way that it could assign to an index out of range.

Yep. I know how that goes... The variable s that are changing are not arrays. Number of sensors... Byte PoolHot.... Bool TimetempRead.... Unsigned int No Arrays

ErikG1165: Yep. I know how that goes... The variable s that are changing are not arrays. Number of sensors... Byte PoolHot.... Bool TimetempRead.... Unsigned int No Arrays

You can see another variable changing if an array is used incorrectly and writes into SRAM that belongs to the other variable.

The code you attached to Reply #14 has a large number of arrays.

...R

Temp_Readings can hold 3 ints.

int Temp_Readings[3];                       // var Array of the temperature readings from sensors.

Here you determine the number of devices:

void setup()
{
  NumberOfSensors = sensors.getDeviceCount();               // get the total number of sensors hooked up

Here you display the number of devices:

void loop()
{
  Serial.print("    TTL # of Sensors = ");
  Serial.println(NumberOfSensors);

…the number is 4.

TOP OF THE PROGRAM — START LOOP …
LightReading = 742
Active Sensors = 3 TTL # of Sensors = 4

Here you read 4 values into an array of length 3.

void GetAndDisplayTemps(int NSensors)
{
  for (i = 0; i < NumberOfSensors; i++)                     // Loop through each device, print out temperature data
  {
    Temp_Readings[i] = sensors.getTempFByIndex(i);        // read each sensor and put into memory array