Skip to content

Instantly share code, notes, and snippets.

@alexbarbosa
Created April 18, 2013 21:15
Show Gist options
  • Save alexbarbosa/5416290 to your computer and use it in GitHub Desktop.
Save alexbarbosa/5416290 to your computer and use it in GitHub Desktop.
simple clock without use of rtc
#include <LiquidCrystal.h>
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
void setup() {
lcd.print("What time is it?");
delay(1000);
lcd.begin(16, 2);
lcd.setCursor(0, 0);
lcd.print("Apr, 15, 2013"); // print out the date
}
//thhis is a list of int variables used in this clock program
int s=0;
int sec=0;
int hrs=0;
int minutes=0;
int initialHours = 02;//variable to initiate hours
int initialMins = 0;//variable to initiate minutes
int initialSecs = 00;//variable to initiate seconds
//this method is for seconds
int seconds()
{
s = initialHours*3600;
s = s+(initialMins*60);
s = s+initialSecs;
s = s+(millis()/1000);
return s;
}
//this method is for hours
int hours()
{
hrs = seconds();
hrs = hrs/3600;
hrs = hrs%24;
return hrs;
}
//this method is for minutes
int mins()
{
minutes = seconds();
minutes = minutes/60;
minutes = minutes%60;
return minutes;
}
int secs()
{
sec = seconds();
sec = sec%60;
return sec;
}
//this loop will conintue to keep looping so that the time can go as follows
void loop(){
digitalClockDisplay();
}
void printDigits(byte digits){
if(digits < 10)
lcd.print('0');
lcd.print(digits);
}
char sep()
{
s = millis()/1000;
if(s%2==0)
{
lcd.print(":");
}
else {
lcd.print(" ");
}
}
void digitalClockDisplay(){
lcd.setCursor(0,1);
printDigits(
hours());
sep();
printDigits(mins());
sep();
printDigits(secs());
}
@SatyabanUkil1959
Copy link

Nice project and it works. But how to set the initial time?

@BATMan-Phln
Copy link

The time shows fine until 9:59:59. It goes from 9:59:59 to 248:243:236. Also, if I change the start time to (line 16) to 08 or 09, I get an error.
error: invalid digit "9" in octal constant
int initialHours = 09;//variable to initiate hours

I'm REALLY new at this so please don't explain it as if to an expert. Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment