Hi there,
I am trying writing a basic function but for some reason the function is not executed and I can't figured out why. I wrote other simple functions before so I am :o . I may feel very dumb after the first reply, I know (don't troll me I just restarted some programing after stopping for 35 years). I can only see the loop printed messages but nothing from the called function. There is probably many issues in my program, just help me with this non executed function, the rest is my problem banging my old brain against C++.
Thanks!
// Convert UNIX time to human readable date and time
// Author: GA
#include <TimeLib.h>
long timeNow = 1552456800;
long timeLater = 1552467600;
static String convResult[4];
void setup(){
Serial.begin(115200);
while(!Serial) continue;
}
void loop(){
if (Serial.read() == 'j') {
Serial.println("Starting conversion 0");
void dateTimeconv(String(timeNow), String(timeLater));
Serial.println("Display result");
Serial.println("Now Date: "+convResult[0]);
Serial.println("Now time: "+convResult[1]);
Serial.println("Later Date: "+convResult[2]);
Serial.println("Later time: "+convResult[3]);
}
}
void dateTimeconv(){
Serial.println("Starting date and time conversion");
Serial.println("Conversion now");
int minuteNow = minute(timeNow);
String timeNowH = (String(hour(timeNow))+":"+printDigits(minuteNow));
String dateNowH = (String(day(timeNow))+"-"+(String(monthStr(timeNow)))+"-"+(String(year(timeNow))));
Serial.println("Conversion later");
int minuteLater = minute(timeLater);
String timeLaterH = (String(hour(timeLater)+":"+printDigits(minuteLater)));
String dateLaterH = (String(day(timeLater))+"-"+(String(monthStr(timeLater)))+"-"+(String(year(timeLater))));
Serial.println("Modif the array");
convResult[0]=dateNowH;
convResult[1]=timeNowH;
convResult[2]=dateLaterH;
convResult[3]=timeLaterH;
}
int printDigits(int digits) {
// utility function if json time stamp returns with 0n or 00 minutes
if (digits >= 0 && digits < 10){
digits='0'+digits;
}
else {
digits='0'+'0';
}
return(digits);
}
When you call a function you don't specify the return type (void) again, that's just in the function declaration.
Also in your main declaration of void dateTimeconv() you say it takes no parameters. But when you try to call it in loop() you try to pass two parameters to it - String(timeNow), String(timeLater). That's not going to work.
Thanks for you replies. I remove the "void" calling the function and in the main declaration I used String dateTimeconv() and now I get Error: too many arguments to function 'String dateTimeconv()'
The parameters are the bits in brackets after the function name. The return type is the thing before the name. String dateTimeconv() means it takes NO parameters but will RETURN a String.
The parameters are the bits in brackets after the function name. The return type is the thing before the name. String dateTimeconv() means it takes NO parameters but will RETURN a String.
Steve
Thanks Steve
After some reading I modified my sketch. No complilation error, it is still not running but strangely I get different results depending on the Ardunio board I am using. On a UNO I can see only the "Starting conversion 0" message on the serial console but nothing from my function. On a MKR 101 "Starting conversion 0" from the loop(), and "Starting date and time conversion", "Conversion now" from my function but it seems that not all the function is executed. Code below. I commented large part of the function to insure that I am not messing in some way with the board performance.
demkat1 thank for your reply. It is just a test I need to learn and understand how to pass parameters to the function because this is how I will used it.
// Convert UNIX time to human readable date and time
// Author: GA
#include <TimeLib.h>
long timeNow = 1552456800;
long timeLater = 1552467600;
static String convResult[4];
void setup(){
Serial.begin(115200);
while(!Serial) continue;
}
void loop(){
if (Serial.read() == 'j') {
Serial.println("Starting conversion 0");
dateTimeconv();
Serial.println("Display result");
Serial.println("Now Date: "+convResult[0]);
/* Serial.println("Now time: "+convResult[1]);
Serial.println("Later Date: "+convResult[2]);
Serial.println("Later time: "+convResult[3]); */
}
}
void dateTimeconv(){
Serial.println("Starting date and time conversion");
Serial.println("Conversion now");
// int minuteNow = minute(timeNow);
// String timeNowH = (String(hour(timeNow))+":"+printDigits(minuteNow));
String dateNowH = (String(day(timeNow))+"-"+(String(monthStr(timeNow)))+"-"+(String(year(timeNow))));
/* Serial.println("Conversion later");
int minuteLater = minute(timeLater);
String timeLaterH = (String(hour(timeLater)+":"+printDigits(minuteLater)));
String dateLaterH = (String(day(timeLater))+"-"+(String(monthStr(timeLater)))+"-"+(String(year(timeLater)))); */
Serial.println("Modif the array");
convResult[0]=dateNowH;
/* convResult[1]=timeNowH;
convResult[2]=dateLaterH;
convResult[3]=timeLaterH;
*/
}
int printDigits(int digits) {
// utility function if json time stamp returns with 0n or 00 minutes
if (digits >= 0 && digits < 10){
digits='0'+digits;
}
else {
digits='0'+'0';
}
return(digits);
}
UKHeliBob:
Are you using Serial to both receive data and to print it to the Serial monitor ?
No, I am only using Serial to print the message out. The Unix time string will come from a json data and some other parameters from a IC2 Adafruit board. These parts are already working, I just need to do the Unix time to Human readable format conversion. Even the Unix time conversion is working in the way as show below. But it is a learning exercise, I want something cleaner and more structured, so a function, and will display the data on a Web page not on the Serial Monitor.
Thanks
Serial.print (F("Time & Date current: "));
Serial.print("\t"+String(hour(timeNow))+":");
int minuteNow = minute(timeNow);
printDigits(minuteNow);
Serial.println("\t"+(String(day(timeNow))+"-"+monthStr(month(timeNow))+"-"+String(year(timeNow))));
Serial.print("Time & Date prevision: ");
Serial.print("\t"+String(hour(timeLater))+":");
int minuteLater = minute(timeLater);
printDigits(minuteLater);
Serial.print("\t"+(String(day(timeLater))+"-"+monthStr(month(timeLater))+"-"+String(year(timeLater))));
Finally running, I also found than my Arduino IDE was somewhere corrupt explaining abnormal incomplete Serial console output than made me really confused thinking my function was not working. I reinstalled the IDE but this time from the Arduino site not from the th Windows Store. Working code below. Thanks for your help.
// Convert UNIX time to human readable date and time
// Author: GA
#include <TimeLib.h>
long timeNow = 1552975704;
long timeLater = 1552467611;
static String convResult[4];
void setup(){
Serial.begin(115200);
while(!Serial) continue;
}
void loop(){
if (Serial.read() == 'j') {
dateTimeconv();
Serial.println("Now Date: "+convResult[0]);
Serial.println("Now time: "+convResult[1]);
Serial.println("Later Date: "+convResult[2]);
Serial.println("Later time: "+convResult[3]);
}
}
void dateTimeconv()
// Translate UNIX timestamp in human readable format
{
String minuteNow = String(minute(timeNow));
String minuteLater = String(minute(timeLater));
String timeNowH = String(hour(timeNow))+":"+String(printDigits(minuteNow));
String dateNowH = String(day(timeNow))+"-"+monthStr(month(timeNow))+"-"+String(year(timeNow));
String timeLaterH = String(hour(timeLater))+":"+String(printDigits(minuteLater));
String dateLaterH = String(day(timeLater))+"-"+monthStr(month(timeLater))+"-"+String(year(timeLater));
convResult[0]=dateNowH;
convResult[1]=timeNowH;
convResult[2]=dateLaterH;
convResult[3]=timeLaterH;
}
String printDigits(String digits)
// Adding leading 0 to time minutes
{
if (digits == "0"){ // If the minute() function returned 0 minutes
digits = "00";
}
if (digits.length() == 1){ // If the minute() function returned more that 0 but less than 10 minutes
digits="0"+digits;
}
return digits;
}