I'm trying to measure the amount of time that has passed between every time a reed switch mounted on a wheel is activated to calculate the speed of a bike in miles per hour and display it on two 7 segment LEDs. This is what I have so far (without 7 seg code, as that works):
const int reed = A3;
float circumference;
float pi = 3.141592653589
unsigned long time0 = 0;
unsigned long time1;
unsigned long revolutionTime;
void setup() {
Serial.begin(9600);
}
void loop() {
circumference = (pi * 26); //26 = the diameter of the wheel
int reedState = digitalRead(reed);
if (reedState == HIGH) {
time1 = millis();
revolutionTime = (time1 - time0);
time0 = time1;
int mph = ((circumference / revolutionTime) * (3600000 / 63360)); //this is the conversion from inches/millisecond to miles/hour.
Serial.println(mph);
//And I'd print the result on the 7 segment displays here
}
}
This code results in around 25 printed entries in the serial monitor from a single activation of the reed switch, all of which consist of randomly alternating values of either 0 or 4571.
Following each step of the code, I assume that the program would generate a garbage result for the first entry (because the time before the reed is activated for the first time could vary greatly), but that following entries would be the correct mph displayed on the 7 segment displays until the reed switch is activated again and a new mph is calculated.
So how would I go about modifying the code to get useful data?