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.
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.
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?
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
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
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.
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