Hello,
I'm working on my bachelor project and i need to be able to read a rotary encoder. The rotary encoder i'm working with is the Koyo absolute rotary encoder TRD-NA1024NW.
datasheet can be found here: http://www.koyoele.co.jp/english/product/encoder/pdf/TRD_NA.pdf
It has 10 output pins. Now i've plugged in the pins on my arduino uno from pin 3 to 12. (see picture) And i'm trying to read the movements of the encoder on the serial monitor using this code:
#define BITS 10
#define LSB 12
int gray_decode(unsigned int n) {
unsigned int p = n;
while ( n = n >> 1) p = p ^ n;
return p;
}
int binToDec(int *inputval, int bits){
int i;
int val = 1;
int result = 0;
for (i = bits - 1; i >= 0; i--){
result += inputval[i] * val;
val *= 2;
}
return result;
}
void setup(){
int i;
for (i = LSB; i > LSB - BITS; i--){
pinMode(i, INPUT_PULLUP);
}
Serial.begin(115200);
Serial.print("ready\n");
}
void loop(){
int rawValues[BITS];
int i, j;
int result;
j = BITS - 1;
for (i = LSB; i > LSB - BITS; i--){
int readVal;
readVal = !digitalRead(i);
rawValues[j] = readVal;
j--;
}
result = binToDec(rawValues, BITS);
result = gray_decode(result);
for(j = 0; j < BITS; j++){
Serial.print(rawValues[j]);
}
Serial.print("\t");
Serial.println(result);
delay(100);
}
when i run this i only get zero values and some random jumps in values as you can see below. Slowly turning the encoder does nothing..
0000000000 0
0000000000 0
0000000000 0
0000000000 0
0000000000 0
0000000000 0
0100000000 511
0000000000 0
0000000000 0
0000000000 0
0000000000 0
0000000000 0
0000000000 0
0000000000 0
0000100000 63
0000000000 0
1000000000 1023
0000000000 0
1000000000 1023
0000000000 0
0000000000 0
0000000000 0
0000000000 0
0000000000 0
0000000000 0
0000010000 31
I would appreciate any input/thoughts that might help me in the right direction.
Thanks