Extended and smooth fading time ( 20 minutes or more)

PaulRB:
It sounds like your Arduino pin is "floating". A floating input is unreliable and could read HIGH or LOW at random and cause the flicking you are seeing.

How is the switch wired? Is it a SPDT or a pushbutton?

If SPDT, you could connect one lead to ground, one to 5V and one to the Arduino input pin. But you risk causing a short if you get the leads confused.

Better to treat an SPDT like a pushbutton by using only two of its leads, connected to 5V and the Arduino pin, and a pull-down resistor from the Arduino pin to ground. Better still, connect the switch between the Arduino pin and ground, and use INPUT_PULLUP. Then you don't need a resistor, but you may need to swap HIGH for LOW and LOW for HIGH in your sketch.

Paul

Hi Paul,
I am using a SPDT in input_pullup Configuration. I did the swap from HIGH to LOW, because when the switch is closed the pin 2 is connected to the GND. but still the same. the previous code was working at 50%.