rotary encoder as array selector knob

Hello, everyone. As is usual for me, I have made a lot of progress on a project, followed immediately by being completely baffled by the next step.

Here's where I am, perhaps someone can point me in the right direction.

I have been messing around with some of the rotary encoder examples, and they work, but I don't quite understand how to adapt the example codes for my purpose.

I need to be able to use a rotary encoder (this one: Rotary Encoder - COM-09117 - SparkFun Electronics) to select one of 12 arrays.

What confuses me is that everything I read related to the encoders involves determining which direction the encoder is turning and the number of 'clicks' the encoder has passed in either direction. I am unclear as how to translate this into a user selecting from a number of options (in this case, one of 12 arrays).

Since this encoder also acts as a pushbutton, it would be nice to utilize that as well -- but it might not be necessary for what I'm trying to do.

Originally I was going to use a potentiometer and simply define 12 value ranges that would each then be attached to one of the arrays, but the lack of 'clickableness' proved unsatisfying. I want to do something like this but with a rotary encoder.

Is there away to define 12 'steps' for the encoder, and then say, "when the encoder is at position 1, use array A. When the encoder is at position 2, use array B" etc etc?

Can anyone out there in arduinoland help me out? Is there an example code out there that I am unaware of that will make this more clear to me?

Did you look at the worked examples for this device at the Sparkfun site? (though I'm not sure about the assertion it is a "Gray code" encoder - I'd've said "quadrature")

That's the only difficult bit - once that's working, the index is modulo arithmetic.

Well, if you understand the whole counting clicks in either direction part then just think of that as your array number. Lets say your counting the clicks with the integer arrayNumber. Initialize it at 1, and then have it count the clicks. Before applying arrayNumber determine what array to pick, put an if statement in there to make sure its within the 12 numbers you want, and if not make sure that it loops around.

Here is an example

void loop(){

int arrayAddOrSubtract = 0;

int arrayNumber = 1;

arrayAddOrSubtract = rotaryEncoderDirection();//function i made up which i would imagine would return a 1 or -1 depending on if its adding or subtracting

arrayNumber = arrayNumber+arrayAddOrSubtract;//if its a 1 should bump it up by (to 2 initially) or -1 would drop the number 1 (to 0 initially)

if (arrayNumber>12) arrayNumber=1;//assuming that your program only reads 1 click at a time, this should make it so that if you exceed your limit of 12 arrays to choose from, it will choose the 1st one again

if (arrayNumber<1) arrayNumber=12; //same as before, but now setting it to array 12 if the array gets to be less than one

ARRAYNAME[arrayNumber][x];//Finally, this is how you might want to call your array or whatever, using arrayNumber to choose which array you want and x to be where you want to work in that array. Maybe use a switch function or something too.

}

Sorry if this isn't what you needed but it seemed as though you grasped the concept of how the clicks counting goes, but had trouble with this part for some reason. Hope this helps.

Initialize it at 1

C array indices start at zero, so starting at one is potentially wasteful.

looks up modulo arithmetic

re-reads responses from WoAz and Groove

dim light bulb flickers on

Guys, thanks. I think you have provided the context that I needed to understand how to approach this. For whatever reason, I didn't grok how to keep the encoder from counting up or down forever, and that prevented my thoughts from developing further.

Using code that causes the encoder to increment like a clock (ie, so that it starts over at 12 o'clock instead of going to 13 o'clock) using <> symbols is what I need to do.

Seriously, thank you so much for your help. I understand this a lot better now.