hello..i want to ask about arduino mega..if i want to create clock on my project should i buy rtc shield or just make some coding?
both can work,
one with an RTC is more precise, especially the DS3231 (do not forget the battery)
Here's a sketch I use for testing time tracking.
Set the time a little ahead of actual time and download. Open the serial monitor a couple seconds before your pre-set time.
unsigned long currentmicros = 0;
unsigned long nextmicros = 0;
unsigned long interval = 10000; // adjusted for my board
int years = 2013;
int days = 334;
byte tens_hours = 2; // set to Friday 11/29/13, 9:04 pm east coast US
byte ones_hours = 3;
byte tens_minutes = 0;
byte ones_minutes = 9;
byte tens_seconds = 4;
byte ones_seconds = 0;
byte tenths = 0;
byte hundredths= 0;
byte prior_seconds = 0;
void setup()
{
Serial.begin(115200);
nextmicros = micros();
}
void loop()
{
currentmicros = micros(); // read the time.
if ((currentmicros - nextmicros) >= interval) // 10 milliseconds have gone by
{
hundredths = hundredths +1;
if (hundredths == 10){
hundredths = 0;
tenths = tenths +1;
}
if (tenths == 10){
tenths = 0;
ones_seconds = ones_seconds +1;
}
if (ones_seconds == 10){
ones_seconds = 0;
tens_seconds = tens_seconds +1;
}
if (tens_seconds == 6){
tens_seconds = 0;
ones_minutes = ones_minutes +1;
}
if (ones_minutes == 10){
ones_minutes = 0;
tens_minutes = tens_minutes +1;
}
if (tens_minutes == 6){
tens_minutes = 0;
ones_hours = ones_hours +1;
}
if (ones_hours == 10){
ones_hours = 0;
tens_hours = tens_hours +1;
}
if ((tens_hours == 2) && (ones_hours == 4)){
ones_hours = 0;
tens_hours = 0;
days = days +1;
if (days == 365){
years = years =1;
}
}
nextmicros = nextmicros + interval; // update for the next comparison
} // end time interval check
// counters are all updated now, send to display
if (prior_seconds != ones_seconds){
Serial.print ("Year: ");
Serial.print (years);
Serial.print (" Day of the year: ");
Serial.print (days);
Serial.print (" Time: ");
Serial.print (tens_hours, DEC);
Serial.print (" ");
Serial.print (ones_hours, DEC);
Serial.print (" : ");
Serial.print (tens_minutes, DEC);
Serial.print (" ");
Serial.print (ones_minutes, DEC);
Serial.print (" : ");
Serial.print (tens_seconds, DEC);
Serial.print (" ");
Serial.println (ones_seconds, DEC);
prior_seconds = ones_seconds; // show time update once/second
} // end one second passing check
// do other stuff in the meantime ...
} // end void loop