I know I've been asking a lot of questions recently. Please accept my apology, I'm new to Arduino and have basic knowledge of coding.
I'm using a - PCF85063A RTC clock- And I have found one library that works well with my chip. https://github.com/jvsalo/pcf85063a
I want to edit this library so that instead of typing Unix time in the code every time you program it, you can use COM port to type in the Unix time and use that as the initial value for counting.
I used the code from below to get strings from COM port and print them. : https://www.norwegiancreations.com/2017/12/arduino-tutorial-serial-inputs/
I modified it and placed it in the RTC library but since the values don't match, it doesn't compile. One is String and the other is long unsigned int. The error is: cannot convert 'String' to 'time_t {aka long unsigned int}' for argument '1' to 'void breakTime(time_t, tmElements_t&)'
Here is the code that I have:
/*
Demonstrates how to set and get time from the RTC.
The type tmElements_t and functions like breakTime() are from
the Time library.
*/
#include "PCF85063A.h"
PCF85063A rtc;
String my_time; //TIME Value to get from user
void setup() {
tmElements_t tm;
Serial.begin(115200);
Serial.println("What time is UNIX time?");
Serial.println(String("Resetting RTC, status: ") + rtc.reset());
if(Serial.available()){
my_time = Serial.readStringUntil('\n');
Serial.println("Your Unix time is " + my_time + "!");
}
/* Convert Unix timestamp to tmElements_t */
breakTime(my_time, tm);
rtc.time_set(&tm);
while (!Serial)
}
/* Print human readable time */
void print_time(tmElements_t t) {
Serial.print(String(" ") + (1970 + t.Year));
Serial.print(String(" / ") + t.Month);
Serial.print(String(" / ") + t.Day);
//Serial.print(String(" Wday ") + t.Wday);
Serial.print(String(" ") + t.Hour);
Serial.print(String(" : ") + t.Minute);
Serial.print(String(" : ") + t.Second);
Serial.print("\n");
}
void loop() {
tmElements_t t;
/* Get and print current time */
rtc.time_get(&t);
print_time(t);
/* Print Unix timestamp */
// Serial.print("Unix time: ");
// Serial.println(makeTime(t));
delay(125);
}
I tried sprintf to convert values but no success. Also, I could convert it to integer but that won't work because Unix time is 10 digits while byte only supports up to 32768.
What I did the first time, read the Epoch time (which you can get online from several web sites) into a char array, and then convert to a 32 bit value using strtoul(). Then used that to set the RTC. By the way, 'int' supports up to 32767, byte up to 255. 32768 becomes a negative number if it's in an 'int'.
aarg:
What I did the first time, read the Epoch time (which you can get online from several web sites) into a char array, and then convert to a 32 bit value using strtoul(). Then used that to set the RTC. By the way, 'int' supports up to 32767, byte up to 255. 32768 becomes a negative number if it's in an 'int'.
Thanks but will it work with this code? Because the value taken from the user needs to be String.
String my_time; //TIME Value to get from user
if(Serial.available()){
my_time = Serial.readStringUntil('\n');
Serial.println("Your Unix time is " + my_time + "!");
}
I think that you can read the user input directly into a long int with Serial.parseInt().
Then pass that value directly into breakTime().
long my_time; //TIME Value to get from user
if(Serial.available()){
my_time = Serial.parseInt();
Serial.println("Your Unix time is " + my_time + "!");
}
/* Convert Unix timestamp to tmElements_t */
breakTime(my_time, tm);
rtc.time_set(&tm);
Thanks for the reply,
Yes, I just tried it. this code can run well independently.
But when I put it in my main code, code misbehaves. two things happens:
If I put the following lines in the loop :
if(Serial.available()){
my_time = Serial.parseInt();
Serial.println("Your Unix time is " + my_time);
Serial.println(my_time);
}
tmElements_t tm;
breakTime(my_time,tm );
rtc.time_set(&tm);
It only types the initial value without changing the "seconds" and keeps printing the same initial time.
If I write the following code,
#include "PCF85063A.h"
PCF85063A rtc;
long my_time; //TIME Value to get from user
void setup() {
Serial.begin(115200);
while (!Serial)
if(Serial.available()){
my_time = Serial.parseInt();
Serial.println("Your Unix time is " + my_time);
Serial.println(my_time);
}
tmElements_t tm;
breakTime(my_time,tm );
rtc.time_set(&tm);
}
/* Print human readable time */
void print_time(tmElements_t t) {
Serial.print(String(" ") + (1970 + t.Year));
Serial.print(String(" / ") + t.Month);
Serial.print(String(" / ") + t.Day);
//Serial.print(String(" Wday ") + t.Wday);
Serial.print(String(" ") + t.Hour);
Serial.print(String(" : ") + t.Minute);
Serial.print(String(" : ") + t.Second);
Serial.print("\n");
}
void loop() {
if(Serial.available()){
my_time = Serial.parseInt();
Serial.println("Your Unix time is " + my_time);
Serial.println(my_time);
}
tmElements_t t;
/* Get and print current time */
rtc.time_get(&t);
print_time(t);
delay(125);
}
The COM port gets the value but doesn't use it to set the clock with. And it will continue counting the old time.
Alright guys, I fixed it. looks like the code that takes time has to be in the same function as the one that takes values from the user. It was not working because it was outside. I'll leave the code here for anyone who wants to use this code in their projects. Thanks to the guys who replied. Use it with the same library that I gave in my first post. This code only works with "PCF85063A" RTC chip.
#include "PCF85063A.h"
PCF85063A rtc;
long my_time; //TIME Value to get from user
void setup() {
Serial.begin(115200);
while (!Serial)
if(Serial.available()){
my_time = Serial.parseInt();
Serial.println("Your Unix time is " + my_time);
Serial.println(my_time);
}
}
/* Print human readable time */
void print_time(tmElements_t t) {
Serial.print(String(" ") + (1970 + t.Year));
Serial.print(String(" / ") + t.Month);
Serial.print(String(" / ") + t.Day);
//Serial.print(String(" Wday ") + t.Wday);
Serial.print(String(" ") + t.Hour);
Serial.print(String(" : ") + t.Minute);
Serial.print(String(" : ") + t.Second);
Serial.print("\n");
}
void loop() {
if(Serial.available()){
my_time = Serial.parseInt();
Serial.println("Your Unix time is " + my_time);
Serial.println(my_time);
tmElements_t tm;
breakTime(my_time,tm );
rtc.time_set(&tm);
}
tmElements_t t;
/* Get and print current time */
rtc.time_get(&t);
print_time(t);
delay(125);
}
Alright guys, I fixed it. looks like the code that takes time has to be in the same function as the one that takes values from the user. It was not working because it was outside.
Yes, the variables inside a function are not available outside that function. The technical term for it is "scope". It's a blessing because it prevents variable names from colliding, and unintentional modification of a variable by some generally unrelated part of the program.