EDIT (June, 25th 2012):
The headache just doesn't go away...
Despite the photo below now I have a tube of black heatshrink shielding the sensor. It increase the analog output, for both cases (black and white). Regardless, the behaviour remains the same as without the shield when I move unto digital pins. I've been trying to deal with debouncing for the past couple of days with no luck.
// Playing with encoder digital readings
const int sensorPin = 7;
int lastColor;
int currentColor;
int count;
long lastDebounceTime = 0; // the last time the output pin was toggled
long debounceDelay = 2; // the debounce time; [0,1] -> crazy bouncing, >= 2 silence!
void setup()
{
Serial.begin(19200);
//pinMode(sensorPin, INPUT); // 7-8 initial bounces on black when lastColor = HIGH;
//digitalWrite(sensorPin, HIGH); // and both these lines are commented.
lastColor = digitalRead(sensorPin);
delay(10);
Serial.print("Starting color ");
Serial.println(lastColor);
//lastColor = HIGH; // HIGH bouncing on white stripes; LOW bouncing on black stripes
}
void loop()
{
readSensor();
if (currentColor != lastColor)
{
count++;
Serial.print("Click! ");
Serial.println(count);
}
} //close loop()
void readSensor()
{
int reading = digitalRead(sensorPin);
if (reading != lastColor){
lastDebounceTime = millis();
}
if( (millis() - lastDebounceTime) > debounceDelay){
currentColor = reading;
}
}
The major implications of this code is by varying:
long debounceDelay = 2; // the debounce time; [0,1] -> crazy bouncing, >= 2 silence!
Also the bouncing will happen with the oposite stripe of the starting one. That is, 1st read is of a white stripe, then whenever a black stripe goes by hundreds of clicks will happen, if debounceDelay is 0 ou 1. However if it's 2 or more.... only the first read is done, then forever silence... :(
So the time has arrived where I've realized I can't go on without the use of wheel encoders. So I've made these:
EDIT: Added TCRT5000-based encoder detector circuit below.
NEW EDIT: Replaced with corrected schematics.
NEW NEW EDIT: Yet again replaced the schematics, HOPEFULLY this time around I didn't leave any mistake on :P
I understand how they are supposed to work, and when I do turn the wheels manually I do get/capture the black stripes lack of reflective power and the otherwise reflective power of the white stripes (rock on \m/)!
I do know, and been throught a number of quadrature posts here and elsewhere, but they only make my head hurt and for the time being I have not need of the advantages they offer, plus I’ve already done this single channel encoder detection apparatus.
So I was trying some code I’ve found in the interwebs:
// sample arduino code for getting relative speed from an encoder
// this code will print the # of encoder ticks each 1/2 second
volatile int counter = 0;
void setup(){
attachInterrupt(0, count, RISING);
Serial.begin(19200);
}
void loop(){
counter=0;
delay(500);
Serial.print(counter);
Serial.print("\n");
}
void count(){
counter++;
}
My beef is that when the motors and consequently the wheels spin I get values in the serial port up to the ~500 and the comment in the code says:
"the # of encoder ticks each 1/2 second"
which either I’m thinking wrong, the wheels speed way faster than I can see or my poor encoding apparatus does not provide the resolution to keep up with the speed… my guess is “all of the above” :/