Finding IRL time through computer

I currently have some code that lets me input the seconds, minutes, and hours into a function which will then change the values respectively on a clock i built. My question is if there is any way to easily synch this with real time from my computer or something. this is the current code I have which will loop through all the seconds from midnight to midnight.

Edit: I have the computer connected through the USB that you use to Upload code.

void loop()
{ 
 for(int i=0; i <=86400; i++){
 int sec = i %60;
 int min = i/60 %60;
 int hr = i/60 /60 %60;
 Time_Refresh(sec,min,hr);
 delay(1000);
 }

how does the arduino communicate with your computer or network?

just through the USB that you use to upload code, is this enough or do i need something else to do it? if so what would that be?

Just buy a real time clock module, You will find the current code will not be accurate.

yea i know that this wont be accurate forever, it is more to test if everything works. a problem with using a clock module is that I have run out of pins to use. is there a way to add extra pins?
I am using an arduino Uno and have used pins 0-13 and A0-A5

You can use external shift registers to get yourself far more pins for your LEDs, leaving some spare for a clock.

alright thank you so much!
and just to clarify using the clock module i would be able to somehow extract the seconds, minutes and hours in different integers correct?

Have the PC send HH:MM:SS.

In the sketch:

const unsigned long SECOND = 1000;
const unsigned long MINUTE = 60 * SECOND;
const unsigned long HOUR = 60 * MINUTE;
const unsigned long DAY = 24 * HOUR;

unsigned long LastDayStart = 0;

void loop()
{
  unsigned long TimeOfDay = millis() - LastDayStart;

  int sec = (TimeOfDay / SECOND) % MINUTE;
  int min = (TimeOfDay / MINUTE) % HOUR;
  int hr = (TimeOfDay / HOUR) % DAY;
  Time_Refresh(sec, min, hr);

  if (Serial.available())
  {
    hr = Serial.parseInt();
    min = Serial.parseInt();
    sec = Serial.parseInt();

    TimeOfDay = hr * HOUR + min * MINUTE + sec * SECOND;
    LastDayStart = millis() - TimeOfDay;

    // Clear out any line ending characters
    delay(100);
    while (Serial.available())
    {
      Serial.read();
      delay(100);
    }
  }

  delay(1000);
}

If you don't want to use millis():

const unsigned long SECOND = 1;
const unsigned long MINUTE = 60 * SECOND;
const unsigned long HOUR = 60 * MINUTE;
const unsigned long DAY = 24 * HOUR;

void loop()
{
  static unsigned long TimeOfDay = 0;

  int sec = (TimeOfDay / SECOND) % MINUTE;
  int min = (TimeOfDay / MINUTE) % HOUR;
  int hr = (TimeOfDay / HOUR) % DAY;
  
  Time_Refresh(sec, min, hr);

  if (Serial.available())
  {
    hr = Serial.parseInt();
    min = Serial.parseInt();
    sec = Serial.parseInt();

    TimeOfDay = hr * HOUR + min * MINUTE + sec * SECOND;

    // Clear out any line ending characters
    delay(100);
    while (Serial.available())
    {
      Serial.read();
      delay(100);
    }
  }

  delay(1000);
  TimeOfDay = TimeOfDay + SECOND;
}

An int can only go up to 32767. Try using a long instead.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.