Global variables: loosing the value

Hello together: I'm new in the forum, but I'm using Arduino for different applications. I have a strange problem with global variables in some big programs (22kb compiled more or less): In my programs I use global variables quite a lot because I have a lot of different process that are monitoring in real time sensors and I/O cards. In anycase I'm using not more than 50% of RAM memory (so I think this is not the problem). The problem is that some global values are correctly set to the value but during the loop loose their value and become == 0.

I can see very well the problem: I have made one code like that to debug.....

/*


loop


*/ void loop() { //Reading the inputs

ReadInputs(); flags.iStop = true;

. . . . . .

and then, in different functions inside the loop I go to check if the flags.iStop become == false.

this happen very offen: more or less 2 time each second....

This situation change if I move the variable declaration more UP in the code (in the same declaration block); but in this case some other variable present the same problem.

Somebody have any idea about that? Thanks in advance

If that's all the code you are going to show us, how are we supposed to help? Anything we say would just be a guess. Post the whole sketch.

In anycase I'm using not more than 50% of RAM memory

Do you consider the stack size ?

If you used 100% of RAM for global variables, even calling the function setup() or loop(), will change some of your global variables. With 50% for global variables, it just takes a deeper function stack with more local variables, to achieve the same effect.

But, as dxw00d pointed out, this is just a guess. You are able to create other problems, too.

Sorry if I posted few code but the sketch is very big.....

I think this can help more:

this is the declaration part... in this way the code is working seems without problem......

//Classi Parameters pars; Flags flags; ComPc comPc;

float Debug;

//Valori calcolati e di movimanto int motHEma = 0; int motO2ma = 0; int lettureSensori = 0; float letPrecHe = 0; float letPrecO2 = 0; float letPrecCorHe = 0; float letPrecCorO2 = 0; long valoreLettureHe = 0; long valoreLettureO2 = 0; long valoreLettureCorHe = 0; long valoreLettureCorO2 = 0; float movingPrecLetHe = 0; float movingPrecLetO2 = 0; int timeToMovePrecHE = 0; int timeToMovePrecO2 = 0;

//Luce di debug bool DebugStatus = false; bool SpeedPin = false;

//Ora interna int Ora; int Minuto; int Secondo; int Millisecondo = 0;

//Ingressi bool iStart = false; bool iStop = false; bool iCalibrate = false; bool Inp4 = false; bool Inp5 = false; bool Inp6 = false; bool Inp7 = false; bool Inp8 = false; //Uscite int OutPutsA = 0; bool OSicurezzaO2 = false; bool OStart = false; bool OStop = false; bool OCalibrate = false; bool Out5 = false; bool Out6 = false; bool Out7 = false; bool Out8 = false;

//Timers unsigned long LastTimeLoop;

//Gestione timers int const MaxTimers = 20;

int Ore[MaxTimers]; int Minuti[MaxTimers]; int Secondi[MaxTimers]; int Millisecondi[MaxTimers]; int TimerName[MaxTimers]; int TimerEnabled[MaxTimers];

..... but if I move the last 7 rows a little bit up the values of the variables declared after the arrays declaration can be loosed....

so if I declare this...

//Timers unsigned long LastTimeLoop;

//Gestione timers int const MaxTimers = 20;

int Ore[MaxTimers]; int Minuti[MaxTimers]; int Secondi[MaxTimers]; int Millisecondi[MaxTimers]; int TimerName[MaxTimers]; int TimerEnabled[MaxTimers];

... before this...

//Ingressi bool iStart = false; bool iStop = false; bool iCalibrate = false; bool Inp4 = false; bool Inp5 = false; bool Inp6 = false; bool Inp7 = false; bool Inp8 = false; //Uscite int OutPutsA = 0; bool OSicurezzaO2 = false; bool OStart = false; bool OStop = false; bool OCalibrate = false; bool Out5 = false; bool Out6 = false; bool Out7 = false; bool Out8 = false;

..... the last 16 variables are offten loosed....

about the use of the RAM i'm checking with this function during the cycle: and there are always more than 50% RAM free....

/*


freeRam


*/ int freeRam () { extern int heap_start, *brkval; int v; return (int) &v - (brkval == 0 ? (int) &heap_start : (int) __brkval); }

Thanks again guys...

If you're not going to post the whole thing, we can't help you. If it's too big to post, attach it instead. Click 'Additional Options...' under the text entry box.

OK, I have cleaned the code and isolated the problem: attached the sketch with the problem visible.
It is possible to load in Arduido and see the debug light that must be on but is off.
The problem is created by the Arrays managed in the function SetTime.

if you try to move the declaration part
//Timers
unsigned long LastTimeLoop;
int Ore[MaxTimers];
int Minuti[MaxTimers];
int Secondi[MaxTimers];
int Millisecondi[MaxTimers];
int TimerName[MaxTimers];
int TimerEnabled[MaxTimers];

at the end of the declaration seems to wotrk fine…

Thanks

TestCode.ino (18 KB)

The problem is created by the Arrays managed in the function SetTime.

if you try to move the declaration part //Timers unsigned long LastTimeLoop; int Ore[MaxTimers]; int Minuti[MaxTimers]; int Secondi[MaxTimers]; int Millisecondi[MaxTimers]; int TimerName[MaxTimers]; int TimerEnabled[MaxTimers];

at the end of the declaration seems to wotrk fine....

If you move the array declarations around, all that you are doing is changing what gets stepped on when the Arduino runs out of memory. That it appears to then "wotrk fine" simply means that you are not seeing, or are not concerned about, what is now getting stepped on.

The point is that you ARE running out of memory.

Yes, it make sense: thanks for support.

Bye

I have the same problem in a big project. This sucks! How do I know when the memory is "out"???? Shouldn't the arduino IDE tell me?

How do I know when the memory is "out"???? Shouldn't the arduino IDE tell me?

How would it know?

I'm using UTFT, UTOUCH, tinyFAT, OneWire, DallasTemp and maybe 100 global variables. IDE says "Binary sketch size: 62 226 bytes (of a 126 976 byte maximum)" on a Mega 1280.

Any hints?

Any hints?

Read the sticky threads at the top of this forum section.

I apologize for writing that it “sucks”.
I will never burden a forum again before having spent hours and hours of googling.
Thank you.

@xxxx - I still can't see your code, so I'm assuming you've fixed your problem.

Nope. freeMemory and freeRam shows a steady ~4300 bytes left.

There was a hint in reply 13. We're not psychic.

I suspect the problem is that you’re overflowing the bounds of one of those arrays. Here is one place you do it - I haven’t checked for others:

for (int i = 0; i <= MaxTimers; i++)

The ‘<=’ should be a ‘<’.

I also noticed this code:

  int DiffMillis;
  unsigned long ActualMills = millis();
  if (LastTimeLoop > ActualMills)
  {
    // questo ? il valore massimo di millisecondi :4,294,967,295 
    DiffMillis = 4294967295 - LastTimeLoop + ActualMills;
  }
  else
  {
    DiffMillis = ActualMills - LastTimeLoop;
  }

Please don’t do that. I don’t know whether it actually copes correctly with timer overflow, but it’s a horrible way to do it. If you want to know how many milliseconds have elapsed between two calls to millis(), do it like this:

unsigned long startTime = millis();
// do stuff
unsigned long elapsed = millis() - startTime;

I assume you can generalise that approach to do whatever it is you’re trying to do here. The important details in order for timer overflow to be handled correctly are:

  • Use unsigned long
  • Use subtraction not addition