Need advice: smoothing Pulsein results

I'm building a circuit to interpret the tach signal from a Honda Civic and drive a servo. The servo will turn the needle of a 3' diameter tachometer, which will be on the roof our our 24 Hours of LeMONs car. Original post here: http://arduino.cc/forum/index.php/topic,55381.0.html

I have everything laid out and it is generally working, in that the servo position generally climbs with the vehicle RPM, but behaves jumps around a lot. Looking at the serial monitor, I'm getting Pulsein values of around 32000 at idle, and 1200 at high RPM. Those values are inconsistent however. At idle, I'll get 32000, 30000, 34000, 25000, 2200, 32000, 2330, etc...

Unfortunately the freq counter and servo libraries cannot coexist, from what I've read.

Do I need a function to smooth the data? Anyone see anything glaring in the code?

What I got this far:

Voltage divider circuit (as described in original post) to convert 12V pulse to a 5V pulse.
5V DC-DC regulating power supply - grounded/powered by vehicle.
Arduino- GND pin grounded to powersupply/vehicle, but powered on the bench by USB for the serial monitor (is this a grounding problem?)

This simple code:

#include <Servo.h>
 
Servo tach;             // create servo object to control tach needle
int pin = 7;            // tach signal pin
unsigned long duration; // variable to store tach signal  

void setup()
{
  Serial.begin(9600);
  tach.attach(9);    // attaches the servo on pin 9 to the servo object
  pinMode(pin, INPUT);
}

void loop()
{
  duration = pulseIn(pin, HIGH);  // pulsein, pin 7 input, waits for it to go high, times out in 1000ms
  map(duration,500,30000,180,0);          // map pulsein duration 
  tach.write(duration);                     // move needle to position
  Serial.println(duration);            // debug pulsein
  delay(100);                         // arbitrary delay = need to tune this to servo resolution
}

Thanks in advance for advice.

Look in the MATH section here: http://arduino.cc/playground/Main/GeneralCodeLibrary

If you need to smooth, see this tutorial...

Your values have a > 10:1 range, I think that's a bit beyond smoothing. You need to look into the cause of that problem.

What do you get if you print duration before the map()?


Rob

#include <Servo.h>
 
Servo tach;             // create servo object to control tach needle
//int pos = 0;            // variable to store the servo position
int pin = 6;            // tach signal pin
unsigned long duration; // variable to store tach signal  

void setup()
{
  Serial.begin(9600);
  tach.attach(6);    // attaches the servo on pin 9 to the servo object
  pinMode(pin, INPUT);
}

void loop()
{
  duration = pulseIn(pin, HIGH);  // pulsein, pin 7 input, waits for it to go high, times out in 1000ms
  //constrain(duration,0,5000);          // constrain pulsein duration to expected values
  //map(duration,0,5000,0,180);          // map pulsein duration 
  //tach.write(pos);                     // move needle to position
  Serial.println(duration);            // debug pulsein
  //Serial.println(pos);                 // debug servo position
  delay(1000);                         // arbitrary delay = need to tune this to servo resolution
}

Here is the data captured from Pulsein in one second intervals:

0
0
<engine start, idle>
99
1741
1885
2064
31594
1807
2272
2022
2115
2040
2401
1955
40423
2255
2240
2341
2052
2023
2181
2269
1875
1830
2009
1943
1831
1922
1733
1990
<rev to 5k rpm and hold>
1646
1146
6391
957
990
2
1014
1201
938
965
1149
1226
1137
1232
1036
6383

2069
1904
2332
2165
2144
2005
38786
1813
2089
2007

I'd try to get a scope on that and see if I could characterise the noise.

Unfortunately, I don't have an o-scope. My meter can measure Hz, and I'm getting the expected frequency before and after the voltage divider circuit.

I may have to try a different approach, as I have about a 2 week deadline. Freqcouter and servo libraries don't play well together, which is why I'm trying to do pulsein.

I may be able to pull off an analog input from the cars gauge. I'm not sure how the needle in the dash is actuated.

I appreciate the advice.