printing/getting angle from encoder

HP (QEDS-7163) Optical Incremental Encoder (256 CPR)
I would like to convert the position of my encoder to angle. I did the following but I am gettin still the position : var_degrees = ((360/256)*counter);

``````#include <LiquidCrystal.h>

LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

#define encoderPinA 2
#define encoderPinB 3
#define CPR 256
volatile int counter =0;
volatile boolean flag;

volatile int var_degrees =0;

void setup() {

pinMode(encoderPinA, INPUT);
pinMode(encoderPinB, INPUT);
Serial.begin (9600);
attachInterrupt(digitalPinToInterrupt(encoderPinA), isr_2, RISING);
lcd.clear();

}

void loop() {

if(flag == true){
//Serial.println(counter);
var_degrees = ((360/256)*counter);

Serial.println(var_degrees);
lcd.setCursor(0, 1);
lcd.print("Position: ");
lcd.setCursor(9, 1);
lcd.print(var_degrees);
flag = false;
}

}
//Interrupts

void isr_2(){
flag = true;
counter = counter -1; //COUNTER CLOCK WISE
}
else{
counter = counter +1; //CW
}
}
else{
counter = counter + 1; //CW
}
else{
counter = counter - 1 ; //CCW
}
}
}
``````

You're getting "integer-math'ed" here - there may be other things, but this is definitely a problem:

360/255 is 1 in integer math. If you want to do floating point math, make one of the operands a float, eg (360/255.0)

What output are you getting? Does that explain the issue? You haven't posted what output you're getting... though your post reads as if you intended to.

Oh didn't know that!! I just changed it to var_degrees = ((360/256.0)*counter); and I just rotated the encoder 360 degrees and it seem to be outputting the right degree which is 360.