Rotary encoder ignores some increments

Hi!

So I have written this software that displays a 4 digit timer on a 7-segment display. The timer can be changed by rotating the encoder. I followed this tutorial by brainy bits on de-bouncing the encoder and its runs really smoothly. However, I increased the complexity of the code so it can do more stuff (meant I had to make a bunch of functions) - Now the encoder doesn't run smooth at all and ignores lots of rotations. I have no idea why because the core logic for the encoder is the same.

My only assumption is that the 5 or 6 functions + bunch of variables I have added have slowed down the processor but all those functions don't contain anything demanding, just if statements. Heres the code for the original that worked, maybe someone can tell me what its vulnerabilities are. I can also add the code for the larger file created.

Any advice much appreciated thankyou!

#include "LedControl.h"

LedControl lc=LedControl(12,11,10,1);


// Rotary Encoder Module connections
const int PinSW=7;   // Rotary Encoder Switch
const int PinDT=5;    // DATA signal
const int PinCLK=6;    // CLOCK signal

// Variables to debounce Rotary Encoder
long TimeOfLastDebounce = 0;
int DelayofDebounce = 0.01;

// Store previous Pins state
int PreviousCLK;   
int PreviousDATA;

int counterTime = 0;

int displaycounter=0; // Store current counter value


void setup() {
  Serial.begin(9600);

  lc.shutdown(0,false);
  /* Set the brightness to a medium values */
  lc.setIntensity(0,4);
  /* and clear the display */
  lc.clearDisplay(0);
  
  // Put current pins state in variables
  PreviousCLK=digitalRead(PinCLK);
  PreviousDATA=digitalRead(PinDT);

  // Set the Switch pin to use Arduino PULLUP resistors
  pinMode(PinSW, INPUT_PULLUP);

 

}
void loop() {
  // If enough time has passed check the rotary encoder
  if ((millis() - TimeOfLastDebounce) > DelayofDebounce) {
    
    check_rotary();  // Rotary Encoder check routine below
    
    PreviousCLK=digitalRead(PinCLK);
    PreviousDATA=digitalRead(PinDT);
    
    TimeOfLastDebounce=millis();  // Set variable to current millis() timer
    printOutTime(counterTime);
  }
  
  // Check if Rotary Encoder switch was pressed
  if (digitalRead(PinSW) == LOW) {
    displaycounter=0;  // Reset counter to zero
    //P.print(displaycounter);
  }
}


// Check if Rotary Encoder was moved
void check_rotary() {

 if ((PreviousCLK == 0) && (PreviousDATA == 1)) {
    if ((digitalRead(PinCLK) == 1) && (digitalRead(PinDT) == 0)) {
      counterTime++;
      Serial.println(counterTime);
      
     
    }
    if ((digitalRead(PinCLK) == 1) && (digitalRead(PinDT) == 1)) {
      counterTime--;
      Serial.println(counterTime);
      
    }
  }

if ((PreviousCLK == 1) && (PreviousDATA == 0)) {
    if ((digitalRead(PinCLK) == 0) && (digitalRead(PinDT) == 1)) {
    counterTime++;
    Serial.println(counterTime);
      
    }
    if ((digitalRead(PinCLK) == 0) && (digitalRead(PinDT) == 0)) {
      counterTime--;
      Serial.println(counterTime);
      
    }
  }

if ((PreviousCLK == 1) && (PreviousDATA == 1)) {
    if ((digitalRead(PinCLK) == 0) && (digitalRead(PinDT) == 1)) {
      counterTime++;
      Serial.println(counterTime);
      
    }
    if ((digitalRead(PinCLK) == 0) && (digitalRead(PinDT) == 0)) {
      counterTime--;
      Serial.println(counterTime);
      
    }
  }  

if ((PreviousCLK == 0) && (PreviousDATA == 0)) {
    if ((digitalRead(PinCLK) == 1) && (digitalRead(PinDT) == 0)) {
      counterTime++;
      Serial.println(counterTime);
      
    }
    if ((digitalRead(PinCLK) == 1) && (digitalRead(PinDT) == 1)) {
      counterTime--;
      Serial.println(counterTime);
         
    }
  }            
 }

 void rpmDigitPrintOut(int one, int two, int three) {
    lc.setDigit(0,7,one,false);
    lc.setDigit(0,6,two,true);
    lc.setDigit(0,5,three,false);
}

void printOutrpm(int rpm) {
  int rpmStandardised = rpm;
  int thrdDig = (rpm/100) % 10;
  int secDig = (rpm/1000) % 10;
  int firstDig = (rpm/10000) % 10;
  rpmDigitPrintOut(firstDig,secDig,thrdDig);

}

void timeDigitPrintOut(int one, int two, int three, int four) {
    lc.setDigit(0,3,one,false);
    lc.setDigit(0,2,two,true);
    lc.setDigit(0,1,three,false);
    lc.setDigit(0,0,four,false);
}

void printOutTime(int timeCount) {

  int timeMinutes = (int)(timeCount/60.0);
  int timeSeconds = (timeCount - timeMinutes*60);
  int firstDig = (timeMinutes/10) % 10;
  int secondDig = (timeMinutes) % 10;
  int thirdDig = (timeSeconds/10) % 10;
  int fourthDig = (timeSeconds) % 10;
  timeDigitPrintOut(firstDig,secondDig,thirdDig,fourthDig);

  
}

int DelayofDebounce = 0.01; // Please Google the meaning of integer

That got past me! Thanks for pointing that out. Still doesn't solve the problem unfortunately.

To detect all the increments produced by a rotary encoder, you need to be reading the inputs as rapidly as possible, and to detect state changes. Debouncing the state changes is a secondary issue.

Your code is missing state changes for two reasons: an unnecessary delay:

  if ((millis() - TimeOfLastDebounce) > DelayofDebounce) {
     check_rotary();  // Rotary Encoder check routine below

and the Arduino is printing instead of looking for further changes:

    if ((digitalRead(PinCLK) == 1) && (digitalRead(PinDT) == 0)) {
      counterTime++;
      Serial.println(counterTime);

There are well thought out encoder libraries for Arduino -- do try them out.