I am working on some gauges for my car. So far i have boost pressure, fuel pressure, oil pressure, and an idiot light system to let me know something is wrong.
The gauges work but they are jittery. I tried a rough smoothing on the oil pressure values and it seems to not be working. the code is really long but here it is.
the jist is three analog reads from three 0-5 volt sensors and each sensor runs a three digit seven seg dispay and they are registering accurate but to fast to read. How can i smooth the analog inputs.
thank you .
p.s. i know the code is sloppy this is a first attempt to get the gauges working.
Create an array that has, say 8 values in it.
Have an integer index into the array.
Initialize the array with the first value in each
location total the values in the array and save
this to a variable.
For the loop part:
Divide that variable by 8 to display.
Each time you read a new value, use the index
to fetch the value there first and subtract it from
the variable. Store the new value at the point and
add it to the variable.
Now increment the pointer and AND it with 0x0F
( for 8 elements ). Loop back.
You then have created a filter. Of sorts. Each input
has a sensitivity of only 1/8 of what is was.
You can make a more complicated filter that scales
each value, depending on how long it was in the filter.
That way you can make newer values have more
more than 1/8 and older values less. This improves
response.
These type filters are called FIR or finite input response.
There is another type called IIR for infinite input response.
Both type are useful but have different problems with
sampled system. Both can be made to have faster response
but along with faster response is more jitter or jumping
around.
You should also have some analog input filtering such that any
input response is some less than 1/2 the sampling frequency
or you may also see noise like effects on the data that can't
be filtered. It should have a minimum of an RC filter. More than
one stage will give sharper cutoff.
In any case, look up sampling theory. Once you get noise from
not band limiting the input, you can't get it out of the data with
digital filters.
Dwight
Also, it looks like you could use the map command and remove a bunch of code.
As I recall, the oil pressure sensor in a car is a pulse type sender.
Don't know why I didn't think about this earlier.
It has a switch contact, a bimetallic piece, resistance wire wrapped around the
bimetal piece and a spring loaded piston from the oil.
The harder the oil pushes on the spring, the longer percentage of time the wire
has to heat the bimetal to open the switch. When the switch opens,
the bimetal piece cools down and the switch closes.
So, reading it with an analog input will be real noisy, since it is really a pulsed
output instead of a continuous output.
The pressure is a percentage of the on to off of the sensor.
You need to be measuring digital time not voltage. I don't know what the
cycle time of the on/off is but I'd think it is on the order of one tenth to a
second someplace so measuring milliseconds would be best.
You'd measure the ratio of on to off. The frequency changes with pressure
as well. It has a higher frequency as the pressure goes up but measuring
the on/off ratio is what the meter on the dash does by having a slow
response time.
If you really wanted to measure with the analog, put an RC filter between
the sensor and the input with a TC of about 1 second.
Dwight