I am trying to make a tachometer for my car.
The car ecu puts out at 5v square wave with each ignition pulse. (4 per revolution)
I thought I could use the tutorials for the IR computer fan tachometer, but I dont understand the code.
When I run the pulses too high, the terminal window gives me negative numbers.
I am using Picaxe 28X2 shield to provide the pulses, so at this point they are free from ignition noise while I figure out how to program it.
Here is the current code, Can someone explain it to me and help me get it to not give negative numbers?
Thanks in advance
Cory
Code:
volatile float time = 0;
volatile float time_last = 0;
volatile int rpm_array[5] = {0,0,0,0,0};
void setup()
{
Serial.begin(115200);
//Digital Pin 2 Set As An Interrupt
attachInterrupt(0, fan_interrupt, FALLING);
Serial.println("Current RPM:");
}
//Main Loop To Calculate RPM and Update LCD Display
void loop()
{
int rpm = 0;
while(1){
//Slow Down The LCD Display Updates
delay(400);
//Update The Rpm Count
Serial.println(rpm);
////lcd.setCursor(4, 1);
////lcd.print(time);
//Update The RPM
if(time > 0)
{
//5 Sample Moving Average To Smooth Out The Data
rpm_array[0] = rpm_array[1];
rpm_array[1] = rpm_array[2];
rpm_array[2] = rpm_array[3];
rpm_array[3] = rpm_array[4];
rpm_array[4] = 60*(1000000/(time*7));
//Last 5 Average RPM Counts Eqauls....
rpm = (rpm_array[0] + rpm_array[1] + rpm_array[2] + rpm_array[3] + rpm_array[4]) / 5;
}
}
}
//Capture The IR Break-Beam Interrupt
void fan_interrupt()
{
time = (micros() - time_last);
time_last = micros();
}
Code: