Looking for ideas to eliminate chattering of my relays that are activated depending on the ADvalue that is read from the analog input.

The Analog value is filtered through DT_Analog module (Analog oversampling to 12 bits).

But on the decision point, relays are chattering on/off as the analog value is slow changing.

Code:
gosub getADC
    'ADvalue=abs (ADvalue-818)
    if ADvalue < upper then
        relay.0=1
    else
        relay.0=0
    endif
       
    if ADvalue < mid_upper then
        relay.1=1
    else
        relay.1=0
    endif
    
    if ADvalue < mid then
        relay.2=1
    else
        relay.2=0
    endif
    
    if ADvalue < mid_lower then
        relay.3=1
    else
        relay.3=0
    endif
    
    if ADvalue < lower then
        relay.4=1
    else
        relay.4=0
    endif
Any ideas?

Ioannis