0

Looking at this Time Arduino library because I would like to send time stamps with data across my HC05-PC channel.

https://playground.arduino.cc/Code/Time

I wrote this function to get the time at any point

String getTime() {
    String timeSecond = String(second());
    String timeMinute = String(minute());
    String timeHour = String(hour());
    String timeDay = String(day());
    String timeMonth = String(month());
    String timeYear = String(year());

    String now = timeHour + ":" + timeMinute + ":" + timeSecond + " " + 
        timeMonth + "/" + timeDay + "/" + timeYear;

    return now;
}

It seems in the docs he provides info on how to set the default time on a Linux/Unix system so that this library will always be grabbing the correct time by editing some bash files.

When I run the function, having been compiled on Windows, it begins running on 1/1/1970 0:0:0.

He does not provide any documentation for setting the Windows time stamp. How would I go about doing that?

5
  • You probably need to show us the code where you receive/set the time. – Johnny Mopp Sep 14 '17 at 17:56
  • I don't do an original set. Do you need extra hardware and another library to do that? – Jack Frye Sep 14 '17 at 17:58
  • AFAIK, no Arduino has an internal clock. So you need to set the library time manually. On the page you link to search for "processSyncMessage". If you want to disconnect your Arduino from the PC after setting the time, I recommend you get a Real Time Clock module. – Johnny Mopp Sep 14 '17 at 18:11
  • In that case, I'd have to reset the time before every upload/run? – Jack Frye Sep 14 '17 at 18:12
  • That's what the example shell script does - sends the PC time to the Arduino. – Johnny Mopp Sep 14 '17 at 18:25
1

The Adafruit RTClib has a function to use the compiler date and time. You use the TimeLib maintained by Paul Stoffregen. That library does not have such a function, but you can write it yourself. For example with sscanf and setTime. Without external RTC that will only work if you upload a sketch and keep the Arduino always on. I have a clock like that, and since that clock is always on, I never had the need to add a RTC chip. When the Arduino resets, it returns to the date and time when it was compiled.

If you use a Arduino Due or Zero or M0 then they have their own rtc library.

If you want the computer to send the date and time, then you could make a command and send that via the serial port or via Bluetooth.

What kind of date and time do you want ? The local time, including the offset of the Daylight Saving Time ? That is possible, but not easy to make on a Arduino with RTC chip, in that case it is easier to send a command from the computer with the newest date and time.

It that function okay ? The object 'now' disappeares when the function ends, and yet you return that object.

1
  • My issue is that is have this channel between the arduino hc05 chip and my computer. It seems that because only one bit can be transmitted at a time there is a delay in the data being sent from one side to the other. I want to use exact datetime to test this theory but it seems like it is more trouble than it is worth. (I am not going to keep and arduino attached to my laptop 24/7) or buy additional hardware. Running a command line command is a fair option but even that is too extensive for the purposes of the application. – Jack Frye Sep 14 '17 at 18:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.