No, not real time. I will be storing the number of hours when the fan I am monitoring turns on.
I want to display HH:MM:SS instead of DD:HH:MM:SS like 24:00:00 instead of 1:00:00:00.
Sorry I bothered you. I've been replacing time(millis()/1000) with time(86400000) to check for 24 hours I forgot the /1000.
This is what I used:
#include <DateTime.h>
/* Useful Constants */
#define SECS_PER_MIN (60UL)
#define SECS_PER_HOUR (3600UL)
/* Useful Macros for getting elapsed time */
#define numberOfSeconds(_time_) (_time_ % SECS_PER_MIN)
#define numberOfMinutes(_time_) ((_time_ / SECS_PER_MIN) % SECS_PER_MIN)
#define numberOfHours(_time_) (( _time_ / SECS_PER_HOUR) % SECS_PER_HOUR)
void setup(){
Serial.begin (9600);
}
void loop(){
time(millis()/1000);
delay(1000);
}
void time(long val){
int hours = numberOfHours(val);
int minutes = numberOfMinutes(val);
int seconds = numberOfSeconds(val);
// digital clock display of current time
Serial.print(hours,DEC);
printDigits(minutes);
printDigits(seconds);
Serial.println();
}
void printDigits(byte digits){
// utility function for digital clock display: prints colon and leading 0
Serial.print(":");
if(digits < 10)
Serial.print('0');
Serial.print(digits,DEC);
}
arduinoTime:
No, not real time. I will be storing the number of hours when the fan I am monitoring turns on.
I want to display HH:MM:SS instead of DD:HH:MM:SS like 24:00:00 instead of 1:00:00:00.
try something like this:
unsigned long oneSecond = 1000UL;
unsigned long startTime;
int mySeconds = 0;
int myMinutes = 0;
int myHours = 0;
int myDays = 0;
void setup()
{
Serial.begin(9600);
startTime = millis();
}
void loop()
{
if (millis() - startTime >= oneSecond)
{
displayTime();
mySeconds++;
startTime += oneSecond;
if (mySeconds > 59)
{
myMinutes++;
mySeconds = 0;
if (myMinutes > 59)
{
myHours++;
myMinutes=0;
if (myHours > 23)
{
myDays++;
myHours=0;
}
}
}
}
//your other code here.....
}
void displayTime()
{
//Serial.print(myDays);
//Serial.print(":");
if (myHours < 10) Serial.print("0");
Serial.print(myHours);
Serial.print(":");
if (myMinutes < 10) Serial.print("0");
Serial.print(myMinutes);
Serial.print(":");
if (mySeconds < 10) Serial.print("0");
Serial.println(mySeconds);
}
or do all the work in the updateDisplay() function if you like:
void loop()
{
if (millis() - startTime >= oneSecond)
{
displayTime();
}
//your other code here.....
}
void displayTime()
{
startTime += oneSecond;
if (mySeconds > 59)
{
myMinutes++;
mySeconds = 0;
if (myMinutes > 59)
{
myHours++;
myMinutes=0;
if (myHours > 23)
{
myDays++;
myHours=0;
}
}
}
//Serial.print(myDays);
//Serial.print(":");
if (myHours < 10) Serial.print("0");
Serial.print(myHours);
Serial.print(":");
if (myMinutes < 10) Serial.print("0");
Serial.print(myMinutes);
Serial.print(":");
if (mySeconds < 10) Serial.print("0");
Serial.println(mySeconds);
mySeconds++;
}