Go Down

Topic: Mouse Scroll Wheel Sensor and Arduino (Read 18601 times) previous topic - next topic

littlemuntainjump

Dec 09, 2009, 08:11 pm Last Edit: Dec 09, 2009, 08:32 pm by littlemuntainjump Reason: 1
Hello guys,
I'm trying to add this scroll wheel sensor to my Arduino project.
Does anybody have any ideas?
I would upload a photo but by now is imposible, there are 3 pins and
there appear to be two different steps, in one all the pins are connected, and in the other they're all disconnected.
This is a problem by the time of knowing in which way the wheel is going.

Osgeld

1 should come on before the other (slightly before you have to really watch it) that is what direction the wheel is spinning
http://arduino.cc/forum/index.php?action=unread;boards=2,3,4,5,67,6,7,8,9,10,11,66,12,13,15,14,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,86,87,89,1;ALL

G_Man

The answer is "Quadrature".  Here have a look at this:  http://en.wikipedia.org/wiki/Rotary_encoder#Incremental_rotary_encoder

The three contacts serve as two bits, with one contact being common.  With two bits, you have 4 possibilities... 00, 01, 10, 11.  The order they appear in tells you the direction.

littlemuntainjump

#3
Dec 10, 2009, 01:03 am Last Edit: Dec 10, 2009, 01:11 am by littlemuntainjump Reason: 1
Thanks to all, your help is very useful,
I'll show you when I get something stable,
all I got for now are some unsuccessful experiments.

westfw

You might find it easier to interface to the whole mouse...

Osgeld

yea but its less fun just pluggin it in and running someone else's lib, or that could be just me

::)
http://arduino.cc/forum/index.php?action=unread;boards=2,3,4,5,67,6,7,8,9,10,11,66,12,13,15,14,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,86,87,89,1;ALL

Tchnclfl

#6
Dec 10, 2009, 02:33 am Last Edit: Dec 10, 2009, 02:34 am by Tchnclfl Reason: 1
Quote
yea but its less fun just pluggin it in and running someone else's lib, or that could be just me


Agreed :).  Half of the fun is mucking through it yourself ;D!

littlemuntainjump

#7
Dec 10, 2009, 11:46 am Last Edit: Dec 10, 2009, 12:29 pm by littlemuntainjump Reason: 1
Yeah that is right, we'll do it from 0.
I'm still trying the same thing no news, now i'll show you the code i've done (it's not supposed to work but has to evolutionate ) see if you know what to change or add:

Code: [Select]

int inPin = 7;   // pushbutton connected to digital pin 7
int inPin2 = 8;

unsigned long duration;
unsigned long duration2;

void setup()
{
 Serial.begin(9600);
 pinMode(inPin, INPUT);
 pinMode(inPin2, INPUT);
}

void loop()
{
 
 duration = pulseIn(inPin,HIGH);   // read the input pin
 
  duration2 = pulseIn(inPin2,HIGH); // read the input2 pin

 if(duration<duration2){
   Serial.print("left\t");
 }
 else{
   Serial.print("\tright");
 }
Serial.print("\n");

}

The final result is a mess of  left and rights appearing rapidly when the pins are in connection mode.
As i've seen in the quadrature article,  this component works by flanks, and not by the duration of the pulses.
Thanks to all

G_Man

How about this one:

Code: [Select]

/*
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;
}


I haven't actually tried it, but it should work.

littlemuntainjump

#9
Dec 10, 2009, 02:25 pm Last Edit: Dec 10, 2009, 02:25 pm by littlemuntainjump Reason: 1
The main problem I have when running this codes is
that when the encoder is in connecting position a stream of
messages comes to the terminal, and when the device is in
disconnecting mode the messages stop. This programming might
be the one but with this problem I can't see anything.
It has to stop automaticly when the users stops moving the wheel
and it falls in connecting position.
I'll try to fix it after lunch.
Thanks a lot.

G_Man

That's odd... the getQuadDir() function will return one of 4 values.  Either 0 (the wheel didn't move), 1 (the wheel moved clockwise), -1 (the wheel moved counterclockwise) or 999 (a pulse was missed).

The loop() function should only print something to the terminal if something has changed (i.e. the wheel has been turned or a pulse was missed).

Now you're going to make me break out an old mouse, tear out the wheel and try it out! ;-)

littlemuntainjump

Ok, there might be a connection problem, the pins are connected 2 to the i/o pins of Arduino, and the other one to 5 V. But when I change this
last pin to GND, same thing happens, Why? Anyway I think 5V is the
right way because can't connect them to 5V and GND in different pins
or it will short when connection mode.
Other idea is connecting a pull down 10kohm resistor like in a usual bottom somewhere.  

G_Man

#12
Dec 10, 2009, 11:55 pm Last Edit: Dec 10, 2009, 11:56 pm by ggutshal Reason: 1
You should connect it like a switch using a pull down resistor perhaps.

Osgeld

Quote
You should connect it like a switch using a pull down resistor perhaps.


This is very true with the mechanical knob type encoders, and I think you are correct, but I have never personally messed with optical encoders (like the one on a mouse wheel) so i cant say for sure

I dont feel like drawing up a new schematic right this second (nor do i have any wheel mice to cut open right now either) but maybe you can take reference from a schematic that i did for a totally unrelated project

http://www.instructables.com/files/orig/FCG/SX6V/FT7PJY5V/FCGSX6VFT7PJY5V.jpg

just ignore the chip connections (a dual flip flop) and button
http://arduino.cc/forum/index.php?action=unread;boards=2,3,4,5,67,6,7,8,9,10,11,66,12,13,15,14,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,86,87,89,1;ALL

littlemuntainjump

#14
Dec 11, 2009, 11:13 am Last Edit: Dec 11, 2009, 12:51 pm by littlemuntainjump Reason: 1
It is not an optical encoder, is mechanical, when the wheel rotates, it changes the position of the 3 pins from connection to disconnection that's all.
I'll try to connect a pull down resistor today to see what happends.

This is the device we are talking about:

   

Go Up