Blynktimer make the reading sensor delayed dan send wrong result value to blynk

Hello again all.

I have new problem about the project that I have posted before.

I have a linear actuator which I have instaled 2 reed magnetic sensor to determine direction and position. When I test it without blynk it runs very well, when the actuator extends it displays value from 0 to 400 and when the actuator retracts it displays value from 400 to 0.

But when I want to send the value to my phone use blynk, it sends the wrong value, when it extends only send data from 0 to 60, and when it retract it send 60 to 80 not decrease the value. I guess it because the blynktimer which delayed the reading of the sensor.

Plase help, how to send true value which represent the position if the actuator.

Mega+esp8266 as a shield, android pie.

This is the code.

#define A 22
#define B 23
int counter = 0;
int diam;
int akhir;
float volt;
#define BLYNK_PRINT Serial
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "54737a98fcd843968facd53968b53691";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "jonihermawanto";
char pass[] = "123456789";

// Hardware Serial on Mega, Leonardo, Micro...
#define EspSerial Serial1

// or Software Serial on Uno, Nano...
//#include <SoftwareSerial.h>
//SoftwareSerial EspSerial(2, 3); // RX, TX

// Your ESP8266 baud rate:
#define ESP8266_BAUD 115200

ESP8266 wifi(&EspSerial);


BlynkTimer timer;

void sensor(){
  diam=digitalRead(A);
  if (diam!=akhir){
    if(digitalRead(B)!=diam){
      counter ++;
      }else{
        counter --;
      }
      Serial.print("Posisi :  ");
      Serial.println(counter);
       Blynk.virtualWrite(V5, counter);
   }
      akhir=diam;
}


void setup (){
  pinMode(A, INPUT);
  pinMode(B, INPUT);
  Serial.begin (9600);
  akhir=digitalRead(A);
  timer.setInterval(100 , sensor);
  

  // Set ESP8266 baud rate
  EspSerial.begin(ESP8266_BAUD);
  delay(10);

  Blynk.begin(auth, wifi, ssid, pass);
  // You can also specify server:
  //Blynk.begin(auth, wifi, ssid, pass, "blynk-cloud.com", 80);
  //Blynk.begin(auth, wifi, ssid, pass, IPAddress(192,168,1,100), 8080);
}

void loop (){
  Blynk.run();
  timer.run();
}

This sounds like an encoder type sensor… you probably need to use interrupts to read this changes and calculate the direction/count as even 100ms polling timer will not be fast enough for proper detection.

Yes ,it’s like a rotary encoder.
Thank you for your advice but, I still don’t urdenstant to uses interrupts.

https://www.google.com/search?q=rotary+encoder+with+interrupt