I have set up some code to read information from the cadence and wheelspeed of a bicycle.
The cadence part is working fine, but the wheelspeed is unresponsive.
I am using a reed switch to capture wheelspeed, and when the reed switch is triggered the speed reads as -0.00
Can anyone advise where I have made a mistake? In lay terms, I am not very advanced in programming.
Thanks!
//////////////////CADENCE VARIABLES/////////////////
int RPM;
long newCtime = 0;
long newLastCt = 0;
long Ctime = 1000; // give this an initial value so that you can calculate debounceDelay
long lastCt = 0;
int HE01 = A0; // this is the analog pin
int HE02 = A1; // this is the analog pin
const int trigNum=840;
const int trigNum2=520;
boolean HE01trig = false;
//////////////////SPEED VARIABLES/////////////////
#define reed A2//pin connected to read switch
int reedVal;
long timer;// time between one full rotation (in ms)
float kph;
float radius = 13.15;// tire radius (in inches)
float circumference;
int maxReedCounter = 100;//min time (in ms) of one rotation (for debouncing)
int reedCounter;
void setup(){
reedCounter = maxReedCounter;
circumference = 2*3.14*radius;
pinMode(reed, INPUT);
Serial.begin(57600);
}
void loop() {
//////////////////CADENCE/////////////////
// collect sensor data
int HE01val = analogRead(HE01);
int HE02val = analogRead(HE02);
// model data
if(HE01val > trigNum && HE01trig == false) {
newCtime = millis() - lastCt;
newLastCt = millis();
HE01trig = true;
}
if(HE02val > trigNum2 && HE01trig == true){
Ctime = newCtime;
lastCt = newLastCt;
RPM = 60000/Ctime;
HE01trig = false;
}
int timeCheck = millis() - lastCt;
if(timeCheck > Ctime){
Ctime = timeCheck;
RPM = 60000/Ctime;
}
//////////////////SPEED/////////////////
long startTimer;
reedVal = digitalRead(reed);//get val of A2
if (reedVal){//if reed switch is closed
if (reedCounter == 0){//min time between pulses has passed
kph = (36*float(circumference))/float(timer);//calculate km per hour
startTimer = millis();
reedCounter = maxReedCounter;//reset reedCounter
}
else{
if (reedCounter > 0){//don't let reedCounter go negative
reedCounter -= 1;//decrement reedCounter
}
}
}
else{//if reed switch is open
if (reedCounter > 0){//don't let reedCounter go negative
reedCounter -= 1;//decrement reedCounter
}
}
timer = millis() - startTimer;
if (timer > 2000){
kph = 0;//if no new pulses from reed switch- tire is still, set kp to 0
}
//Update display
Serial.print("Cadence = ");
Serial.print(RPM);
Serial.print(" ");
Serial.print("Speed = ");
Serial.println(kph);
}