Hello
This is my sketch for your project Knight rider PWM. This sketch has been tested with an Arduino Uno using six led and works well. May you need to make changes inside the ledseq array to change the light behaiviour.
//BLOCK COMMENT
// https://forum.arduino.cc/t/knight-rider-pwm/902477
#define ProjectName "Knight rider PWM"
// CONSTANT DEFINITION
// you may need to change these constants to your hardware and needs
constexpr byte Output_[] {3, 5, 6, 9, 10, 11};
constexpr byte ledSeq[][sizeof(Output_)] {
{255 >> 0, 255 >> 2, 255 >> 3, 255 >> 4, 255 >> 5, 255 >> 6},
{255 >> 1, 255 >> 0, 255 >> 4, 255 >> 5, 255 >> 6, 255 >> 7},
{255 >> 2, 255 >> 1, 255 >> 0, 255 >> 6, 255 >> 7, 255 >> 8},
{255 >> 3, 255 >> 2, 255 >> 1, 255 >> 0, 255 >> 8, 255 >> 8},
{255 >> 4, 255 >> 3, 255 >> 2, 255 >> 1, 255 >> 0, 255 >> 8},
{255 >> 5, 255 >> 4, 255 >> 3, 255 >> 2, 255 >> 1, 255 >> 0},
{255 >> 6, 255 >> 5, 255 >> 4, 255 >> 3, 255 >> 2, 255 >> 0},
{255 >> 7, 255 >> 6, 255 >> 5, 255 >> 4, 255 >> 0, 255 >> 1},
{255 >> 8, 255 >> 7, 255 >> 6, 255 >> 0, 255 >> 1, 255 >> 2},
{255 >> 8, 255 >> 8, 255 >> 0, 255 >> 1, 255 >> 2, 255 >> 3},
{255 >> 8, 255 >> 0, 255 >> 1, 255 >> 2, 255 >> 3, 255 >> 4},
{255 >> 0, 255 >> 1, 255 >> 2, 255 >> 3, 255 >> 4, 255 >> 5},
};
// VARIABLE DECLARATION AND DEFINTION
unsigned long currentTime;
unsigned int sequence;
struct TIMER {
unsigned long duration;
unsigned long stamp;
};
TIMER blink_{75, 0};
// FUNCTIONS
bool checkTimer(TIMER & time_) {
if (currentTime - time_.stamp >= time_.duration) {
time_.stamp = currentTime;
return true;
} else return false;
}
void setup() {
Serial.begin(9600);
Serial.println(F("."));
Serial.print(F("File : ")), Serial.println(__FILE__);
Serial.print(F("Date : ")), Serial.println(__DATE__);
Serial.print(F("Project: ")), Serial.println(ProjectName);
for (auto Output : Output_) pinMode(Output, OUTPUT);
}
void loop () {
currentTime = millis();
if (checkTimer(blink_)) {
int element = 0;
for (auto led : Output_) analogWrite(led, ledSeq[sequence][element++]);
sequence++;
sequence = sequence % (sizeof(ledSeq) / sizeof(ledSeq[0]));
}
}
Have a nice day and enjoy coding.