Having trouble with 10 LED Fading Driver Sketch

Hey there,

The goal of this sketch is to create 2 seperate 5 LED indicator bars for two control voltages signal between 0-5V. I would love to incorporate a fade function for indicating whether the control voltage is between a single digit voltage value.

Psudo example: analogRead = 2.5V, then led1= ON, led2= ON, led3= half brightness

I am using an ATMEGA328 and that chip does not provide enough PWM pins for 10 LEDs so I am using this fade sketch to change the brightness of 10 LEDs:

  if(potVoltageRange1 == 0){                             // the led doesnt turn all the way off so to make sure it turns off
  digitalWrite(led1, LOW);                               // when the pot value is 0, turn the led completely off
  } else {                                               // but if its not 0, then do this instead
  digitalWrite(led1, HIGH);                              // turn the LED on
  delayMicroseconds(led1FadeValue);                      // time delay using the pot value

  digitalWrite(led1, LOW);                               // turn the LED off
  delayMicroseconds(1024 - led1FadeValue);               // time delay using the value of 1024 minus the pot value 
  }                                                      // this creates a duty cycle causing the LED to "dim"
  }                                                      // end fade animation

What I need help with is the logic for the control voltage to sweep between LEDs 1-5 as well as control the brightness of each one along the sweep.

I am using a B100K potentiometer as a voltage divider between 0-5V to mimic the control voltage signal.

Note: I have just started to code one 5 LED driver to start and then duplicate when I get it right.

Below is the code I have written so far:

  // psudo explination
  //---------------------------
  // between value 0 and 1 do these things:
  // make the pot control the fade of the led1 from off to full brightness
  // turn off leds 2,3,4, and 5 to show we are in the first range value = 0
  //---------------------------
  // ok so now between value 1 and 2 do these things:  
  // make the pot control the fade of the led2 from off to full brightness
  // turn off leds 3,4, and 5 and turn on led1 on to show we are in the first range value = 1
  //---------------------------
  // this pattern repeats for the rest of the ranges
  //---------------------------

int led1 = 12;                                        //pin 12 for LED1
int led2 = 11;                                        //pin 11 for LED2
int led3 = 10;                                        //pin 10 for LED3
int led4 = 9;                                         //pin 9 for LED4
int led5 = 8;                                         //pin 8 for LED5

int ledValRange1;
int led1FadeValue;
int led2FadeValue;

int pot1 = A0;                                        //pin A0 for potentiometer
int potVal1;                                          //raw potentiometer data between 0-1023
int potVoltageRange1;                                 //mapped potentiometer data between 0-4 for control voltage range 0-5V


void setup()                                          //run setup
{
  pinMode(13, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(11, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(9, OUTPUT);
  
  pinMode(A0, INPUT);                                   //control voltage input 1

  Serial.begin(9600);                                   //just to make sure we can see where data is being recieved in the loop

}

void loop() {                                            // begin main loop
   
  potVal1 = analogRead(pot1);                            // read pot value               
  potVoltageRange1 = map(potVal1, 0, 1023, 0, 500);      // map pot value to 0-4 so we have a range of 5 for 0-5V
  Serial.print(potVoltageRange1);                        // print the range between 0-4

//-------------------------------------------------------

  if(potVoltageRange1 <= 100){                           // do this thing while the pot reads between 0 and 1                                            
  led1FadeValue = map(potVal1, 0, 100, 0, 1023);         // map the raw pot value from 0 to 100 to 0 - 1023 for fade animation                                                       
  fade();                                                // need to change variables in fade so they can be used by the other leds                                                    
  }                                                      // end pot range 0-1 conditions and animation
  
  }                                                      // end main loop

void fade(){                                             // begin fade animation

  if(potVoltageRange1 == 0){                              // the led doesnt turn all the way off so to make sure it turns off
  digitalWrite(led1, LOW);                               // when the pot value is 0, turn the led completely off
  } else {                                               // but if its not 0, then do this instead
  digitalWrite(led1, HIGH);                              // turn the LED on
  delayMicroseconds(led1FadeValue);                      // time delay using the pot value

  digitalWrite(led1, LOW);                               // turn the LED off
  delayMicroseconds(1024 - led1FadeValue);               // time delay using the value of 1024 minus the pot value 
  }                                                      // this creates a duty cycle causing the LED to "dim"
  }                                                      // end fade animation

There are software PWM libraries.
This one is a little weird but once you have read the documentation it can do a lot: GitHub - Palatis/arduino-softpwm: Software PWM library for Arduino.