Accurate time keeping in the background

Hi all, I'm sure this question has been asked multiple times before but I can't seem to find where. All I want is to have a clock, on my Arduino Uno which will keep time while I do other tasks.

I have read about the library Time but the standalone version uses Serial. I don't want serial, I just want to print the frigging time to an LCD screen, read in some sensors, and do some calculations.

Is this possible?!?!

So, don't use Serial.

(What exactly is the "frigging" time?)

It's really easy to use a Real Time Clock like the DS1307 which uses the I2C protocol.

If you are content with a rough approximation of time passing you can use millis() but it does not keep time nearly as well as a Real Time Clock module.

...R

Use micros() to track time, the Arduino tracks the US time pretty well I have found.

unsigned long currentMicros;
unsigned long previousMicros;
unsigned long elapsedTime;

byte hundredths;
byte tenths;
byte secondsOnes;
byte oldSecondsOnes;
byte secondsTens;
byte minutesOnes = 0;
byte minutesTens = 4;
byte hoursOnes = 1;
byte hoursTens = 1;

void setup(){

  Serial.begin(115200); // make serial monitor match
  currentMicros = micros();
  previousMicros = currentMicros;
  Serial.println ("Setup Done");
}

void loop(){

  currentMicros = micros();

  // how long's it been?
  elapsedTime = currentMicros - previousMicros;
  Serial.print ("Elapsed: ");  
  Serial.println (elapsedTime);
  if ( elapsedTime >=10000UL){  // 0.01 second passed? Update the timers
    elapsedTime = 0;
    previousMicros  = previousMicros + 10000UL;
    hundredths = hundredths+1;
    if (hundredths == 10){
      hundredths = 0;
      tenths = tenths +1;
      if (tenths == 10){
        tenths = 0;
        secondsOnes = secondsOnes + 1;
        if (secondsOnes == 10){
          secondsOnes = 0;
          secondsTens = secondsTens +1;
          if (secondsTens == 6){ 
            secondsTens = 0;
            minutesOnes =  minutesOnes + 1;
            if (minutesOnes == 10){
              minutesOnes = 0;
              minutesTens = minutesTens +1;
              if (minutesTens == 6){
                minutesTens = 0;
                hoursOnes = hoursOnes +1;
                if (hoursOnes == 10){
                  hoursOnes = 0;
                  hoursTens = hoursTens =1;
                  if (hoursOnes == 4 && hoursTens ==2){
                    hoursOnes = 0;
                    hoursTens = 0;
                  }
                }
              } // minutesTens rollover check
            } // minutesOnes rollover check
          } // secondsTens rollover check
        } // secondsOnes rollover check
      } // tenths rollover check
    } // hundredths rollover check
  } // hundredths passing check



  if (oldSecondsOnes != secondsOnes){  // show the elapsed time
    oldSecondsOnes = secondsOnes;
    Serial.print ("Time: ");
    Serial.print (hoursTens);
    Serial.print(hoursOnes);
    Serial.print(":");
    Serial.print(minutesTens);
    Serial.print(minutesOnes);
    Serial.print(":");
    Serial.print(secondsTens);
    Serial.println(secondsOnes);

  } // end one second check

} // end loop

Replace Serial with whatever you are using to send to the LCD. I have sent the data to shift registers for display; serial is good to show that it is working to start.

magruder13:
Hi all, I'm sure this question has been asked multiple times before but I can't seem to find where. All I want is to have a clock, on my Arduino Uno which will keep time while I do other tasks.

I have read about the library Time but the standalone version uses Serial. I don't want serial, I just want to print the frigging time to an LCD screen, read in some sensors, and do some calculations.

Is this possible?!?!

Maybe rethink ... I am only doing 1 additional sensor for temperature, but you could just as easy do humidity or barometric, etc. At least you know the time is correct! GPS clock TFT GLCD

Ray