LEDs operating one at a time, not simultaneously

This code controls the brightness of each LED connected to pins 2, 3, 4, and 5 simultaneously. The starting brigthness for each LED is different, as you can see in the PWM[4] array. I confirmed that the PWM values are changing simultaneously in the serial monitor, but in the actual operation, I observed that the LEDs operate only one at a time, not simultaneously. What could be the problem? I apologize if my English is not perfect as I used a translator. Thank you.

int pins_LED[] = {2, 3, 4, 5};
int PWM[4] = {0, 63, 127, 191};

void setup() 
{
  Serial.begin(9600);
  for (int i = 0; i < 4; i++)
    pinMode(pins_LED[i], OUTPUT);
}

void loop()
{
  for (int i = 0; i < 4; i++)
  {
    analogWrite(pins_LED[i], PWM[i]);
    if (PWM[i] > 255) PWM[i] = 0;
    else PWM[i] = PWM[i] + 1;
    
    Serial.print(String(PWM[i]) + " ");
  }
  Serial.println();
}

Which board are you using? Note that on e.g. an Uno pins 2 and 4 are not PWM capable pins.

What happens if you remove the serial prints?

I am using Arduino Mega2560 board, so all 2~5 pin have PWM function.
If I remove Serial prints, the LED brightness changes faster but still happens one at a time.

Here's the video link: https://photos.app.goo.gl/3aYD2pToxL6NRc1SA

Is that with or without the prints? Note that printing takes time once the software buffer fills up.

I think I solved the problem. Deleting the printing and setting the delay shorter made it look clearer. Thank you!

Which delay? There was no delay in your original code in the opening post.

I deleted the printings and found out the changing speed was too fast, so I added delay(7) at the end of the loop.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.