Alright here is what I have... The exact code above, but added some serial LCD commands so I could view things on a small LCD.
When I have a push button (with debouncing) hooked up to the interrupt pin (digi pin 2), all seems to work great. If I push the button wait about 1 sec press again, my LCD outputs roughly 60 RPM. If I wait two seconds and press the button, it output 30 RPM. So it seems my basic math is working great.
So it was on to the vehicle. I built my little tach filter (see design above), and got things wired into my vehicle. I used an o-scope to make sure my signals were good, and verified that the output on the filter looked good, which it was, as it was a pretty much a perfect sq wave 5V output, with a freq change as RPM increased or decreased.
Figured all was good to hook up the arduino with my program on it. Thats when things went kinda south... I started getting an RPM reading of -5536, or -2, and very rarely 30,000. The -5536 and the -2 was pretty random, but those were the only 3 values it would output, nothing else.
So I bring the whole setup back in, hook up my switch to the interrupt and everything outputs great. Please let me know if you have any suggestions or would like to see any information. I will re-attach my current code.
I will be tinkering some more tomorrow, do some debugging and get back to you all with my findings.
Thanks,
Chris
// Program to count pulses per revolution in an automobile
// TODO: Includes a running average to insure proper RPM output
// TODO: Output a clean averaged 5v Sq wave of RPM
// NOTE: May need to go to (us) as opposed to (ms) for better resolution
int Cycle = 0; // set to 0 for PulseStartTime and set to
// 1 for PulseEndTime
unsigned long PulseStartTime; // Saves Start of pulse in ms
unsigned long PulseEndTime; // Saves End of pulse in ms
unsigned long PulseTime; // Stores dif between start and stop of pulse
unsigned long RPM = 0; // RPM to ouptut (30*1000/PulseTime)
void setup()
{
Serial.begin(9600); // OPENS SERIAL PORT SETS DATA TO 9600 bps
Serial.print(0x0B,BYTE); // LCD Setup
Serial.print(0x40,BYTE); // LCD Setup
delay(100);
Serial.print(0x1F,BYTE); // LCD Setup
Serial.print(0x28,BYTE); // LCD Setup
Serial.print(0x61,BYTE); // LCD Setup
Serial.print(0x40,BYTE); // LCD Setup
Serial.print(0x01,BYTE); // LCD Setup
Serial.print(0x0C,BYTE); // LCD Setup
delay(100);
attachInterrupt(0, RPMPulse, RISING); // Attaches interrupt to Digi Pin 2
}
void RPMPulse()
{
if (Cycle == 0) // Check to see if start pulse
{
PulseStartTime = millis(); // stores start time
CycleOnOrOff = 1; // sets counter for start of pulse
return; // a return so it doesnt run the next if
}
if (Cycle == 1) // Check to see if end pulse
{
detachInterrupt(0); // Turns off inturrupt for calculations
PulseEndTime = millis(); // stores end time
Cycle = 0; // resets counter for pulse cycle
calcRPM(); // call to calculate pulse time
}
}
void calcRPM()
{
PulseTime = PulseEndTime - PulseStartTime; // Gets pulse duration
Serial.print("PulseTime ="); // Output pulse time for debug
Serial.print(PulseTime); // Pulse debug output
Serial.print(" ");
RPM = 30*1000/PulseTime*2; // Calculates RPM
attachInterrupt(0, RPMPulse, RISING); // re-attaches interrupt to Digi Pin 2
}
void loop()
{
Serial.print("RPM = "); // Output RPM for debug
Serial.print(int(RPM)); // RPM debug output
Serial.print(" ");
delay(1000); // 1 sec delay for debug output
Serial.print(0x0C,BYTE); // Clear LCD screen cmd
}