Hello guys, its my first time to join into the world of Arduino forums. I just read the thread from the previous Arduino Fourms about quadrature encoders for mouse scroll wheel. Here it is by the way...
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1260385871/0
The scroll wheel in the mouse has typically 3 pins. One is the COM (ground), the other is the A and B that determines the quadrature encoding and its body itself should be connected to Vcc (+5V).
Here is a sample specification sheet of the mechanical encoder:
industrial.panasonic.com/www-data/pdf/ATC0000/ATC0000CE20.pdf
And barely to say, I've uploaded this code to try, but it seems a little buggy.
Here it is:
/*
Quadrature Example
*/
int quadAPin = 1;
int quadBPin = 2;
int lastQuadValue;
int CLOCKWISE = 1;
int CCLOCKWISE = -1;
int MISSEDPULSE = 999;
void setup()
{
lastQuadValue = readQuadValue();
Serial.begin(9600);
}
void loop()
{
int newQuadValue = readQuadValue();
int quadDir = getQuadDir(lastQuadValue, newQuadValue);
if (quadDir == CLOCKWISE) Serial.print("Clockwise");
if (quadDir == CCLOCKWISE) Serial.print("Counter Clockwise");
if (quadDir == MISSEDPULSE) Serial.print("Missed Pulse Detected");
lastQuadValue = newQuadValue;
}
int readQuadValue()
{
int val = digitalRead(quadAPin);
val = val * 2 + digitalRead(quadBPin);
return val;
}
int getQuadDir(int prevVal, int newVal)
{
//because the step order is 0, 1, 3, 2 lets do some switching around
if (newVal == 3)
{
newVal = 2;
}
else
{
if (newVal == 2) newVal=3;
}
if (prevVal == 3)
{
prevVal = 2;
}
else
{
if (prevVal == 2) prevVal=3;
}
int quadDir = prevVal - newVal;
//see if we missed a pulse (i.e. quadDir is 2 or -2)
if (abs(quadDir) == 2) quadDir = MISSEDPULSE;
return quadDir;
}
WHAT'S THE REAL DEAL WITH IT? I need to create a program that counts the number of scrolls and then be printed for example on an LCD. When rotated clockwise, it should count 0, 1, 2, 3, 4... and so on, while if it is rotated counter clockwise, it should be in decreasing order. Here's a sample video of it (but this one uses IR or optical encoder, so it has different pin configurations...)
Please help me with it. I got to have four to five days. Thanks guys!