Hi all!
I have developed the following code based on some examples I've found on this forum using interrupts to check for pulses seen on an input pin. This seems to work nicely, but I'm now trying to turn these pulses into a speed value. I now have a speed value but have 2 problems. The first is that I wanted to construct the length traveled per pulse of the tooth wheel using "(1/teeth)/(diffRatio*WheelCirc)" but the 1/teeth part seems to always return a zero. What's the best way of dealing with this calculation?
Furthermore, I'm currently only looking at each pulse on its own. Is there a neat way of averaging maybe the last 10 values on a rolling basis to give a smoother input?
Thanks in advance!
I have developed the following code based on some examples I've found on this forum using interrupts to check for pulses seen on an input pin. This seems to work nicely, but I'm now trying to turn these pulses into a speed value. I now have a speed value but have 2 problems. The first is that I wanted to construct the length traveled per pulse of the tooth wheel using "(1/teeth)/(diffRatio*WheelCirc)" but the 1/teeth part seems to always return a zero. What's the best way of dealing with this calculation?
Furthermore, I'm currently only looking at each pulse on its own. Is there a neat way of averaging maybe the last 10 values on a rolling basis to give a smoother input?
Thanks in advance!
Code:
//Speed
volatile unsigned long VSS_count=0;
unsigned long VSS_prior_count=0;
int pin_VSS = 14;
void pulse() {
VSS_count = VSS_count + 1;
}
int SPEED_MPH;
int SPEED_STEPS;
int SPEED_SEND;
int currentMillis;
int lastMillis;
unsigned long VSS_new_count;
int teeth = 17;
unsigned long diffRatio = 3.42;
int WheelCirc = 2126; //mm
float toothDistance=427701; //(1/teeth)/(diffRatio*WheelCirc); //= 36566.91;
unsigned long elapsedTime;
unsigned long speedRaw;
void setup()
{
Serial.begin(9600);
//toothDistance = (1/teeth)/(diffRatio*WheelCirc);
//---Speed Input Count-----------------------------------
pinMode(pin_VSS, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(pin_VSS), pulse, RISING);
NVIC_SET_PRIORITY(IRQ_PORTA, 0);
}
void loop() {
if (VSS_count != VSS_prior_count) {
elapsedTime = (millis() - lastMillis);
speedRaw = (toothDistance)/(elapsedTime);
VSS_prior_count = VSS_count;
Serial.println(speedRaw/1000);
lastMillis = millis();
}
}
Last edited: