I have a high power LED driver board; which is responsible for turning on a High power led ; in this board there is 10K ohm potentiometer; which is responsible for dimming the light of LED; this part was in SMD package; which I turned it to a 10K ohm DIP version and this potentiometer has 5 pin which from other pins I use to on/off the LED (5 pin led with switch); also based on potentiometer values I designed a LED indicator board which is responsible to read the the output pin of potentiometer and than based on mapped value turns led 1 to 6 ; for this board I used atmega 328p; I have the code as below; which when I turn of the potantiometer; it will turn off indicator leds; but this works randomly; some times it will work but some times it will not work and when I press the reset key on the at mega it will turn off LED indicator led one please check the atmega328p code; and help me to improve it so it will check the led one(when we turn potentiomer at low to activate switch function for on and off ; it is logical to led one is activated and then when we switch it off ; led one also need to be turned off) and make sure that the led one is off(the code I have works fine but some times it dose not work and led one will be turned on until I press reset key in the Indicator and atmega board )
#define F_CPU 16000000UL

#include <avr/io.h>
#include <util/delay.h>
#include <stdlib.h>

#define potPin 0

#define led1 PD1
#define led2 PD2
#define led3 PD3
#define led4 PD4
#define led5 PD5
#define led6 PD6

#define SAMPLES_TO_AVERAGE 10
#define HYSTERESIS 25
#define OFF_THRESHOLD 10  // Improved detection definition for “Off” scenario

void adc_init() {
    ADMUX = (1<<REFS0); // AVCC reference
    ADCSRA = (1<<ADEN) | (1<<ADPS2) | (1<<ADPS1) | (1<<ADPS0); // Enable ADC, set prescaler to 128
}

uint16_t adc_read(uint8_t ch) {
    ADMUX = (ADMUX & 0xF0) | (ch & 0x0F);
    ADCSRA |= (1<<ADSC); // Start conversion
    while (ADCSRA & (1<<ADSC)); // Wait for completion
    return ADC;
}

uint16_t adc_read_average(uint8_t ch) {
    uint32_t sum = 0;
    for (int i = 0; i < SAMPLES_TO_AVERAGE; ++i) {
        sum += adc_read(ch);
    }
    return (uint16_t)(sum / SAMPLES_TO_AVERAGE);
}

int main(void) {
    DDRD |= (1<<led1) | (1<<led2) | (1<<led3) | (1<<led4) | (1<<led5) | (1<<led6);
    adc_init();

    uint16_t last_potValue = 0xFFFF;

    while(1)
    {
        uint16_t potValue = adc_read_average(potPin);
        
        if (abs((int)potValue - (int)last_potValue) > HYSTERESIS) {
            last_potValue = potValue;

            if (potValue < OFF_THRESHOLD) {
                PORTD &= ~((1<<led1) | (1<<led2) | (1<<led3) | (1<<led4) | (1<<led5) | (1<<led6)); // Turn off all LEDs
            } else {
                uint16_t mappedValue = (uint32_t)(potValue - OFF_THRESHOLD) * 1023 / (1023 - OFF_THRESHOLD);

                PORTD &= ~((1<<led1) | (1<<led2) | (1<<led3) | (1<<led4) | (1<<led5) | (1<<led6)); // Reset LEDs

                if (mappedValue >= 400) {
                    PORTD |= (1<<led1) | (1<<led2) | (1<<led3) | (1<<led4) | (1<<led5) | (1<<led6);
                } else if (mappedValue >= 333) {
                    PORTD |= (1<<led1) | (1<<led2) | (1<<led3) | (1<<led4) | (1<<led5);
                } else if (mappedValue >= 265) {
                    PORTD |= (1<<led1) | (1<<led2) | (1<<led3) | (1<<led4);
                } else if (mappedValue >= 200) {
                    PORTD |= (1<<led1) | (1<<led2) | (1<<led3);
                } else if (mappedValue >= 132) {
                    PORTD |= (1<<led1) | (1<<led2);
                } else {
                    PORTD |= (1<<led1);
                }
            }
        }

        _delay_ms(30);
    }
}