Nothing Very Interesting here, more for newbies learning about interfacing hardware.
This code will work on uno and mega, it will display the Uptime on the LCD sheild like the one in the image below:
Header 1 |
---|
/* 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/>.
*/ #include <LiquidCrystal.h> //Standard LCD Lbrary
//************************** Just Some basic Definitions used for the Up Time LOgger ************// long Day=0; int Hour =0; int Minute=0; int Second=0; int HighMillis=0; int Rollover=0;
//**** Setting up LCD ************// // select the pins used on the LCD panel LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
// define some values used by the panel and buttons int lcd_key = 0; int adc_key_in = 0; int button =0; #define btnRIGHT 1 #define btnUP 2 #define btnDOWN 3 #define btnLEFT 4 #define btnSELECT 5 #define btnNONE 6
int Screen =1;
//************** Setup routine - Runs once at power up **************************// void setup(){ Serial.begin(9600); // starting Serial Com's
//Doing A Quick Start UP Splash Screen lcd.begin(16, 2); // start the library lcd.setCursor(0,0); delay(1000); lcd.print("Arduino UpTime"); lcd.setCursor(0,1); delay(1000); lcd.print("Mike Ratcliffe"); lcd.setCursor(0,1); delay(1000); lcd.setCursor(0,1); lcd.print("Free Software "); delay(1000); lcd.setCursor(0,1); lcd.print("Mike Ratcliffe"); delay(1000); lcd.setCursor(0,1); lcd.print("Free Software "); delay(3000); //Clearing LCD SCREEN lcd.setCursor(0,0); lcd.print(" "); lcd.setCursor(0,1); delay(1000); lcd.print(" ");
};
//****** Main Loop - Put your Code Here ********************// void loop(){
uptime(); //Runs the uptime script located below the main loop and reenters the main loop print_Uptime_Serial(); print_Uptime_LCD(); delay(50); };
//************************ Uptime Code - Makes a count of the total up time since last start ****************//
void uptime(){ //** Making Note of an expected rollover *****// if(millis()>=3000000000){ HighMillis=1;
} //** Making note of actual rollover **// if(millis()<=100000&&HighMillis==1){ Rollover++; HighMillis=0; }
long secsUp = millis()/1000;
Second = secsUp%60;
Minute = (secsUp/60)%60;
Hour = (secsUp/(60*60))%24;
Day = (Rollover*50)+(secsUp/(60*60*24)); //First portion takes care of a rollover [around 50 days]
};
//******************* Prints the uptime to serial window **********************// void print_Uptime_Serial(){
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")); };
//******************* Prints the uptime to serial window **********************// void print_Uptime_LCD(){ lcd.setCursor(0,0); lcd.print("Uptime Counter: ");
lcd.setCursor(0,1); lcd.print(" ");//Clearing LCD lcd.setCursor(0,1); lcd.print(Day); lcd.setCursor(3,1); lcd.print("Day"); lcd.setCursor(8,1); lcd.print(Hour); lcd.setCursor(10,1); lcd.print(":"); lcd.setCursor(11,1); lcd.print(Minute); lcd.setCursor(13,1); lcd.print(":"); lcd.setCursor(14,1); lcd.print(Second);
}; |
Leave it running for a day or so against a good clock to see what people mean about drift. Put it somewhere warm or cold to see how this effects the stability of the timer.