Converting millis to Sec or Hours for Uptime Display

Hello
Sorry for the basic question but wondering what the simplest way to change the display of milliseconds to minutes or hours would be.

Currently using the following to track hardware uptime
int value = millis() / 1000;
Blynk.virtualWrite(V4, value);

That displays milliseconds but its a crazy number and hard to digest visually.
id prefer to see uptime in hours & minutes if possible

How would i go about converting the milliseconds to minutes ? or hours? or both?
int value = (millis() / 1000) / 60 / 60 ; // icantzmath
Blynk.virtualWrite(V4, value);
or
int value = millis() / 1000 / 60 / 60 ; // icantzmath
Blynk.virtualWrite(V4, value);

i know in python i can use regular math to achieve results but not sure about c++
i tried searching but was unable to find.

It’s just the same with C++.

that displays seconds, actually…

you would better search for this time conversion topic on coding forums, like stackoverflow, i’m pretty sure there are dozens of ready made solutions for this.

G :eyes:GLE

https://www.google.ca/search?q=change+milliseconds+to+minutes+and+hours+in+C%2B%2B

Try this:

#include <TimeLib.h>

void TimeShowFormatted(int ms) {
  long days = 0;
  long hours = 0;
  long mins = 0;
  long secs = 0;
  String secs_o = ":";
  String mins_o = ":";
  String hours_o = ":";
  secs = ms / 1000; // set the seconds remaining
  mins = secs / 60; //convert seconds to minutes
  hours = mins / 60; //convert minutes to hours
  days = hours / 24; //convert hours to days
  secs = secs - (mins * 60); //subtract the coverted seconds to minutes in order to display 59 secs max
  mins = mins - (hours * 60); //subtract the coverted minutes to hours in order to display 59 minutes max
  hours = hours - (days * 24); //subtract the coverted hours to days in order to display 23 hours max
  if (secs < 10) {
    secs_o = ":0";
  }
  if (mins < 10) {
    mins_o = ":0";
  }
  if (hours < 10) {
    hours_o = ":0";
  }
  return days + hours_o + hours + mins_o + mins + secs_o + secs;

}

Usage is:

Blynk.virtualWrite(V4, TimeShowFormatted(millis()));
3 Likes

@Gunner @wanek … thanks for the input… as i mentioned i did search google & blynk community but was unable to understand how to expand millisec to hours, minutes secs… which is why i posted in HELP section.

is there a section for total noobs who dont know how to code?

I use Blynk mainly because i dont have to code and because its a quick way to get from idea to reality =)

@jamin Thank you kind sir… exactly what i was looking for.

Yes… but I am guessing you will not like the answer :stuck_out_tongue_winking_eye: Google for programming how-to websites.

That is true, it can be used that way… but it is also very limited to what Blynk is actually capable of. Trust me, I was the same way… but a little (OK lot of :stuck_out_tongue_winking_eye: ) reading and learning later and I can do MUCH more than twiddle a few controls and blink a few LED’s.

1 Like

@jamin sorry for multiple posts but im getting an error

 error: a function-definition is not allowed here before '{' token
   void TimeShowFormatted(int ms) {
   : error: expected '}' at end of input
 }
BLYNK_CONNECTED() {
  // Request Blynk server to re-send latest values for all pins
  Blynk.syncAll();
  void TimeShowFormatted(int ms) {
  long days = 0;
  long hours = 0;
  long mins = 0;
  long secs = 0;
  String secs_o = ":";
  String mins_o = ":";
  String hours_o = ":";
  secs = ms / 1000; // set the seconds remaining
  mins = secs / 60; //convert seconds to minutes
  hours = mins / 60; //convert minutes to hours
  days = hours / 24; //convert hours to days
  secs = secs - (mins * 60); //subtract the coverted seconds to minutes in order to display 59 secs max
  mins = mins - (hours * 60); //subtract the coverted minutes to hours in order to display 59 minutes max
  hours = hours - (days * 24); //subtract the coverted hours to days in order to display 23 hours max
  if (secs < 10) {
    secs_o = ":0";
  }
  if (mins < 10) {
    mins_o = ":0";
  }
  if (hours < 10) {
    hours_o = ":0";
  }
  return days + hours_o + hours + mins_o + mins + secs_o + secs;
}
}
Blynk.virtualWrite(V4, TimeShowFormatted(millis()));
}

what am i doing wrong?

Format your code correctly so it makes sense… i cant read any of that.

```cpp
// CODE HERE
```

but looks like you just need to remove the last bracket

@jamin thanks… that formatting job was terrible. its fixed now.
I tried removing the last bracket after Blynk.virtualWrite(V4, TimeShowFormatted(millis()));
but still getting that error

BLYNK_CONNECTED() {
  // Request Blynk server to re-send latest values for all pins
  Blynk.syncAll();
  void TimeShowFormatted(int ms) {
  long days = 0;
  long hours = 0;
  long mins = 0;
  long secs = 0;
  String secs_o = ":";
  String mins_o = ":";
  String hours_o = ":";
  secs = ms / 1000; // set the seconds remaining
  mins = secs / 60; //convert seconds to minutes
  hours = mins / 60; //convert minutes to hours
  days = hours / 24; //convert hours to days
  secs = secs - (mins * 60); //subtract the coverted seconds to minutes in order to display 59 secs max
  mins = mins - (hours * 60); //subtract the coverted minutes to hours in order to display 59 minutes max
  hours = hours - (days * 24); //subtract the coverted hours to days in order to display 23 hours max
  if (secs < 10) {
    secs_o = ":0";
  }
  if (mins < 10) {
    mins_o = ":0";
  }
  if (hours < 10) {
    hours_o = ":0";
  }
  return days + hours_o + hours + mins_o + mins + secs_o + secs;
}
}
Blynk.virtualWrite(V4, TimeShowFormatted(millis()));
}

This is not how to use this command… It is not a function in of itself (e.g. surrounded in the brackets)… but it does need to be put into a proper function (e.g. void DoSomethingHere() ) in order to run properly.

This is where all that pesky programming need to know kicks in… :wink:

Check out some of the examples in the Sketch Builder to see how these commands work.

@Gunner
i did try that but get the same error

 In function 'void BlynkOnConnected()':
v2v3:26: error: a function-definition is not allowed here before '{' token
   void TimeShowFormatted(int ms) {          
: error: expected '}' at end of input
 }  
BLYNK_CONNECTED() {
  // Request Blynk server to re-send latest values for all pins
  Blynk.syncAll();
  void TimeShowFormatted(int ms) {
  long days = 0;
  long hours = 0;
  long mins = 0;
  long secs = 0;
  String secs_o = ":";
  String mins_o = ":";
  String hours_o = ":";
  secs = ms / 1000; // set the seconds remaining
  mins = secs / 60; //convert seconds to minutes
  hours = mins / 60; //convert minutes to hours
  days = hours / 24; //convert hours to days
  secs = secs - (mins * 60); //subtract the coverted seconds to minutes in order to display 59 secs max
  mins = mins - (hours * 60); //subtract the coverted minutes to hours in order to display 59 minutes max
  hours = hours - (days * 24); //subtract the coverted hours to days in order to display 23 hours max
  if (secs < 10) {
    secs_o = ":0";
  }
  if (mins < 10) {
    mins_o = ":0";
  }
  if (hours < 10) {
    hours_o = ":0";
  }
  return days + hours_o + hours + mins_o + mins + secs_o + secs;
}
Blynk.virtualWrite(V4, TimeShowFormatted(millis()));
}

If these three lines are in your code… and it looks like all you did was copy/paste @Jamin’s code(s) but lumped them all together… then that’s your problem. This is one reason we don’t encourage just asking for code, or offering code to those who don’t want to learn code… because if you don’t know how to properly set it up, it is useless…

Take a look at those links I showed you, figure out how you want to display that time via that Blynk.virtualWrite(V4, TimeShowFormatted(millis())); command and create a proper void function for it (preferably using a timer else you may flood your program).

I don’t know what hardware you are using, but look at this ESP8266 example and see how the void myTimerEvent() is setup…

PS no need to keep tossing @Jamin’s code examples back at us… we already see it above :slight_smile:

BLYNK_CONNECTED() {
  // Request Blynk server to re-send latest values for all pins
  Blynk.syncAll();
  Blynk.virtualWrite(V4, TimeShowFormatted(millis()));
}

void TimeShowFormatted(int ms) {
  long days = 0;
  long hours = 0;
  long mins = 0;
  long secs = 0;
  String secs_o = ":";
  String mins_o = ":";
  String hours_o = ":";
  secs = ms / 1000; // set the seconds remaining
  mins = secs / 60; //convert seconds to minutes
  hours = mins / 60; //convert minutes to hours
  days = hours / 24; //convert hours to days
  secs = secs - (mins * 60); //subtract the coverted seconds to minutes in order to display 59 secs max
  mins = mins - (hours * 60); //subtract the coverted minutes to hours in order to display 59 minutes max
  hours = hours - (days * 24); //subtract the coverted hours to days in order to display 23 hours max
  if (secs < 10) {
    secs_o = ":0";
  }
  if (mins < 10) {
    mins_o = ":0";
  }
  if (hours < 10) {
    hours_o = ":0";
  }
  return days + hours_o + hours + mins_o + mins + secs_o + secs;
}
3 Likes