Slowing Serial Without slowing Sketch.

Hello!!! I have a sketch that work fine and doing what i want so far. Now when i use the serial monitor the clock reading just go insanely fast if i don't put a delay in it but if i do i slow down the hole sketch. I am not sure how to put a delay in it without affecting the rest. I just learn how to use millis but to make something "work". I don't know if we can use millis and how i should write it to add a delay at end of a function or if there is something else i can use.

So how can i slow down the Serial Monitor to refresh every second (like a clock) without creating a delay if use in a bigger sketch.

This is the code i am using print time in the serial monitor.

#include <Time.h>
#include <Wire.h>
#include <DS1307RTC.h>

void setup()
{
Serial.begin(9600);
Serial.println("Welcome!"); //Test the serial monitor
setSyncProvider(RTC.get);
}

void loop()

{
Serial.print(hour());
printDigits(minute());
printDigits(second());
Serial.println();
}

void printDigits(int digits)
{
Serial.print(":");
if(digits < 10)
Serial.print('0');
Serial.print(digits);
}

Hello,

Look at the Blink Without Delay example from the Arduino IDE.

Alternatively you could do something like this:

void loop() 
{
  static uint8_t prevSecond = 255;

  if ( second() != prevSecond )
  {
    prevSecond = second();

    Serial.print(hour());
    printDigits(minute());
    printDigits(second());
    Serial.println();
  }
}

So of it should look something like this and use an interval of 1000

Void loop()
{

unsigned long currentMillis = millis();

if(currentMillis - previousMillis > interval) {

previousMillis = currentMillis;

Serial.print(hour());
printDigits(minute());
printDigits(second());
Serial.println();
}
}

Yes, but you have to change > to >= for more accuracy.

The demo several things at a time illustrates the use of millis() to manage timing without slowing other parts down.

...R

Keep a copy of the seconds value from the last time you printed and only print again if it's changed.