Quadrature Encoders help

I have been Having a really difficult time doing this work as i haven't been able to find a suitable solution. Please help me

I have 2 motors each with quadrature encoders.I am using an Arduino Mega 2560. What i have to do is, when the motor rotates, i want to program in such a way that i get the direction(forward/backward) and the number of revolutions from the Quadrature encoders.

I am pretty much a noob at programming in arduino. will be grateful for the help.

Please tell me what do i do??

Thanks in advance

Start with 1

Then head here: http://playground.arduino.cc/Main/RotaryEncoders

It does help in knowing the direction, but what abt the number of revolutions?? thanks

So for each pulse you increment or decrement a variable depending on the direction.

Well, I haven't looked at it myself - but encoders should provide you with a signal of some sort that tells you how many times a slit in the disk has been passed - count this! if your encoder has say 400 pulses per rev then count/400 is your rev count.

Obviously it get's harder when you're changing direction but that is where the direction toggle will allow you nut out a solution - for instance in one direction subtract counts and the other add ...

That's just my 10secs worth of what makes sense to me, maybe I'm wrong, maybe it's done another way, something more efficient - who knows :)

Did it get you thinking?

It has worked out fine now for 1 motor, Can I use the same code but now for 2 motors. It is possible right?

This is the code i will be using:

int val; int encoder0PinA = 3; int encoder0PinB = 4; int encoder1PinA = 5; int encoder1PinB = 6; int encoder0Pos = 0; int encoder1Pos = 0; int encoder0PinALast = LOW; int encoder1PinALast = LOW;

int n1 = LOW; int n2 = LOW; void setup() { pinMode (encoder0PinA,INPUT); pinMode (encoder0PinB,INPUT); pinMode (encoder1PinA,INPUT); pinMode (encoder1PinB,INPUT); Serial.begin (9600); }

void loop() { n1 = digitalRead(encoder0PinA); if ((encoder0PinALast == LOW) && (n == HIGH)) { if (digitalRead(encoder0PinB) == LOW) { encoder0Pos--; } else { encoder0Pos++; } Serial.print (encoder0Pos); Serial.print ("/"); } encoder0PinALast = n1;

n2 = digitalRead(encoder1PinA); if ((encoder1PinALast == LOW) && (n == HIGH)) { if (digitalRead(encoder1PinB) == LOW) { encoder1Pos--; } else { encoder1Pos++; } Serial.print (encoder1Pos); Serial.print ("/"); } encoder1PinALast = n2; }