I have a scroll wheel I pulled out of an old mouse. I want to use it as an input device, but I don't know what to write in terms of code. It senses direction by connecting two points to ground in the following pattern one way, and the opposite pattern the other way. I dan't know how to store this data in a way that will allow me to use it as a single data input. I would like the final data to reflect direction and possibly speed, if not just send out a signal/ every time the cycle is repeated. I plan to hook this up to one of Sparkfun's LED grids, and combined with a tiny switch, I want to make a little image editor.
Anyway, here's the pattern:
00
01
11
10
00
01
11
10
00
and so on and so forth. The pattern is flipped for the other direction. I would like to use the data to either add or subtract from a variable, or to set a variable's value relative to either the direction or speed of cycle repetition.
I have no idea how to implement this idea.
Any help is greatly appreciated. Thank you in advance.
The sequence you've shown is a 2-bit Gray code counter. See Gray code - Wikipedia for more specifics. You can map the Gray code values back to a normal counting sequence (0, 1, 2, 3) using a lookup table.
Ok, so what would a lookup table look like in code. I figure it'l be easy to measure speed, just sook for when both are open or closed, but measureing direction is whats troubling me.
If somebody could post a sample code it would be super helpful.
I'm trying to dive headfirst into programming, you know, trial-by-fire kinda thing.
The lookup table above would be indexed by the 2-bit Gray code value and the content at the index will be the "normal" (decimal) equivalent. The table could also be put in code space using the PROGMEM attribute.
Direction is determined by comparing a pair of values. If the current value is greater than the last value, the direction is forward, otherwise reverse. That comparison is not as simple as it sounds, though, because of the "boundary" cases, e.g. last=3, current=0 implies forward. Speed is determined by distance (number of counts) divided by time.
Thats awesome, but I was hoping for a real simple "hello world" kind of pragram to implement the table. I'll look at the code to figure out how it works, but I could really use a contextual example.
Say you name one signal A and the other B the order is of no importance.
If you hold the state of B at the time A changes from low to high this will give you the direction.
If you count the pulses (transition) on A or B this will give you the displacement.