Reset Problem with struct and variables in struct

Hi everyone, I'm programming on Arduino not for long time.
I use program for several years on JAVA, but with C and managing the memory I have strange behaviours.

I have the following Struct to hold the status of a railway signal.

//------------------------
const int STRUCT_SIZE = 65;
//------------------------
struct Signal {
	int Ladres;               // basedcc  Base Address
	int Hadres;               // basedcc+numaspect-1 High Signal Address
	byte numaspect;           // Number of Aspects(max 10)
	byte numled;              // Number of leds per signal (max 8)
	unsigned int leds[MAXASPECTS];  // led settings for each aspect
	byte lastasp;             // last Aspect
	bool inverted;            // inverted Y/N
	byte port[MAXLED];        // Pin usati
	byte MaxLight[MAXLED];	  // Max led brightness
	byte aspect;              // Selected aspect
	int ontime;
	int offtime;
	byte Psein;  			//0=Psein; 255 geen Psein; 10+x=eindstand Psein
	char admin[5];          // administratieve code
	int fading;             // 0= no fading; >0 = fading time in msec
	byte type;				// type: 0=manual,  >0 from database;
	byte signalType;// 1=Principal, 2=Advanced, 3=Combined, 4=shunting, 5=Auxiliary
	int darkPhase;          // 0= no dark Phase; >0 = dark Phase time in msec
	byte associatedSignal;
	byte oldAspect;
//	byte offOnRed;			//Switch off advanced signal on RED
};
Signal Allsignals[MAXSIGNAL];

Each time that I send a command is writing on EEPROM the status of the signal.

EEPROM.put(A_SIGNALS + cursignal * STRUCT_SIZE, Allsignals[cursignal]);

Every is running ok, but if I add the commented line to have one more variable in my struct, the code become unstable and each time that I run it reset my ATMega328p.
How is possible that adding a variable on my struct the arduino is resetted.

Thanks
Marcello

Please post a complete sketch that illustrates the problem.

For instance, how many structs are there in the array and how many bytes does each take ?

this "simple" line will add additional 65 bytes of memory. Are you sure you aren't running out of memory?
btw, have you checked your other variables?
Do you really need int for addresses and timestamps, flags? are you expecting signed values?

This is the file.
Signal_Decoder.ino (66.1 KB)

I'm using an AtMega238p. I should have 1K of memory.
At the moment I'm creating only 2 signals into the array, so max 128 bytes.
Is calculate max 8 signals so max 512 bytes for the array.
Doing a
int size = sizeof(Allsignals[cursignal]); isa givne me 63 bytes

What is the memory usage reported by the compiler ?

Where I see it ?
can be this one ?

"C:\Sloeber\arduinoPlugin\packages\arduino\tools\avr-gcc\7.3.0-atmel3.6.1-arduino5/bin/avr-size" -A "C:\Sloeber\sloeber-workspace\SBBSignalDecoder\Release/SBBSignalDecoder.elf"
C:\Sloeber\sloeber-workspace\SBBSignalDecoder\Release/SBBSignalDecoder.elf :
section size addr
.data 350 8388864
.text 26408 0
.bss 1527 8389214
.comment 17 0
.note.gnu.avr.deviceinfo 64 0
.debug_aranges 392 0
.debug_info 41723 0
.debug_abbrev 4039 0
.debug_line 13972 0
.debug_frame 2912 0
.debug_str 5134 0
.debug_loc 19070 0
.debug_ranges 2040 0
Total 117648

After the compiler has finished there should be details of the memory used in the compiler output window at the bottom of the screen in the black window

I'm using Sloeber as ide.

DCC_Decoder.h (8.6 KB)

Maybe I get the info you need :

Sketch uses 26688 bytes (82%) of program storage space. Maximum is 32256 bytes.
Global variables use 1885 bytes (92%) of dynamic memory, leaving 163 bytes for local variables. Maximum is 2048 bytes.
Low memory available, stability problems may occur.

Says it all

Any suggestion to free more memory ???

Can you put any of your variables in PROGMEM ?
Are you making use of the F() macro
Are all of your variables of the smallest appropriate data type ?

Thanks a lot for your BIG help.
I cleaned the code, removed unused vars, remove useless or debuging serial.printf and now the code is running as I will.
Thanks a lot again

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.