Running out of flash memory in Arduino Mega

I'm using Arduino Mega to run a program wich contains the millis function:

if ((millis() - millisala49) > ((sala49-48)*3000000)){
  sala49 = 0;
     mega1.shiftWrite(3, HIGH);
mega1.shiftWrite(4, LOW);
}

In this funcition the "int sala49" is usualy zero and nothing major happen, but when it turns into a number different from zero It consumes memory from the Arduino, and it eventualy runs out of It and resets within three hours of operation (someting that cannot happen). There are 10 millis functions like the shown here and all of them is executed every 10 minutes at the same time (that is the maximum interval that I can work with).
I would like to know if Arduino volatile memory can be expanded easly or exist other time function that doesn't fill the Arduino memory with "trash".

Post the rest of the code, which is where the actual problem lies. millis() is not the problem.

1 Like

I've tried all the possibillities for this problem, and all the tests lead to the millis function.
I don't know why but ia appears that this function uses a little bit of the arduino flash memory every time that it's executed and the variable sala49 ins greater than 0.

There are inexpensive FRAM modules, I use 8 x 32K on the I2C bus. I can use 8 of them. They are also available on SPI. When powering down they will remember just like EEPROM but they are much faster, no delay in read or write, and over a billion cycles.

Thank's for the advice!
But could It be used yust as an expansion of the existing memory. Without the need of making the code write or read things on it?
Because It's not actual useful data thai is consuming the Arduino memory but rather is yust "trash" that is coming from the millis function.

Utterly impossible on the Mega. Only the bootloader can write to flash memory.

The problem is in the code you forgot to post.

2 Likes

I have doubts..
me and millis(), we're friends..
use it by the boat loads..

please provide a small sketch that shows this issue with millis..

sorry for your troubles.. ~q

This is the specific part of the code that is giving me trouble. Specialy the "void tempo"


void tempo(){
  if (sala48 > 0){                                        //Caso a variável sala 48 for maior que zero.
if ((millis() - millisala48) > ((sala48-48)*3000000)){    //Caso a fução millis menos a fução millis da sala 48 seja maior que o número de aulas digitado (é feita a conversão de aulas para milli segundos, considerando que a duração de uma aula seja 50 minutos).
  sala48 = 0;                                             //A variável sala48 é mudada para 0.
     mega1.shiftWrite(1, HIGH);
  mega1.shiftWrite(2, LOW);                               //O LED verde é aceso e o LED vermelho é apagado.                                          //A variável rest48 é mudada para 1.
}
  }
  if (sala49 > 0){
if ((millis() - millisala49) > ((sala49-48)*3000000)){
  sala49 = 0;
     mega1.shiftWrite(3, HIGH);
  mega1.shiftWrite(4, LOW);
}
}
if (sala50 > 0){
if ((millis() - millisala50) > ((sala50-48)*3000000)){
  sala50 = 0;
mega1.shiftWrite(5, HIGH);
  mega1.shiftWrite(6, LOW);
}
}
if (sala51 > 0){
if ((millis() - millisala51) > ((sala51-48)*3000000)){
  sala51 = 0;
     mega1.shiftWrite(7, HIGH);
  mega1.shiftWrite(8, LOW);
}
}
if (sala52 > 0){
if ((millis() - millisala52) > ((sala52-48)*3000000)){
  sala52 = 0;
     mega1.shiftWrite(9, HIGH);
  mega1.shiftWrite(10, LOW);
}
}
if (sala53 > 0){
if ((millis() - millisala53) > ((sala53-48)*3000000)){
  sala53 = 0;
     mega1.shiftWrite(11, HIGH);
  mega1.shiftWrite(12, LOW);
}
}
if (sala54 > 0){
if ((millis() - millisala54) > ((sala54-48)*3000000)){
  sala54 = 0;
     mega1.shiftWrite(13, HIGH);
  mega1.shiftWrite(14, LOW);
}
}
if (sala55 > 0){
if ((millis() - millisala55) > ((sala55-48)*3000000)){
  sala55 = 0;
     mega1.shiftWrite(15, HIGH);
  mega1.shiftWrite(16, LOW);
}
}
if (sala56 > 0){
if ((millis() - millisala56) > ((sala56-48)*3000000)){              
  sala56 = 0;
     mega1.shiftWrite(17, HIGH);
  mega1.shiftWrite(18, LOW);
}
}
if (sala57 > 0){
if ((millis() - millisala57) > ((sala57-48)*3000000)){
  sala57 = 0;
     mega1.shiftWrite(19, HIGH);
  mega1.shiftWrite(20, LOW);
}
}
mensageminicial();
}

   
void mensageminicial(){
  for (counter = 0; counter < 42; ++counter) {                                                             
 if ( ! mfrc522.PICC_IsNewCardPresent())   //Caso nenhuma TAG seja lida.
  {
  lcd.clear();
  lcd.setCursor(1, 0);              
  lcd.print("Hora: ");              
  lcd.print(rtc.now().hour());      
  lcd.print(":");
  lcd.setCursor (10,0);                   
  lcd.print(rtc.now().minute());    
  lcd.print(":");                   
  lcd.print(rtc.now().second());    
  lcd.setCursor(0, 2);
  lcd.print("Data: ");              
  lcd.print(rtc.now().day());       
  lcd.print("/");                   
  lcd.print(rtc.now().month());     
  lcd.print("/");                   
  lcd.print(rtc.now().year()); 
  delay(5000);                            //Mostra hora e data e aguarda 7 segundos.
  
 if ( ! mfrc522.PICC_IsNewCardPresent())  //Caso nenhuma TAG seja lida. 
  {
lcd.clear();
  lcd.print("Temp.: "); 
    lcd.print(bmp.readTemperature());
    lcd.println(" *C "); 
    delay(5000);                          //Mostra a Temperatura e aguarda 7 segundos.


 if ( ! mfrc522.PICC_IsNewCardPresent())  //Caso nenhuma TAG seja lida.
  {
  lcd.clear();
   lcd.setCursor(1, 0);
     lcd.print("Insira a TAG");          //Mostra a mensagem "Insira a TAG".
  }else{
    tag();
  }
  }else{
    tag();
  }
 }else{
    tag();
  }                                      //Caso alguma TAG seja lida, é executada a fução void tag.

delay (5000);                            //Aguarda 7 segundos
  }           
  tempo();             
}
     

What part of "post the rest of the code" do you not understand?

1 Like

Is salaxx a char?
Why don't you perform the salaxx-48 operation when you enter the character and directly store the decimal value it represents?
It's because of things like this that code fragments are useless.

Maybe not.
We are only guessing what happens with your program. If you want help you need to post the entire program...

Remember this? https://forum.arduino.cc/t/arduino-restart-after-two-hours-that-is-had-initialized/1149799/2

You have been struggling with the same program for at least two months, and you are still not providing the information this community needs to help you...

1 Like

This line has definitely incorrect math in it, because the overflow in the last parenthesis.
Without correcting these errors there is no point in moving forward.

1 Like

That is just one of problems in the small part posted. I imagine there are many more problems in the rest of the code.

@renan_s I strongly recommend to study this tutorial, and learn how to use millis() correctly.

3 Likes

The function "tempo()" is calling the function "mensageminicial()".
The function "mensageminicial()" is calling the function "tempo()".

About 43 delays of 5 seconds is 215 seconds. Let's say 30 bytes stack per function and a stack size of maximum 3kbyte. Then it crashes after 3 hours.

6 Likes

good catch :slight_smile:

@renan_s it called "recursion". As general rule, you can't develop a code for microcontrollers such way.

3 Likes

It appears your knowledge of microcontrollers is very limited. You cannot read or write any memory without writing code to do it. When coming out of reset it will fetch an instruction at a known location, after that it is your code that does its thing. If you read the data sheet on the processor you would have found that it does not have the internal busses pined out so there is no way to add additional memory into the existing memory map. The FRAM libraries are well written and the I/O is transparent to your program.

That is completely untrue.

A variable always occupies the same amount of memory - irrespective of its value.

On an 8-bit AVR, an int will occupy 16 bits - storing a value of zero will still occupy all those 16 bits.

As @Koepel has spotted, your problem (or, at least, one of them) is due to a recursive loop of tempo() calling mensageminicial() calling tempo() ...

2 Likes

@renan_s , did you read Post #2 by @jremington?

I suspect you will not get the help you need until you follow that recommendation

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