Knight rider PWM

Hey,
I am new in arduino and for my project i need to make a knight rider LED with PWM. I know the problem in my code (the fading out of the first led is repeated until de second led is at ful 255PWM) but i dont know how to solve this. can sombody help me?

the code:

int led[4] = {3, 5, 6, 9};
int wait = 5;

void setup()
{
for (int i=0; i>=4; i++)
pinMode(i, OUTPUT);
}

void loop()
{
for(int x=0; x<256; x++)
{
analogWrite(led[0], x); Led A ON
delay (wait);
}
}
for(int x=0; x<256; x++)
for(int i=255; i>=0; i--)
{
analogWrite(led[1], x); //Led B ON
delay (wait);
analogWrite(led[0], i); //Led A OFF
delay (wait);
}
for(int x=0; x<256; x++)
for(int i=255; i>=0; i--)
{
analogWrite(led[2], x); //Led C ON
delay (wait);
analogWrite(led[1], i); //Led B OFF
delay (wait);
}
for(int x=0; x<256; x++)
for(int i=255; i>=0; i--)
{
analogWrite(led[3], x); //Led D ON
delay (wait);
analogWrite(led[2], i); //Led C OFF
delay (wait);
}
for(int i=255; i>=0; i--)
{
analogWrite(led[3], i); //Led D OFF
delay (wait);
}
}

Please follow the advice given in the link below when posting code. Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

Your duplicate topic in the Project Guidance section has been closed

Please do NOT cross post / duplicate as it wastes peoples time and efforts to have more than one post for a single topic.

Continued cross posting could result in a time out from the forum.

Could you also take a few moments to Learn How To Use The Forum.

Other general help and troubleshooting advice can be found here.
It will help you get the best out of the forum in the future.

i'm sorry

i wil rad the file for lesrning how to use the forum.

Oops2 (Possibly even Oops3 )

Please remember to use code tags when posting code

i think that last closing brace closes the function

Hello
Do you have some experience by using CPP instruction like STRUCT, ARRAY and/or BaseRangedForLoop?

i have one ARRAY in the code. but CPP and STRUCT i do not konw

Hello
I can´t see your sketch well formated and in code tags.

these loops execute ~65000 times with a 10 msec delay in each execution which means it takes 5+ minutes to complete.

led [1], in this case is set to the same for for each value of i as led [0] is pulsed

wouldn't you want the one LED to be turned off as the other is turned on?

@kajb1996 do you mind if you first read, you how to avoid delay and use millis()
https://werner.rothschopf.net/microcontroller/202109_millis_two_leds_en.htm

and than how to use this for a simple On/Off Knight Rider LED sequence:
https://werner.rothschopf.net/microcontroller/202109_millis_larson_scanner_en.htm

if you confirm, that you understand the millis() concept, I would show up a PWM sketch also
[even if I'm convinced that something like that gets posted every two months...]

the above is also a problem. should be

    for (int i=0; i>=4; i++)
        pinMode (led [i], OUTPUT);

consider (on my hardware, LEDs are active LOW)

#undef MyHW
#ifdef MyHW
byte led[] = { 10, 11, 12, 13 };
#else
byte led[] = {3, 5, 6, 9};
#endif

#define N_LED   sizeof(led)
#define MAX_PWM 256

int i;
int j;

int wait = 2;

void setup()
{
    Serial.begin (9600);
    for (unsigned i=0; i < N_LED; i++)  {
        pinMode      (led [i], OUTPUT);
        digitalWrite (led [i], HIGH);
    }

    i = 1;
}

void loop()
{
    j = i;
    i = (i+1) % N_LED;

    for (int x = 0; x < MAX_PWM; x++) {
        analogWrite (led [i], MAX_PWM - x);
        analogWrite (led [j], x);
        delay (wait);
    }
}

Have another try.

thanks

and

My MobaTools library contains a softled-example for a K.I.T.T. Simulation.
Maybe this is what you want?

I didn't think the original Knight Rider effect (Larson Scanner) had any dimming.

...I've made sound-activated versions of it (where volume affects speed) and I didn't use any dimming. ...Sharing my code would probably just be confusing because I might have a different number of lights/LEDs and I'm using bits in a variable for the LED states. That makes it easier to shift left & right but I have to store the bits that shift off the ends and a function to write-out the bits, etc. To keep things interesting, I also randomly vary the number of LEDs that are on at a time, and how far pattern shifts off the ends.

yes that is exactly what i want.

I think it did -- using lamps, there was a trailing effect that way.

There most certainly was, and a leading effect if you use 6 V (incandescent) bulbs and series resistors.

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.