Encoders, Encoders...
Encoders can be used to provide feedback info to your pic to let it know what the mechanics of the machine are doing. In the case of this post, I will be assuming you want to know how far your wheels have gone.
The first guy to look at is this one:
This sensor can also be found here in the componants section.
It is also found at SparkFun Electronics for just $1.13! --These little guys are indeed not only tiny and cheap, but work great for encoding and also line follow applications. If you check out Sparkfun, and other supply houses, you will find this light/sensor set-up comes in a lot of different configurations. Note: the LED portion of this sensor will require a 330R resistor and the input pin should be pulled down with a 10k resistor to gnd (if it isn't allready pulled down on the board itself)
Next is this guy:
These work great when you can straddle a wheel or gear. Again, there is an IR light on one side and a sensor on the other. The same as above, when the sensor sees the light, it allows a high signal to pass through to a digital input pin on your pic. These are also available from Sparkfun and they do offer a PC Board for it. The sensor is just a couple bucks and the board is a dollar. Again, the LED requires a 330R resistor and space for one is included on the PC Board.
Another cool encoder is a rotary unit:
First and Foremost, BaseOverApex has done a nifty post about these in the componant section. I personally have never played with these and I actually have a couple on order right now. I ordered these from Digikey. I am going to shy away from adding anyother info here as Mr. BOA has allready done so in his componant post.
Code:
One thing I found when starting to work with these was this: Figure you have white and black stripes painted on a spinning wheel. The sensor can tell the diference between them. However, your code is cycling faster than the wheel is spinning! Thus, your code could cycle through even a few times while the sensor is still on a white space thus counting it more than once. The solution is simple. The code is basically if the space WAS black and is NOW white then count it. It is not really counting white spaces as it is counting changes from black to white. Here is the basic code I use to do testing:
let b9=0 'start the count at 0
main:
high portc 1 '**Change this to make your motor go
low 1 '** Change this to make your motor go
if pin5=0 then let b8=0 ''if the sensor sees black, remember that (b8 is the "remember" variable)
end if
if pin5=1 and b8=0 then gosub countone 'if the sensor sees white AND it used to be black, count it
goto main
countone:
let b9=b9+1 'count one unit
let b8=1 'switch the "remember" variable to "i'm seeing white now, untill I see black again
if b9=8 then gosub onerev 'if we have counted what we want go somewhere else
return
onerev:
low portc 1 ' **Change this to make your motor stop
low 1 ' **Change this to make your motor stop
pause 2000 'Wait a bit
let b9=0 'Clear the count back to 0
goto main 'Do it all again
https://www.youtube.com/watch?v=WgK42eKXgGE