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 };
}; |