Hi, I'm pretty new to the arduino and having a bit of trouble with this project I'm working on. My ultimate goal is to wire up all of our cycling teams bike trainers to the computer and monitor everyone speeds and thus be able to race in the comfort of the basement in the winter or bad weather. My original thought was just to use a hall effect sensor and a magnet. However, when I did the math I found that the trainers flywheel will be averaging around 4000-7200rpms and I don't have a good way to mount a magnet that will hold at that speed. So I decided to try to use one of my QRE1113 I/R reflective sensors that I have (The digital output version).
The part of the flywheel that the bike wheel rides on has a small hole for a set screw which is in the perfect spot to mount the QRE1113. So as the flywheel spins, the sensor "sees" a reflection and when the hole passes and doesn't see a reflection. Here is the code I'm using, which works perfect up until about 17 mph / 2720 rpm. I'm not sure what I'm doing wrong or if I have to take a different approach to this. Thanks for your help.
--Chris
int count = 0;
float length = 6.75;
long old_time=0;
float mph = 0.0;
float rpm = 0.0;
int QRE1113_Pin = 3;
int output = 4;//Pin used to trigger interrupt when sensor sees the hole.
void setup() {
Serial.begin(9600);
pinMode(output,OUTPUT);
attachInterrupt(0,RPM_FUN,FALLING);
digitalWrite(output,HIGH);
}
void loop() {
//Read I/R sensor
int sens = readQD();
//Trigger interrupt if I/R senses black
if(sens > 175) {
digitalWrite(output,LOW);
}
//Otherwise keep the output pin HIGH
else digitalWrite(output,HIGH);
//If 50 revolutions went by do rpm/mph math
if(count >= 50) {
float t = (millis() - old_time)/50;
rpm = 60000/t;
rpm = rpm;
mph = ((((60000/t) * 60)*6.75)/12)/5280;
mph = mph;
Serial.println(mph,DEC);
count = 0;
old_time = millis();
}
}
void RPM_FUN() {
count ++;
}
int readQD(){
pinMode( QRE1113_Pin, OUTPUT );
digitalWrite( QRE1113_Pin, HIGH );
delayMicroseconds(10);
pinMode( QRE1113_Pin, INPUT );
long time = micros();
//time how long the input is HIGH, but quit after 250us
while (digitalRead(QRE1113_Pin) == HIGH && micros() - time < 250);
int diff = micros() - time;
return diff;
}