element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Vertical Farming
  • Challenges & Projects
  • Design Challenges
  • Vertical Farming
  • More
  • Cancel
Vertical Farming
Blog Automated Green House Blog:9 -  Some Generic Arduino Tricks - SRam - UpTime
  • Blog
  • Forum
  • Documents
  • Polls
  • Files
  • Events
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: m.ratcliffe
  • Date Created: 27 Aug 2015 10:29 PM Date Created
  • Views 735 views
  • Likes 1 like
  • Comments 0 comments
  • adapted_greenhouse
  • arduino
Related
Recommended

Automated Green House Blog:9 -  Some Generic Arduino Tricks - SRam - UpTime

m.ratcliffe
m.ratcliffe
27 Aug 2015

The Last Blog was 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%.



A better implementation can be found here:

Automated Green House Blog:9.1 - Updated uptime counter

Up Time Code

/* 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 <http://www.gnu.org/licenses/>.

 

 

  

*/

 

 

 

 

 

 

//************************** 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 <http://www.gnu.org/licenses/>.

      

*/

 

 

//************************* 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

                              };

 

 

};

  • Sign in to reply
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube