Void After Void Loop?

I have searched for this but only found information about the difference between Setup and Void Loop, which I understand.

So with the code I am working on, where I have added LED PWM to a RTC Clock code.

The code works, but the PWM timings are affected by the RTC Delay

So, a couple of questions, what is the two "Void" after the Void Loop and is there a way to segregate the two parts of the code, so that the PWM does not effect the RTC or vice versa?

#include <TM1637.h>

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

#define CLK 9
#define DIO 8

int ledPin = 6;

TM1637 Display1(CLK, DIO);
  int8_t Digitos[] = {0,1,2,3};
  int horas;
  int minutos;
  boolean alterna;

void setup() {
  Serial.begin(9600);
  while (!Serial) ; // wait for serial
  delay(200);
  Serial.println("DS1307RTC Read Test");
  Serial.println("-------------------");

  Display1.set();
  Display1.init();
  
}

void loop() {
  // fade in from min to max in increments of 5 points:
  for (int fadeValue = 0 ; fadeValue <= 100; fadeValue += 5) {
    // sets the value (range from 0 to 255):
    analogWrite(ledPin, fadeValue);
    // wait for 30 milliseconds to see the dimming effect
    delay(30);
  }

  // fade out from max to min in increments of 5 points:
  for (int fadeValue = 100 ; fadeValue >= 0; fadeValue -= 5) {
    // sets the value (range from 0 to 255):
    analogWrite(ledPin, fadeValue);
    // wait for 30 milliseconds to see the dimming effect
    delay(30);
  }
  
  tmElements_t tm;

  if (RTC.read(tm)) {
    Serial.print("Ok, Time = ");
    print2digits(tm.Hour);
    Serial.write(':');
    print2digits(tm.Minute);
    Serial.write(':');
    print2digits(tm.Second);
    Serial.print(", Date (D/M/Y) = ");
    Serial.print(tm.Day);
    Serial.write('/');
    Serial.print(tm.Month);
    Serial.write('/');
    Serial.print(tmYearToCalendar(tm.Year));
    Serial.println();

    horas = tm.Hour;
    minutos = tm.Minute;
    CalculaDigitos(horas, minutos);
    if (alterna)
      {
        Display1.point(POINT_OFF);
        alterna = false;
      }
      else
      {
        Display1.point(POINT_ON);
        alterna = true;
      }
    
  } else {
    if (RTC.chipPresent()) {
      Serial.println("The DS1307 is stopped.  Please run the SetTime");
      Serial.println("example to initialize the time and begin running.");
      Serial.println();
    } else {
      Serial.println("DS1307 read error!  Please check the circuitry.");
      Serial.println();
    }
    delay(9000);
  }
  delay(1000);
}

void print2digits(int number) {
  if (number >= 0 && number < 10) {
    Serial.write('0');
  }
  Serial.print(number);
}

void CalculaDigitos( int hor, int minu)
   {
      int8_t Digit0 = minu %10 ;
      int8_t Digit1 = (minu % 100) / 10 ;
      int8_t Digit2 = hor % 10 ;
      int8_t Digit3 = (hor % 100) / 10 ;

      Digitos[3] = Digit0 ;
      Digitos[2] = Digit1 ;
      Digitos[1] = Digit2 ;
      Digitos[0] = Digit3 ;

      Display1.display(Digitos);
   }

The 2 other 'functions' are for executing tasks 1 does a Serial.write and the other writes to a display. To remove (or reduce at least) the delay, you'll have to remove the delay() and get your timing done using millis()

Functions serve two purposes:
1)
Re-usability; e.g. analogWrite is a function that you use multiple times in your code.
2)
Keep your code tidy; a single function should basically fit on a sheet of A4 paper.

So integrating one function in another one is not a good idea. And to be able to integrate, you will need to understand what the functions do. But again, it's not a good idea.

As Deva_Rishi implied, the cause of your problem is not the use of functions.

Time to read / study
1)
The blink-without-delay example
2)
Using millis() for timing. A beginners guide
3)
Demonstration code for several things at the same time

Give this a try (not tested.) If it works for you study what it's doing to understand how you can do a few things "at once" without a RTOS and without inappropriate use of delay().

#include <TM1637.h>

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

#define CLK 9
#define DIO 8

int ledPin = 6;

TM1637 Display1(CLK, DIO);
int8_t Digitos[] = {0,1,2,3};
int horas;
int minutos;
boolean alterna;

#define TCHECK_TIME     9000L   //mS between time checks
#define FADE_TIME       30      //mS for LED updates
#define FADE_DOWN       0
#define FADE_UP         1
unsigned long
    currentTime,
    TimeCheckTimer,
    FadeTimer;

void setup() 
{
    pinMode( ledPin, OUTPUT );
    Serial.begin(9600);
    while (!Serial) ; // wait for serial
    delay(200);
    
    Serial.println("DS1307RTC Read Test");
    Serial.println("-------------------");

    Display1.set();
    Display1.init();

    currentTime = millis();
    FadeTimer = currentTime;
    TimeCheckTimer = currentTime;
    
}//setup

void loop() 
{
    LEDFade();
    ReadTime();
    
}//loop

void LEDFade( void )
{
    static int
        fadevalue=0;
    static byte
        bState=FADE_UP;

    currentTime = millis();
    if( currentTime - FadeTimer < FADE_TIME )
        return;

    FadeTimer = currentTime;
    
    analogWrite(ledPin, fadevalue);
    
    if( bState == FADE_DOWN )
    {
        fadevalue -= 5;
        if( fadevalue == 0 )
            bState = FADE_UP;    
    }//if
    else
    {
        fadevalue += 5;
        if( fadevalue == 100 )
            bState = FADE_DOWN;    
        
    }//else
    
}//LEDFade

void ReadTime( void )
{
    tmElements_t 
        tm;

    currentTime = millis();
    if( currentTime - TimeCheckTimer < TCHECK_TIME )
        return;

    TimeCheckTimer = currentTime;
        
    if( RTC.read(tm) ) 
    {
        Serial.print("Ok, Time = ");
        print2digits(tm.Hour);
        Serial.write(':');
        print2digits(tm.Minute);
        Serial.write(':');
        print2digits(tm.Second);
        Serial.print(", Date (D/M/Y) = ");
        Serial.print(tm.Day);
        Serial.write('/');
        Serial.print(tm.Month);
        Serial.write('/');
        Serial.print(tmYearToCalendar(tm.Year));
        Serial.println();

        horas = tm.Hour;
        minutos = tm.Minute;
        CalculaDigitos(horas, minutos);
        if( alterna )
        {
            Display1.point(POINT_OFF);
            alterna = false;
        }//if
        else
        {
            Display1.point(POINT_ON);
            alterna = true;
        }//else
    
    }
    else 
    {
        if( RTC.chipPresent() ) 
        {
            Serial.println("The DS1307 is stopped.  Please run the SetTime");
            Serial.println("example to initialize the time and begin running.");
            Serial.println();
        }//if
        else 
        {
            Serial.println("DS1307 read error!  Please check the circuitry.");
            Serial.println();
        }//else
        
    }//else
    
}//ReadTime

void print2digits( int number ) 
{
    if( number >= 0 && number < 10 )
        Serial.write( '0' );

    Serial.print( number );
    
}//print2digits

void CalculaDigitos( int hor, int minu )
{
    int8_t 
        Digit0 = minu %10 ;
    int8_t 
        Digit1 = (minu % 100) / 10 ;
    int8_t 
        Digit2 = hor % 10 ;
    int8_t 
        Digit3 = (hor % 100) / 10 ;

    Digitos[3] = Digit0 ;
    Digitos[2] = Digit1 ;
    Digitos[1] = Digit2 ;
    Digitos[0] = Digit3 ;

    Display1.display(Digitos);

}//CalculaDigitos

sterretje:
2)
Keep your code tidy; a single function should basically fit on a sheet of A4 paper.

Multiple studies have reached a common conclusion: the ideal function fits within the confines of a monitor.

Delta_G:
I'm going to need a bigger monitor.

Have you considered turning it :wink: Mine will be a big A4 :smiley:

Blackfin:
Give this a try (not tested.) If it works for you study what it's doing to understand how you can do a few things "at once" without a RTOS and without inappropriate use of delay().

#include <TM1637.h>

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

#define CLK 9
#define DIO 8

int ledPin = 6;

TM1637 Display1(CLK, DIO);
int8_t Digitos[] = {0,1,2,3};
int horas;
int minutos;
boolean alterna;

#define TCHECK_TIME     9000L   //mS between time checks
#define FADE_TIME       30      //mS for LED updates
#define FADE_DOWN       0
#define FADE_UP         1
unsigned long
   currentTime,
   TimeCheckTimer,
   FadeTimer;

void setup()
{
   pinMode( ledPin, OUTPUT );
   Serial.begin(9600);
   while (!Serial) ; // wait for serial
   delay(200);
   
   Serial.println("DS1307RTC Read Test");
   Serial.println("-------------------");

Display1.set();
   Display1.init();

currentTime = millis();
   FadeTimer = currentTime;
   TimeCheckTimer = currentTime;
   
}//setup

void loop()
{
   LEDFade();
   ReadTime();
   
}//loop

void LEDFade( void )
{
   static int
       fadevalue=0;
   static byte
       bState=FADE_UP;

currentTime = millis();
   if( currentTime - FadeTimer < FADE_TIME )
       return;

FadeTimer = currentTime;
   
   analogWrite(ledPin, fadevalue);
   
   if( bState == FADE_DOWN )
   {
       fadevalue -= 5;
       if( fadevalue == 0 )
           bState = FADE_UP;    
   }//if
   else
   {
       fadevalue += 5;
       if( fadevalue == 100 )
           bState = FADE_DOWN;    
       
   }//else
   
}//LEDFade

void ReadTime( void )
{
   tmElements_t
       tm;

currentTime = millis();
   if( currentTime - TimeCheckTimer < TCHECK_TIME )
       return;

TimeCheckTimer = currentTime;
       
   if( RTC.read(tm) )
   {
       Serial.print("Ok, Time = ");
       print2digits(tm.Hour);
       Serial.write(':');
       print2digits(tm.Minute);
       Serial.write(':');
       print2digits(tm.Second);
       Serial.print(", Date (D/M/Y) = ");
       Serial.print(tm.Day);
       Serial.write('/');
       Serial.print(tm.Month);
       Serial.write('/');
       Serial.print(tmYearToCalendar(tm.Year));
       Serial.println();

horas = tm.Hour;
       minutos = tm.Minute;
       CalculaDigitos(horas, minutos);
       if( alterna )
       {
           Display1.point(POINT_OFF);
           alterna = false;
       }//if
       else
       {
           Display1.point(POINT_ON);
           alterna = true;
       }//else
   
   }
   else
   {
       if( RTC.chipPresent() )
       {
           Serial.println("The DS1307 is stopped.  Please run the SetTime");
           Serial.println("example to initialize the time and begin running.");
           Serial.println();
       }//if
       else
       {
           Serial.println("DS1307 read error!  Please check the circuitry.");
           Serial.println();
       }//else
       
   }//else
   
}//ReadTime

void print2digits( int number )
{
   if( number >= 0 && number < 10 )
       Serial.write( '0' );

Serial.print( number );
   
}//print2digits

void CalculaDigitos( int hor, int minu )
{
   int8_t
       Digit0 = minu %10 ;
   int8_t
       Digit1 = (minu % 100) / 10 ;
   int8_t
       Digit2 = hor % 10 ;
   int8_t
       Digit3 = (hor % 100) / 10 ;

Digitos[3] = Digit0 ;
   Digitos[2] = Digit1 ;
   Digitos[1] = Digit2 ;
   Digitos[0] = Digit3 ;

Display1.display(Digitos);

}//CalculaDigitos

Very helpful, I see now that you can define delay times for the loop, so they don't start adding up with each other.

One thing I have observed this morning when testing, is that the colon on the TM1637 will stay on for 9 seconds and off for 9 seconds.

I changed "#define TCHECK_TIME 9000L" To "#define TCHECK_TIME 1000L"

Ah right. I misintrepreted the "delay(9000)" in your code (I see now used for the error condition.)

Upon a second glance I see now that 1000 was what you intended.

Oh well. Easy fix :slight_smile: