rotary encoder does not work when pulseIn active

Hi everyone.

The problem with pulseIn function. Rotary encoder does not work (not counting) when pulseIn active. Also Serialprint() printing with delay.
When I remark (block comment) pulseIn function, rotary encoder counting properly and Serialprint() working.

I control the 2 Brushless fan with PWM over PCA9685 module. Using Encoders to increase and decrease speed.
Now I want to count RPM of fans. Isolated input with Optocoupler K817. Want to count RPM but pulseIn function seems does not allow count pulses from encoders.
How can I count the pulses another way?
Here is the sketch:

#include <Adafruit_PWMServoDriver.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <EEPROM.h>

/*
-VCC-5V
  |
 NTC - Vout
  |
  R(fixed)
  |
 GND
*/
Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();
LiquidCrystal_I2C lcd(0x3C,16,2);  //sometimes the adress is not 0x3f. Change to 0x27 if it dosn't work.//A0 and A1 are shorted

short ThermistorPin = 1;// Analog input pin for thermistor voltage
short Temp_Pot = 0; //Temperature Potentiometer PIN

boolean f_encoder_a;
boolean f_encoder_b;
boolean r_encoder_a;
boolean r_encoder_b;
boolean f_encoder_sw;
boolean r_encoder_sw;
uint8_t f_pwmnum= 0;
uint8_t r_pwmnum= 1;

boolean switch_temp;
int Vo;// Integer value of voltage reading
float R = 9870.0;// Fixed resistance in the voltage divider
float logRt,Rt,T;
float c1 = 2.108508173e-03, c2 = 0.7979204727e-04, c3 = 6.535076315e-07;//Water Sersor R=10kOhm-25 degree Celcius.   B=3435

boolean f_encoder_a_prev;
boolean r_encoder_a_prev;
boolean f_encoder_sw_prev;
boolean r_encoder_sw_prev;

unsigned int f_counter;
unsigned int r_counter;
unsigned int amount[3];
unsigned short f_rpm_amount;
unsigned short r_rpm_amount;
unsigned short f_encoder_sw_counter;
unsigned short r_encoder_sw_counter;
//RPMreading//
int f_Htime;
int f_Ltime;
int r_Htime;
int r_Ltime;
float f_Ttime;            // integer for storing total time of a cycle
float r_Ttime;            // integer for storing total time of a cycle
float f_frequency;
float r_frequency;

void setup() {
  pwm.begin();
  pwm.setPWMFreq(1600);  // This is the maximum PWM frequency
  Wire.setClock(400000); //I2C clock frequency 400kHz
  Serial.begin(9600);

  pinMode(2, INPUT); //Temperature Switch
  pinMode(3, INPUT);  //Front Encoder A
  pinMode(4, INPUT);  //Front Encoder B
  pinMode(5, INPUT);  //Rear Encoder A
  pinMode(6, INPUT);  //Rear Encoder B
  pinMode(7, INPUT); //Front Fan RPM input
  pinMode(8, INPUT); //Rear Fan RPM input
  pinMode(11, INPUT); //Front Encoder Switch
  pinMode(12, INPUT); //Rear Encoder Switch
  amount[0] = 1;
  amount[1] = 10;
  amount[2] = 100;

}

void loop() {
  temp_reading();
  rpm_reading();
  switch_temp = digitalRead(2);
  f_encoder_a = digitalRead(3);
  f_encoder_b = digitalRead(4);
  r_encoder_a = digitalRead(5);
  r_encoder_b = digitalRead(6);
  f_encoder_sw = digitalRead(11);
  r_encoder_sw = digitalRead(12);
  encoder_switch();
  encoder_reading();
  Serial.print(f_counter);
  Serial.print(" ");
  Serial.print(r_counter);
  Serial.println();
   
      pwm.setPWM(f_pwmnum, 0, f_counter % 4096 );
      pwm.setPWM(r_pwmnum, 0, r_counter % 4096 );
}

void temp_reading()
{
  Vo = analogRead(ThermistorPin);
  Rt = R*( (1023.0 / (float)Vo) - 1.0 );
  logRt = log(Rt);
  T = ( 1.0 / (c1 + c2*logRt + c3*logRt*logRt*logRt ) ) - 273.15;
} //end temp_reading
/***************************************************************************************/
void rpm_reading()
{
  
  f_Htime=pulseIn(7,HIGH);      //read high time
  f_Ltime=pulseIn(7,LOW);        //read low time
  f_Ttime = f_Htime+f_Ltime;
  f_frequency=1000000/f_Ttime;    
  
}//end rpm_reading
void encoder_switch()
{
                   //FRONT//
  if(f_encoder_sw==false){f_encoder_sw_prev = f_encoder_sw;}
  if(f_encoder_sw==true&&f_encoder_sw_prev==false)
  {
    f_encoder_sw_counter++;
    if(f_encoder_sw_counter>2){f_encoder_sw_counter = 0;}
    f_encoder_sw_prev = f_encoder_sw;
  }
  switch (f_encoder_sw_counter)
  {
    case 0:
          f_rpm_amount = amount[0];
    break;
    case 1:
          f_rpm_amount = amount[1];
    break;
    case 2:
         f_rpm_amount = amount[2];
    break;
  }
                     //REAR//
  if(r_encoder_sw==false){r_encoder_sw_prev = r_encoder_sw;}
  if(r_encoder_sw==true&&r_encoder_sw_prev==false)
  {
    r_encoder_sw_counter++;
    if(r_encoder_sw_counter>2){r_encoder_sw_counter = 0;}
    r_encoder_sw_prev = r_encoder_sw;
  }
  switch (r_encoder_sw_counter)
  {
    case 0:
          r_rpm_amount = amount[0];
    break;
    case 1:
          r_rpm_amount = amount[1];
    break;
    case 2:
         r_rpm_amount = amount[2];
    break;
  }
}//end encoder_switch
/***************************************************************************/
void encoder_reading()
{
            //FRONT//
 if(f_encoder_a != f_encoder_a_prev)
 {
      if(f_encoder_b != f_encoder_a) 
      {
        if(f_encoder_a_prev==true)
         {
          if((f_counter+f_rpm_amount)<4096)
           {
            f_counter = f_counter + f_rpm_amount;
           }
         }
        f_encoder_a_prev = f_encoder_a;
      }   
      else 
      {
        if(f_encoder_a_prev==true)
          {
           if(f_counter>200)
           {
            f_counter = f_counter - f_rpm_amount;
           }
          }     
        f_encoder_a_prev = f_encoder_a;      
      }   

  }
             //REAR//
  if(r_encoder_a != r_encoder_a_prev)
 {
      if(r_encoder_b != r_encoder_a) 
      {
        if(r_encoder_a_prev==true)
         {
          if((r_counter+r_rpm_amount)<4096)
           {
            r_counter = r_counter + r_rpm_amount;
           }
         }
        r_encoder_a_prev = r_encoder_a;
      }   
      else 
      {
        if(r_encoder_a_prev==true)
          {
           if(r_counter>200)
           {
            r_counter = r_counter - r_rpm_amount;
           }
          }     
        r_encoder_a_prev = r_encoder_a;      
      }   
         
  }
    
}//end encoder_reading

I suspect there is no need to use pulseIn() to measure the speed of the fan. However you have not told us what sort of detector you are using to measure the speed.

Also, what is the role of the rotary encoder?

I suspect it would be useful to use the digitalWriteFast() library.

...R

Robin2:
Also, what is the role of the rotary encoder?

As I wrote the Rotary Encoder using to increase and decrease the value which sets the speed of Fan

Robin2:
I suspect it would be useful to use the digitalWriteFast() library.

Could you please clarify the function of particular library?
The problem is with reading the frequency of RPM, not driving of Fan

Deep_Sky:
As I wrote the Rotary Encoder using to increase and decrease the value which sets the speed of Fan

So it is just being turned by hand? I thought it might be turned by the motor and be used to measure speed.

Could you please clarify the function of particular library?
The problem is with reading the frequency of RPM, not driving of Fan

I had assumed the name would convey all that is needed - it just works faster than the standard digitalRead() so it should ake more CPU cycles available for other stuff.

Alas :), you have not responded to the important part of Reply #1 - how are you detecting the speed?

...R

How can I count the pulses another way?

Connect whatever is generating the pulses to a pin that supports external interrupts, and attach an interrupt handler.