// SoftPWM on a Arduino Nano
// This Wokwi project: https://wokwi.com/projects/392905991308708865
// Library: https://github.com/bhagman/SoftPWM
//

#include <SoftPWM.h>

#define NUM_LEDS 18

const int leds[NUM_LEDS] = {2,3,4,5,6,7,8,9,10,11,12,13,A0,A1,A2,A3,A4,A5};
const int speedPin = A6;  // A6 and A7 are analog input only

int index = 0;
int direction = 1;


void setup()
{
  // Initialize the library. 
  // A hardware timer with interrupt is used.
  SoftPWMBegin();

  for(int i=0; i<NUM_LEDS; i++)
  {
    SoftPWMSet(leds[i], 0);
  }

  // Set the fade-up time and the fade-down time.
  SoftPWMSetFadeTime(ALL, 0, 400);
}


void loop()
{
  int potValue = analogRead(speedPin);

  SoftPWMSet(leds[index], 255);
  delay(30+(1023-potValue)/5);
  SoftPWMSet(leds[index], 0);

  index += direction;
  if(index == 0 or index == NUM_LEDS-1)
    direction = -direction;
}
speed