loading my p.c time and date into the arduino during the programing

I am tring to create a clock with my arduino. I want the arduino to be loaded with my P.C system houres minutes and date while I am programing it.
I am fimilir with the time.h library, and a tried to initialize some some integers with it in the setup function, this way:

#include <time.h>
time_t t = time(NULL);
struct tm tm = *localtime(&t);

void setup() {

// set time

h=tm.tm_hour;
m=tm.tm_min;
s=tm.tm_sec;

}

however, the integer wasnt initialized with the desired values.
can you please advice me hou can I do it right (this way, or even with other libraries?

I want the arduino to be loaded with my P.C system houres minutes and date while I am programing it.

Please clarify. how is the PC system time supposed to get to the code? dynamically ? or just by using #defines which captures last compile time (using __DATE__ and __TIME__ is the typical method used in RTC libraries)

i need just by using #defines which captures last compile time. can you give me please some example how can i extract from the DATE 3 integer that i can load into the arduino?

use the code above to set the RTC

DateTime now = rtc.now();

// set time
DateTime now = rtc.now();
  h=(now.hour();
  m=now.minutes() ;
  s=now.seconds() ;{

your RTC library may use different commands. this is a general scheme, not proven & tested code

if you are not using an RTC, it is not a clock

Perhaps this example will demonstrate the use of DATE _TIME macros to set the system clock.

//Software Clock: set to compile/upload time
//with power cycle, reset, or new serial monitor goes to original compile time
//AVR Macro Strings converted to tmElements_t using functions
//getDate() and getTime() functions taken from ds1307 library
#include <TimeLib.h>
tmElements_t tm;
const char *monthName[12] = {
  "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};

void setup(){
  Serial.begin (115200);
  Serial.println(__DATE__);
  Serial.println(__TIME__);
  if (getDate(__DATE__) && getTime(__TIME__)) {
    Serial.println("AVR Macro strings converted to tmElements.");
  }
  setTime(makeTime(tm));//set Ardino system clock to compiled time
    Serial.println("System millis clock referenced to tmElements.");
    Serial.println();
} 
void loop() {
  Serial.print("Ok, Time = ");
  digitalClockDisplay();
  delay(2000);
}

bool getTime(const char *str)
{
  int Hour, Min, Sec;

  if (sscanf(str, "%d:%d:%d", &Hour, &Min, &Sec) != 3) return false;
  tm.Hour = Hour;
  tm.Minute = Min;
  tm.Second = Sec;
  return true;
}

bool getDate(const char *str)
{
  char Month[12];
  int Day, Year;
  uint8_t monthIndex;

  if (sscanf(str, "%s %d %d", Month, &Day, &Year) != 3) return false;
  for (monthIndex = 0; monthIndex < 12; monthIndex++) {
    if (strcmp(Month, monthName[monthIndex]) == 0) break;
  }
  if (monthIndex >= 12) return false;
  tm.Day = Day;
  tm.Month = monthIndex + 1;
  tm.Year = CalendarYrToTm(Year);
  return true;
}

void digitalClockDisplay(){
  // digital clock display of the time
  Serial.print(hour());
  printDigits(minute());
  printDigits(second());
  Serial.print(" ");
  Serial.print(month());
  Serial.print('/');
  Serial.print(day());
  Serial.print('/');
  Serial.print(year()); 
  Serial.println(); 
}

void printDigits(int digits){
  // utility function for digital clock display: prints preceding colon and leading 0
  Serial.print(":");
  if(digits < 10)
    Serial.print('0');
  Serial.print(digits);
}

thank you for the help,
but, do i have to be useing a RTC device conneced to the arduino in order to use your code?
also, i cant find the way to add the <TimeLib.h> libraay (with <time.h> i dont have any problam...)

but, do i have to be useing a RTC device conneced to the arduino in order to use your code?

if you don't want to start from 0 every time you restart or power up: yes

do i have to be useing a RTC device conneced to the arduino in order to use your code?

No.

i cant find the way to add the <TimeLib.h> library

Why are you trying to use a software clock? @Greek Emeritus has pointed out the issues with it.

The AVR macros TIME and DATE make compile time available to you.

If you Arduino is always connected to a PC, there are ways to write code which runs on the PC and will send the time to the Arduino when asked. I'd rather use an RTC.

Try this

void setup(){
  Serial.begin (115200);
  Serial.println(F(__DATE__));
  Serial.println(F(__TIME__));
}

void loop() {}

evyad:
I am tring to create a clock with my arduino. I want the arduino to be loaded with my P.C system houres minutes and date while I am programing it.

The time when a program is compiled has no relevance whatever to the use of an Arduino as a clock.

It would be like the Goon Show script in which the idiot had the time written on a piece of paper.

...R

Robin2:
The time when a program is compiled has no relevance whatever to the use of an Arduino as a clock.

well in practical terms though, if you compile and upload right away through the IDE, that lets you set the current time with a few seconds approximation. then your arduino needs to do the right thing with the info.

J-M-L:
well in practical terms though, if you compile and upload right away through the IDE, that lets you set the current time with a few seconds approximation. then your arduino needs to do the right thing with the info.

It seems to me a very unlikely scenario where the program is uploaded and then allowed to run continuously without ever being reset.

If the Arduino clock is to remain connected to the PC that uploads the program then why not periodically query a program on the PC to get the latest time.

...R

Robin2:
It seems to me a very unlikely scenario where the program is uploaded and then allowed to run continuously without ever being reset.

yes typical use case is you load it once to initialize the RTC with the "right time" then comment out the code and re-upload.

Robin2:
If the Arduino clock is to remain connected to the PC that uploads the program then why not periodically query a program on the PC to get the latest time.

in the grand scheme of things, clocks should stay the same
(but in theory there is no difference between theory and practice. in practice there is :slight_smile: )

probably better to add some sort of communication, may be if Wi-Fi and internet is available, then sync on a time server. that's probably also what the PC does

in the grand scheme of things, clocks should stay the same
(but in theory there is no difference between theory and practice. in practice there is :slight_smile: )

I use a Raspberry Pi as a workshop computer. I rarely take the cellphone back there, so it never syncs to nist.gov. the clock is essentially a random number generator

[quote author=Geek Emeritus link=msg=4139424 date=1555428783]the clock is essentially a random number generator[/quote] :slight_smile: :slight_smile: