LED Series Fade with Pot

Hey guys,
Trying to use a pot to fade a series of LEDS from min-max, one by one, as the pot goes up, each led hits max value, it holds the max value and the next LED begins its fade up.
Not sure on how to get the previous LED to maintain it’s max value, as currently when I turn the pot up, as soon as LED1 hits max value, LED 1 & 2 drop to min value and both start a new fade. Then when LED 1 & 2 hit max, LED 1,2,3 drop to min value and begin the next fade and so on. I have some variables and terms I’ve tried using like currentLED and pastLED but not sure how to utilise them. Thanks for assistance. Attached is circuit schematic

int timer = 2;                       // delay in milliseconds
int ledPins [] { 5, 6, 9, 10, 11};   // led array output to PWM pins
int pinCount = 5;                    // amount of pins
const int potInput = A0;             // Pot input on pin A0
int outputValue = 0;                
int sensorInput = 0;
int pwmRatio = 0;
int currentLED = 0;
int pastLED = 0;

void setup() {                               
for (int pin = 0; pin < pinCount; pin++) {    
  pinMode (ledPins[pin], OUTPUT);              // set Led array as Output
  }
  Serial.begin(9600);
}

void loop() {
     sensorInput = analogRead(potInput);
     outputValue = map(sensorInput, 0, 1023, 0, 255);   // Read  input and map to led output values 
     currentLED = sensorInput /5 ;
     pwmRatio = sensorInput * 1.25;
for (int pin = 0; pin < pinCount; pin++) 
     {     
    if ((pin * 51) < outputValue)                       // Used 255 divided by number of outputs (5)
    {
      analogWrite(pin[ledPins], pwmRatio);
    } 
    else
    {
      digitalWrite(pin[ledPins], LOW);                // pins above value don't turn on yet
    }
  }
   
   Serial.println(sensorInput);
}

I do wonder why people decide to use potentiometers to do this sort of thing!

Is it because the Arduino tutorials give an example for using a potentiometer?

Nowadays - as is the case with professional instrumentation - it is far more practical (and significantly more reliable) to use a rotary encoder.

Do not map your pot to a value of 0 to 255, you are mixing up brightness with your pot range.

Instead split your pot range into 5 equal size intervals. Then go through each led and turn on at full brightness all those that are associated with the ranges below your current reading and turn off those associated with ranges higher than your current range.

Then find the range your pot is reading and set the brightness of that led to a value equal to how far through the range it is.

For example suppose you are half way through the range the you would set the led to half brightness. Of course you do that mathematically, see if you can work out the simple equation that will do that.

Paul__B:
Is it because the Arduino tutorials give an example for using a potentiometer?

Haha, exactly that! And i’m using the duinotech starter kit from Jaycar. I’ll have to check out encoders for my next project.

And thanks for the input Grumpy_Mike! The kicker was as you said, mapping the output to 5 intervals, not 255, which left me with 4 intervals.
A mate and I put our heads together and finally figured it out.
This code works exactly as I want; a series of LEDS that each fade from min-max one by one.

int ledPins [] { 5, 6, 9, 10, 11};   // led array output to PWM pins
int pinCount = 5;                    // amount of pins
const int potInput = A0;             // Pot input on pin A0
int outputValue = 0;                
int sensorInput = 0;
int pwmRatio = 0;
int currentLED = 0;
int pastLED = 0;
int pwmRatio2 = 0;

void setup() {                               
for (int pin = 0; pin < pinCount; pin++) {    
  pinMode (ledPins[pin], OUTPUT);              // set Led array as Output
  }
  Serial.begin(9600);
}

void loop() {
     sensorInput = analogRead(potInput);
     outputValue = map(sensorInput, 0, 1023, 0, 255);   // Read  input and map to led output values 
     currentLED = (sensorInput/ 204);
     pwmRatio = (sensorInput % 204);
  
     Serial.println(currentLED);
     Serial.println(pwmRatio);

   if(currentLED < pastLED){ 
    for (int pin = 0; pin < pinCount; pin++) 
     {     
    digitalWrite(ledPins[pin], LOW);
     }
   }
   analogWrite(ledPins[currentLED], pwmRatio);
   
   for (int pin = 0; pin < currentLED; pin++){
      digitalWrite(ledPins[pin], HIGH);
   }
   pastLED = currentLED;
   Serial.println(sensorInput);
}