Hi,
What you are doing is looking at the output that a quadrature encoder would supply.
You need to poll input A,
when it goes HIGH,
you poll input B,
if it is LOW then you must be going forwards, because it hasn't gone HIGH yet.
if it is HIGH, then you must be going backwards, because it must have gone HIGH before A went HIGH.
You need to detect the change in state of A from LOW to HIGH, when it does then check level of B.
The encoder library for Arduino is pretty good for this. It will use interrupts, so if you put input A onto a pin that has a pin-change interrupt (only pin 2 and 3 on the Uno, others have more interrupt pins) then the library will use that and your code doesn't need to keep polling input A.
@Mandalas, let us know how the answers above have failed to help you. What have you tried, what worked differently to how you expected? If you have the same problem then I have already given my answer to that problem.
@zhomeslice, With 295 posts and 66 karma points, I can't imagine why you would be asking that basic question. Translation problem? "State" means the existence of HIGH or LOW on the pin.
MorganS: @zhomeslice, With 295 posts and 66 karma points, I can't imagine why you would be asking that basic question. Translation problem? "State" means the existence of HIGH or LOW on the pin.
Not a question just a way of saying what I already know...
Thanks for pointing this out I Restated it without the "How I look at it" part
Checking the motors direction is easy
At the moment when A goes High if B is High then the motor is turning one way.
Else if B is Low then the motor is turning the other way.
You can also do this assessment when A goes Low
in addition B changes voltage half way between when A voltage change. and so when b changes from low to high you can look at a to see what voltage it is in.