#include <Arduino.h>
#include <sys/time.h>



//Time
  unsigned long serialReadStart;

  long timeAtLast;
  long timeSinceLast;

  bool readingSerial = false;
  

void ShowTime(unsigned long s, unsigned long m, unsigned long h) {
  //Serial.println("Uptime"); 
  //Serial.println("(h:m:s)"); 
  //Serial.print(hours);
  //Serial.print(":"); 
  //Serial.print(minutes); 
  //Serial.print(":"); 
  //Serial.println(seconds);
  Serial.print("Uptime"); Serial.println("(hh:mm:ss) "); 
  Serial.print("Hours: ");   Serial.println(h);
  Serial.print("Minutes: "); Serial.println(m);
  Serial.print("Seconds: "); Serial.println(s);
  
}
inline double Time() {
  timeval tv = {0};
  gettimeofday(&tv, nullptr);
  return (double)(tv.tv_usec / 1000000.0 + (double) tv.tv_sec);
  }

unsigned long GetTime(unsigned long &hours, unsigned long &minutes, unsigned long &seconds) {
  double currentTime = Time(); 
    
    if(currentTime > (float)60) {        
      seconds = currentTime; 
      minutes = seconds/60;
      seconds = currentTime - (minutes * 60);
        if(minutes > 60) {  
           hours = minutes / 60; 
           minutes = minutes - (hours * 60);
  
          }
        else { }
    }
    else {
      seconds = currentTime;
      minutes = 0;
      hours = 0;
    }
    return currentTime;
 }
void setup() {
  //Serial.setTimeout(3000);
    Serial.begin(115200);
    delay(500);
    Serial.println("Starting Serial Communications");

  timeAtLast = 0;
  }
  

void loop() { 

  float rightNow;
  static unsigned long hours;
  static unsigned long minutes;
  static unsigned long seconds;
  
    rightNow = GetTime(hours, minutes, seconds);
   
    //Serial.println(rightNow);

  if(int(rightNow) - int(5) >= timeAtLast) { 
    ShowTime(seconds, minutes, hours);
    timeAtLast = rightNow;
  }
    
   
  delay(10); // this speeds up the simulation
 
}