help me please with Encoder HEDM-5500 and Decoder HCTL 2021 counter

Hello, I'm a newbie in this, my problem is this? I did not get an exact count of the HEDM-5500 B06 encoder (
) 1024 RCP with arduino mega adk, for a furuta pendulum, the count starts at 0, but when it rotates, it does not reach 0 if it does not move + - 40 for each REV, so connect it to an HCTL decoder 2021-A00 () the problem with this is the one that sends 8 bits = 255, but after that it resets to 0, it does it every 1/16 of a turn of the HEDM-5500 and I can not make an exact account. I have tried other programs on the net, but they are not exact either.

volatile int pin1 = 22;
volatile int pin2 = 23;
volatile int pin3 = 24;
volatile int pin4 = 25;
volatile int pin5 = 26;
volatile int pin6 = 27;
volatile int pin7 = 28;
volatile int pin8 = 29;
volatile int conta = 0;
int x = 0;
int y = 0;
int z = 0;
int t =0;
int total =0;


void setup() {
  Serial.begin(9600);
  pinMode(2,INPUT);
  pinMode(3,INPUT);
  pinMode(4,INPUT);
  pinMode(5,INPUT);
  pinMode(6,INPUT);
  pinMode(7,INPUT);
  pinMode(8,INPUT);
  pinMode(20,INPUT);
}

void loop() {


  
  volatile int p1 = digitalRead(pin1);
  volatile int p2 = digitalRead(pin2);
  volatile int p3 = digitalRead(pin3);
  volatile int p4 = digitalRead(pin4);
  volatile int p5 = digitalRead(pin5);
  volatile int p6 = digitalRead(pin6);
  volatile int p7 = digitalRead(pin7);
  volatile int p8 = digitalRead(pin8);
 
  total =  (p1 * 1) + (p2 * 2) + ( p3 * 4) + ( p4 * 8) + (p5 * 16) + (p6 * 32) + (p7 * 64) + (p8 * 128) ; 

  Serial.print(digitalRead(pin8), BIN);
  Serial.print(digitalRead(pin7), BIN);
  Serial.print(digitalRead(pin6), BIN);
  Serial.print(digitalRead(pin5), BIN);
  Serial.print(digitalRead(pin4), BIN);
  Serial.print(digitalRead(pin3), BIN);
  Serial.print(digitalRead(pin2), BIN);
  Serial.print(digitalRead(pin1), BIN);



    
 if(total >= 254)
 {
  y = y+1;
  x = y * 255;
 
}
z = total + x;
  
  Serial.print("  total:  ");
  Serial.print(total);
  Serial.print(" total2:");
  Serial.println(z);

 
  }

the problem is so fast that the count sometimes does not enter, for example, 250 is passed to 0 by the speed and everything is confusing, I hope I could explain my problem well

For now you should get rid of all "volatile", it does not what you seem to expect.

Also consider what's the chance for total>=254. So far your code does not make any sense.

For more detailed help you should post links to the data sheets of your modules, eventually also a circuit diagram.

Thank you very much for responding to DrDiettrich, yes, of course, you have already corregi the volatile, attach the files HEDM-5500 B06 ((https://www.infineon.com/dgdl/Infineon-Encoder_HEDS-5540-A14-AP-v01_00-EN.pdf?fileId=5546d46147a9c2e40147d3d593970357) and HCTL-2021-A00(https://datasheet.octopart.com/HCTL-2021-PLC-Avago-datasheet-7580518.pdf)

int pin1 = 22;
int pin2 = 23;
int pin3 = 24;
int pin4 = 25;
int pin5 = 26;
int pin6 = 27;
int pin7 = 28;
int pin8 = 29;
int conta = 0;
int x = 0;
int y = 0;
int z = 0;
int t =0;
int total =0;


void setup() {
  Serial.begin(9600);
  pinMode(2,INPUT);
  pinMode(3,INPUT);
  pinMode(4,INPUT);
  pinMode(5,INPUT);
  pinMode(6,INPUT);
  pinMode(7,INPUT);
  pinMode(8,INPUT);
  pinMode(20,INPUT);
}

void loop() {


  
 int p1 = digitalRead(pin1);
 int p2 = digitalRead(pin2);
 int p3 = digitalRead(pin3);
 int p4 = digitalRead(pin4);
 int p5 = digitalRead(pin5);
 int p6 = digitalRead(pin6);
 int p7 = digitalRead(pin7);
 int p8 = digitalRead(pin8);
 
  total =  (p1 * 1) + (p2 * 2) + ( p3 * 4) + ( p4 * 8) + (p5 * 16) + (p6 * 32) + (p7 * 64) + (p8 * 128) ; 

  Serial.print(digitalRead(pin8), BIN);
  Serial.print(digitalRead(pin7), BIN);
  Serial.print(digitalRead(pin6), BIN);
  Serial.print(digitalRead(pin5), BIN);
  Serial.print(digitalRead(pin4), BIN);
  Serial.print(digitalRead(pin3), BIN);
  Serial.print(digitalRead(pin2), BIN);
  Serial.print(digitalRead(pin1), BIN);
    
 if(total >= 254)
 {
  y = y+1;
  x = y * 255;
 
}
z = total + x;
  
  Serial.print("  total:  ");
  Serial.print(total);
  Serial.print(" total1:");
  Serial.println(z);
 
  }

La probabilidad es muy pequeña de que llegue total>=254 (sorry for the quality of my circuit)

Why do you use a decoder if you don't know how to use it? This chip is intended for use with 8 bit microprocessors, not with microcontrollers.

See the many examples for Arduinos reading rotary encoders directly.

clear thank you very much DrDiettrich for answering. I appreciate it and that's why I'm looking at the examples you mentioned, another thing that you forgive me for being a novice, but as you knew or guessed it was for an 8-bit microprocessor, it's because of my mistake, I do it. that. I do not want to fall into the same mistake again.

You are welcome. Come back if you have any questions about your project.