Hi,
I am having trouble getting the debounce function to work. I am using a photoresistor to detect the difference between the black of the rubber and the white lettering on the sidewall of a car tire that is spinning to determine revolutions and ultimately detect speed of wheel. Since the white writing has spaces of black in the lettering the photoresistor picks up on this and jumps back and forth for the readings of black and white multiple times, essentially tricking the wheel counter into thinking that it has turned many more times than it actually has. I figured that the debounce function would be best (better than a delay since I am also going to have to calculate time for speed) to basically ignore jumping around within 1/3 of a second after initially detecting the white lettering.
Any suggestions on what I am doing wrong would be greatly appreciated, as well if there is a better way of doing this. As a note, when the photoresistor is near black it has a value below 125 and when it sees white it goes about 125 (after constraining and calibrating), which I then had it trigger a digitalwrite signal to an LED every time it when above 125 so that the counter can just count LED HIGH signals.
Here is the sketch:
const int photocellPin = 0; // the photoresistor with 10K pulldown to A0
const int LEDpin = 2; // connect LED to pin 2 so that interrupt 0 works
const int wheeldiameter=0.002030641; //in miles
#include <LiquidCrystal.h>
const int rs = 12, en = 11, d4 = 5, d5 = 7, d6 = 3, d7 = 6;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
#include <Bounce2.h>
Bounce debouncer1=Bounce();
int sensorValue = 0; // the photoresistor value
int sensorMin = 1023; // minimum sensor value
int sensorMax = 0; // maximum sensor value
unsigned int wheelcounter=0;
const float pulsesPerMile=492.46;
float wheelfrequency;
float wheelspeed;
float wheeldistance=0;
const float convertMph=pulsesPerMile/492.46; // the wheel rotates this many times to equal 1 mile distance
float mph;
void setup(void) {
pinMode(LEDpin, OUTPUT);
debouncer1.attach(LEDpin);
debouncer1.interval(333);
lcd.begin(16, 2);
lcd.setCursor(0,0); // Sets the cursor to col 0 and row 0
lcd.print("Wheel Speed"); // Prints to LCD
lcd.setCursor(0,1); // Sets the cursor to col 1 and row 0
lcd.print("Calibrating Light"); // Prints to LCD
Serial.begin(9600);
// turn on LED to signal the start of the calibration period:
pinMode(13, OUTPUT);
digitalWrite(13, HIGH);
// calibrate during the first five seconds
// calibration from
https://www.arduino.cc/en/Tutorial/Calibration while (millis() < 5000) {
sensorValue = analogRead(photocellPin);
// record the maximum sensor value
if (sensorValue > sensorMax) {
sensorMax = sensorValue;
}
// record the minimum sensor value
if (sensorValue < sensorMin) {
sensorMin = sensorValue;
}
}
// signal the end of the calibration period
digitalWrite(13, LOW);
lcd.clear();
}
void loop(void)
{
Serial.print(" Photo Resistor Value ");
// read the sensor:
sensorValue = analogRead(photocellPin);
// apply the calibration to the sensor reading
sensorValue = map(sensorValue, sensorMin, sensorMax, 0, 255);
// in case the sensor value is outside the range seen during calibration
sensorValue = constrain(sensorValue, 0, 255);
Serial.println(sensorValue);
// if statement to detect black or white on wheel:
if (sensorValue < 125) //Threshold value set in middle of calibrated value, set in middle of 0-255 range to trigger either white or black
digitalWrite (LEDpin,LOW);
else
{digitalWrite(LEDpin, HIGH);
wheelcounter=wheelcounter+1;
}
Serial.println(wheelcounter);
lcd.setCursor(0,0); // Sets the cursor to col 0 and row 0
lcd.print("Distance: "); // Prints to LCD
lcd.print(wheeldistance); // Prints value to LCD
lcd.setCursor(0,1); // Sets the cursor to col 1 and row 0
lcd.print("Speed (mph): "); // Prints to LCD
lcd.print(mph); // Prints value to LCD
delay(10);
}