I was working on a program and needed the user to be able to select from over 12 choices of pre-recorded patters.

I chose to use a absolute position rotary encoder that had 16 detents.

The particular one I chose was the Alps EC18AGB20401 Unfortunately the code that it returned was not quite binary so I had to use a look up table to go from 0-15.

int rotLUT[]={0,1,15,14,3,2,4,5,11,10,12,13,8,9,7,6};

Based on this table:

# #0 pin pin pin pin decodes as
4 3 2 1 dec
4 3 2 1 dec
1 0 0 0 0 0 0
2 1 0 0 0 1 1 clockwise
3 2 0 1 0 1 5
4 3 0 1 0 0 4
5 4 0 1 1 0 6
6 5 0 1 1 1 7
7 6 1 1 1 1 15
8 7 1 1 1 0 14
9 8 1 1 0 0 12
10 9 1 1 0 1 13
11 10 1 0 0 1 9
12 11 1 0 0 0 8
13 12 1 0 1 0 10
14 13 1 0 1 1 11
15 14 0 0 1 1 3
16 15 0 0 1 0 2

 

 

 

// ALPS Absolute position Rotary Encoder 18mm Size Insulated Shaft Type EC18A Series
// EC18AGB20401
// http://www.alps.com/prod/info/E/HTML/Encoder/Absolute/EC18A/EC18AGB20401.html
// (2) (4)(G)(3)(1) pins
//
int loopCount=0;
int rotLUT[]={0,1,15,14,3,2,4,5,11,10,12,13,8,9,7,6};
void setup()
{
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(A0,INPUT_PULLUP);
pinMode(A1,INPUT_PULLUP);
pinMode(A2,INPUT_PULLUP);
pinMode(A3,INPUT_PULLUP);
Serial.println("Boot...");

}

void loop()
{
// put your main code here, to run repeatedly:
Serial.print(loopCount);
Serial.print("loop, ");

int line3=digitalRead(A3);
Serial.print(",");
Serial.print(line3);

int line2=digitalRead(A2);
Serial.print(",");
Serial.print(line2);

int line1=digitalRead(A1);
Serial.print(",");
Serial.print(line1);

int line0=digitalRead(A0);
Serial.print(",");
Serial.print(line0);

int readVal=(line3<<3)+(line2<<2)+(line1<<1)+line0;
Serial.print(",read=");
Serial.print(readVal);

int realVal=rotLUT[readVal];
Serial.print(",real=");
Serial.print(realVal);

Serial.println(" ");
delay(1000);
loopCount++;
}

/*

# #0 pin pin pin pin decodes as
4 3 2 1 dec
4 3 2 1 dec
1 0 0 0 0 0 0
2 1 0 0 0 1 1 clockwise
3 2 0 1 0 1 5
4 3 0 1 0 0 4
5 4 0 1 1 0 6
6 5 0 1 1 1 7
7 6 1 1 1 1 15
8 7 1 1 1 0 14
9 8 1 1 0 0 12
10 9 1 1 0 1 13
11 10 1 0 0 1 9
12 11 1 0 0 0 8
13 12 1 0 1 0 10
14 13 1 0 1 1 11
15 14 0 0 1 1 3
16 15 0 0 1 0 2
*/