hysteresis on a variable - Avoid constant changes

I want to retrieve an analog voltage from the ADC and display it in a bar of 10 LED's.

Its fairly common that the reading goes back and forward 1 LED and therefore I would like to add some hysteresis.

For example.

Say my average voltage is 5V and each LED (out of 10) represents 1 volt interval.
If the LED were to change from 5V to 6V the voltage would first have to reach 7V for an instant. The same if if were to change to 4V, the voltage would have to reach 3V.

Any ideas?

Say my average voltage is 5V

Let's not.
Let's say the maximum voltage is 5V.

AWOL:

Say my average voltage is 5V

Let's not.
Let's say the maximum voltage is 5V.

No problem, that serves too.

I think what you might be looking for is called "smoothing" where in you take a running average of your readings so even abrupt changes will manifest subtly. There is already an excellent tutorial on this that you might want to check out.

http://arduino.cc/en/pmwiki.php?n=Tutorial/Smoothing

Hope it helps.

Paul

AWOL is correct though, if you try to apply more than 5v into an arduino input your gonna have a bad day.

pguerra75:
I think what you might be looking for is called "smoothing" where in you take a running average of your readings so even abrupt changes will manifest subtly. There is already an excellent tutorial on this that you might want to check out.

http://arduino.cc/en/pmwiki.php?n=Tutorial/Smoothing

Hope it helps.

Paul

AWOL is correct though, if you try to apply more than 5v into an arduino input your gonna have a bad day.

Thanks Paul,

That would add too much delay, sadly. I want to do in code what i would do in hardware with an analogue comparator.

+/- 1 Volt hysteresis is quite large when dealing with 1 V differences in reading. I'd drop that down to maybe +/- 0.25 V. But that all depends on how stable your readings are and how much swing they have.

I'd create two parallel arrays; one containing the low thresholds for each LED and one containing the high thresholds. Then you can use an index variable to keep track of where you are.

Partial code sample, untested:

// thresholds in millivolts
uint16_t high_thresh[] = {1250, 2250, 3250};
uint16_t low_thresh[] = {0, 750, 1750, 2750};
uint8_t index = 0;

void loop()
{
  uint16_t reading = analogRead(whatever);

  if( reading > high_thresh[index] )
    index++;
  else if( reading < low_thresh[index] )
    index--;
}