Close

Just A Few Useful Arduino Hacks: SRAM & Uptime

A project log for Hacking the way to growing food

Using Technology And A Hackers Mindset To Grow Food. Last Updated [16/01/2021]

michael-ratcliffeMichael Ratcliffe 08/28/2015 at 00:076 Comments

The Next Blog is about a water usage monitor, the total usage is a great readout to have but how can we trust it? We could poll the data to IOT Mysql database, or to an sd card, but maybe that is not available for the project and we just need something simple.

Uptime Counter

It is useful to have an uptime counter on the arduino so we are aware of any recent resets. Mainly because we need to be aware of any problems making it reset and know that any readouts for totals [ie water usage] are not a true representation.

Checking SRam Ussage

Sram ussage isnt really a problem if we are making a small script, but there will come a time when your script grows and you need to be aware of running out of Sram and need to refine the code to reduce how much we are relying on the SRam.

A Sram shortage will cause a code to hang randomly.

On any large code it is a good idea to have a readout of the Maximum used Ram, and either make the operator aware of it or simply reset the controller. The code measures the amount of Sram used and sets a flag "Ram_error=1" if we exeed 70%.

UpTime Counter:

A better updated version can be seen here:

https://hackaday.io/project/7008-fly-wars-a-hackers-solution-to-world-hunger/log/25043-updated-uptime-counter

/* This Script is the bare bones needed to Keep a Uptime counter that will survive the 50 day timer rollover
This will not give a uptime of great accuracy over long periods, but it will let you see if your arduino has reset
if you want better accuracy, pull the Unix time from the IOT, External RTC or GPS module
Also Reconnecting the serial com's will reset the arduino. So this is mainly useful for a LCD screen
 
 
Michael Ratcliffe  Mike@MichaelRatcliffe.com
   
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
 
 
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
 
 
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see .
 
 
   
*/
 
 
 
 
 
 
//************************** Just Some basic Definitions used for the Up Time LOgger ************//
long Day=0;
int Hour =0;
int Minute=0;
int Second=0;
int SecondStamp=0;
int Once=0;
 
 
 
 
//************** Setup routine - Runs once at power up **************************//
void setup(){
Serial.begin(9600); // starting Serial Com's
 
};
 
 
//****** Main Loop - Put your Code Here ********************//
void loop(){
 
uptime(); //Runs the uptime script located below the main loop and reenters the main loop
};
 
 
 
 
 
 
//************************ Uptime Code - Makes a count of the total up time since last start ****************//
//It will work for any main loop's, that loop moret han twice a second: not good for long delays etc
void uptime(){
//** Checks For a Second Change *****//  
if(millis()%1000<=500&&Once==0){
SecondStamp=1;
Once=1;
}
//** Makes Sure Second Count doesnt happen more than once a Second **//
if(millis()%1000>500){
Once=0;
}
 
 
 
 
                         if(SecondStamp==1){
                           Second++;
                           SecondStamp=0;
                           print_Uptime();
                          
                         if (Second==60){
                          Minute++;
                          Second=0;                
                          if (Minute==60){
                          Minute=0;
                          Hour++;
                                                  
                         if (Hour==24){
                          Hour=0;
                          Day++;
                         }
                         }
                         }
                         };
                        
                        
};
 
 
//******************* Prints the uptime to serial window **********************//
void print_Uptime(){
 
  Serial.print(F("Uptime: ")); // The "F" Portion saves your SRam Space
  Serial.print(Day);
  Serial.print(F("  Days  "));
  Serial.print(Hour);
  Serial.print(F("  Hours  "));
  Serial.print(Minute);
  Serial.print(F("  Minutes  "));
  Serial.print(Second);
  Serial.println(F("  Seconds"));
};

SRam Code:

/*
 
Based on the work of:
 
 
This Scrip includes the basics for measuring the Ram ussage of a script and displays it as a Percentage to the serial window
It is usefull for including in larger sketches where you could run out of ram
This script checks the Ram usage and sets a flag "Ram_error" if we are ussing more than 70% of it
 
 
 
Michael Ratcliffe  Mike@MichaelRatcliffe.com
   
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
 
 
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
 
 
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see .
       
*/
 
 
//************************* Change Value Below For Board Of Choice ***********************//
/* ATMega168 ATMega328P ATmega1280 ATmega2560
SRAM 1024 bytes 2048 bytes 8192 Bytes 8192 Bytes
*/
 
 
const int Total_Ram = 8192; //change this value for the correct one from above data
 
 
 
 
//****************************Seting up the Ram check**************************************//
int freeRam () {
extern int __heap_start, *__brkval;
int v;
return (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval);
};  
 
 
//********Just Some variables we use to convert to % ************//
float Ram=3;
float Ram_Ussage=0;
int Ram_error=0;
 
 
//***** Setup, Ran once to startserial com's *******************//
void setup() {
  Serial.begin(9600);
 
 
}
 
 
//*******************Main Loop, Put your code Here **************************//
void loop (){
Max_Ram();  // We need to call this at multiple points in the main loop,  Sram Ussage will change thru the loop
 
 
 
 
Serial.print("Ram Used:");
Serial.print(Ram_Ussage);
Serial.println("%");
 
if(Ram_error==1){
   Serial.print("LOW RAM WARNING");
    }
 
delay(100);
 
};
 
 
//*************** End Of Main Loop *****************************************//
                                                               
  
//************* Loop for calculating Ram Ussage, called from main loop***//  
  
    void Max_Ram(){
           
            if(freeRam()>=Ram){
             Ram=(freeRam());
             Ram_Ussage=(((Total_Ram-Ram)/Total_Ram)*100);
                              };
            
             if(Ram_Ussage>=70){
              Ram_error=1; // Use this set flag to warn the user of a potential Ram ussage problem, or reset the arduino if it becomes true
                              };
 
 
};

As usuall these codes will be avalible to download from: www.MichaelRatcliffe/Projects

Discussions

Michael Ratcliffe wrote 09/12/2015 at 16:29 point

You are correct a millis impimnted one would be easier, I have uploaded it here: 

https://hackaday.io/project/7008-fly-wars-a-hackers-solution-to-world-hunger/log/25043-updated-uptime-counter 

  Are you sure? yes | no

Armin wrote 09/12/2015 at 08:16 point

Hi, why not use the millis() directly for your uptime? it will solve the problem you mentioned about long delays.

Also I would recommend resetting the microcontroller regulary (daily?) it will solve any long term problems.(libraries not handleing memory correct etc).

Untested code below:

uptime(){

long secsUp = millis()/1000;

Second = secsUp%60;

Minute = (secsUp/60)%60;

Hour = (secsUp/(60*60))%24;

Day = (secsUp/(60*60*24));

}

  Are you sure? yes | no

Michael Ratcliffe wrote 09/12/2015 at 15:50 point

Hey thanks for the interest Armin, 

I wanted an uptime counter that would survive the timer roll over, I suppose we could use the mills method and add a method to detect a roll-over and add the corresponding number of days. ie:
Day = (secsUp/(60*60*24))+ (Rolloverevents*52); 
Have you got any reference for the need to rest the MCU? I have never had any problems with long term operation but would like to know of any potential problems in more detail.

  Are you sure? yes | no

Armin wrote 09/12/2015 at 21:55 point

There is no specific problem it is more a precaution. If any of the libraries you use has an error with memory (not freeing it after use) it will cause problems which are very difficult to debug. These memory leaks you will not notice on short runs because it might only be a few bytes. Also timer roll-over is one of the potential problems I like to solve with a reset ;) .

For long term time keeping I would use a Real Time Clock (RTC). It might cost a bit more but it is more accurate. Also with a RTC it is possible to send the MCU in sleep mode and still keep the time.

  Are you sure? yes | no

Michael Ratcliffe wrote 09/12/2015 at 22:03 point

In the main system I am pulling the time via the Ethernet shield and sending a reboot email during the setup() loop. 
This is just a quick code for those who dont have any extra hardware handy but would like to have an idea about the uptime for any cumulative values [total water volume etc] 

I haven't noticed any memory leaks [the Ram check runs in most of my programs] But I may add a ram overflow statement that will reset the MCU before ram completely runs out to be safe.  Thanks for mentioning it, maybe I have been lucky to have good library set so far.

  Are you sure? yes | no

Armin wrote 09/12/2015 at 22:16 point

Yes I understand there is never one correct way of doing these sort of things :)

There is also an arduino library: http://playground.arduino.cc/code/time

I ran in some trouble when using a library for a GSM module. All sorts of problems (especially long term) like: memory leaks, using too much ram and endless loops. Everything worked fine when doing tests on my desk. When going for long term tests and more challenging conditions (bad reception) the code was less than perfect. It became clear that the code was written expecting that everything was perfect.

  Are you sure? yes | no