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();
}
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.