[SOLVED] void setup() loops like program is resetting itself

Arduino Nano;
When I run the procedure void findLastEntry(), line 288 near the end, in a separate sketch, all works fine. I rely on the speed of the loop to avoid having a blocking “for” loop. When I put it in the “main” sketch, setup not ony runs once, but about every second, like it is receiving somekind of reset. Even more bizar is when uncomment line 314, 315 or 316, the serial output goes haywire.

sketch uses 26248 84%
global variables 911 44%

I have the following information in eeprom:
address value
0 1
1 0
2 2
3 0
4 4
5 0
6 6
7 0
8 8
9 0
10 74
11 and further 0

It has something to do with te Serial.print I think, because when line 302 to 317 are marked as comments, all is well. I can’t find out what is causing the problem and I don’t know how to monitor the memory. Any help is appreciated.

The sketch is a little long but I set it up for testing so that the void setup has “detected” a power failure and is going to initialize itself again. Well, that is what I am trying to acheive. Therfore I wanted to test only with Serial.print to see if I was getting the right data.

I have added the sketch as an attachement because it exceeds the maximun allowed length ( 9000 characters).

Thanks for reading.

incubatorDev.ino (16.4 KB)

Resets are most often caused by one of two things: violating an array bound, or power supply fluctuations.

If the Arduino is not sharing power with high current loads like motors or heaters, then check for array problems.

Recall that character arrays require a zero terminator, so the comment here is wrong:

  uint8_t  timeByte[2];                     // two addresses and closing 0

We STRONGLY recommend to use the “n” versions of the C-string functions where available. Lines like the following can overwrite array boundaries,

  sprintf( lastTempBme280, " %02u.%1u", (int)tempBme280, (int)(tempBme280 * 10) % 10 );

so replace it with

  snprintf( lastTempBme280,sizeof(lastTempBme280)," %02u.%1u", (int)tempBme280, (int)(tempBme280 * 10) % 10 );

When you do that, you will discover that you have not allocated enough space in the output string tempBme280.

Thank you very much for your reply. I didn't know the snprint function and makes completely sense. Fixed the glitch with the timeByte and corrected the code. I had already noticed that there was sometimes a strange behaviour with the temp and humidity output on the display, but never connected the dots that it had to do with the array and space allocation.
Well learned something new.
Thanks again.

Hopefully you made lastTempBme280 etc. larger, as this format " %02u.%1u" requires 6 elements minimum.

Yes, yes I did. After your help putting me on the right track and after re-studying the commands I noticed my errors. Sometimes while coding, you ( I) get some kind of "tunnel view". Only concentrating on one thing. Well that's between my ears as a hobyist. Thanks.