How to measure angular position of a rotary encoder?
5 views (last 30 days)
I am using the Arduino support package and an Arduino Mega2560 to measure the angular position of a HKT22 rotary encoder (encoder resolution is 300CPR). My goal is to rotate the shaft of the encoder by hand and have the angular position display in the workspace. However, with the code I wrote below, the angular position display increases continuously even when I stop rotating the shaft. Can anyone offer a solution to this problem?
a = arduino('COM8','Mega2560','Libraries','RotaryEncoder');
channelA = 'D2';
channelB = 'D3';
encoder = rotaryEncoder(a,channelA,channelB,300);
resetCount(encoder); time = 0; i = 1;
En_Measured_Angle = 0;
[count,time] = readCount(encoder);
Encoder_Speed = readSpeed(encoder);
Counts_Array(:,i) = count;
Total_Count = sum(Counts_Array(:,1:i));
En_Measured_Angle = (Total_Count*360)/300;
fprintf('Current Angle: %d\n',En_Measured_Angle);
i = i+1;