Basically I just printed out one of these (laser printer recommended):
...And attached it to a wheel (double sided tape recommended).
Then I hooked up one of these (a 2$ IR sensor: QRB1134):
...And attached it to the motor pointing towards the wheel.
I've been testing it a bit with a few lines of Arduino code and damnit it works :D I'm now able to measure how much the wheel is rotating and therefore calculate how far a robot is moving. I just love it when these cheap lowtech solutions work.
Here are some photos (sorry about the bad quality):
UPDATE:
Here is the schematic I used to hook up the QRB1134 sensor:
I used a 0.1uF ceramic cap. Don't know if that's what was intended? It does have a + indicating a polarized cap, but I dunno? Man I wish people would write the kind of cap you're supposed to use, but apparently that's obvious to everyone but me :/ If anyone has a suggestion of what to use I'm open?
Here is the Arduino code I used for testing:
#define IOP 14
#define PWM 3
int val_new;
int val_old;
int clicks = 0;
int turns = 0;
void setup() {
Serial.begin(115200);
pinMode(IOP, INPUT);
val_new = digitalRead(IOP);
val_old = val_new;
}
void loop() {
analogWrite(PWM, 80);
val_new = digitalRead(IOP);
if(val_new != val_old) {
if(clicks == 40) {
clicks = 1;
turns++;
Serial.print("TURNS: ");
Serial.println(turns);
}
else clicks++;
Serial.print("CLICKS: ");
Serial.println(clicks);
val_old = val_new;
}
}
Basically I just add 1 to the variable click every time the color in front of the sensor changes. When I reach 40 clicks I reset the variable click and add 1 to the variable turns. And off course I'm logging everthing through serial for testing. That's it :)