# Code wheel

Hi I need use this

But I can't with my code, do you have solution or library for this ?

``````int A = 2;
int B = 3;
int line_per_turn = 6000;
int degree = line_per_turn / 380;
int reference_line = 0;
bool forward = 0;
bool A_state;
bool B_state;
int T = 0;
bool TA1 = 0;
bool TB1 = 0;
bool TA2 = 0;
bool TB2 = 0;
bool TA3 = 0;
bool TB3 = 0;
bool TA4 = 0;
bool TB4 = 0;

void setup() {
pinMode(A, INPUT);
pinMode(B, INPUT);
Serial.begin(9600);
}

// the loop function runs over and over again forever
void loop() {

if(A_state_temp != A_state || B_state_temp != B_state){
if(A_state != A_state_temp){
reference_line++;
}

A_state = A_state_temp;
B_state = B_state_temp;

if(T == 0){
TA1 = A_state;
TB1 = B_state;
T++;
}
else if(T == 1){
TA2 = A_state;
TB2 = B_state;
T++;
}
else if(T == 2){
TA3 = A_state;
TB3= B_state;
T++;
}
else if(T == 3){
TA4 = A_state;
TB4 = B_state;
T=0;

if((TA1 && !TB1) && (TA2 && TB2) && (!TA3 && TB3) && (!TA4 && !TB4)){
forward = 0;
reference_line =0;
}
else if((!TA1 && TB1) && (TA2 && TB2) && (TA3 && !TB3) && (!TA4 && !TB4)){
forward = 1;
reference_line =0;
}
}
if(forward){
Serial.print("Avant: ");
Serial.println(reference_line);
}
if(!forward){
Serial.print("ArriÃ¨re: ");
Serial.println(reference_line);
}
}
}
``````

Thank you

Is that a standard quadrature encoder? Your code is very complex. Are you trying to
do more than just count transitions?

Could you tell us what specific encoder you have rather than a diagram explaining how encoders work?

Yes it's quadrature encoder, I don't know the simply method for use this. I haven't datasheet of this, just 2 signal 5V for know if forward or reverse. I have test count but it's not very accuracy because I find arduino to slow for fast rotation.
I think the code is generic, I want to know if someone had doing this ?

I find arduino to slow for fast rotation.

What is the rotational speed of the shaft ?

``````int line_per_turn = 6000;
int degree = line_per_turn / 380;
``````

?