Independent Duty Cycles for all PWM Pins

I'm looking to use an Arduino UNO to control the brightness of 6 LEDs via the PWM pins separately from each other, i.e. they will all (potentially) have different duty cycle values. I'm sure there are many ways to achieve this but would code like this work or will the time taken to process each line result in some flickering:

void loop()  { 

  analogWrite(led1, brightness1);    
  analogWrite(led2, brightness2);    
  analogWrite(led3, brightness3);    
  analogWrite(led4, brightness4);    
  analogWrite(led5, brightness5);    
  analogWrite(led6, brightness6);    
               
}

(as for setting each brightness variable, I plan on using a rotary encoder & interrupts to adjust them between 0-255)

Apart from having separate duty cycle values, do I even need to have the call to analogWrite() in loop() once the individual brigtnessx values are set? I see this from the definition of the function:

After a call to analogWrite(), the pin will generate a steady square wave of the specified duty cycle until the next call to analogWrite() (or a call to digitalRead() or digitalWrite() on the same pin).

So, let's say I store the brightnessx values in memory and on power up I want to read those values and apply them. Only if I want to change the brightness levels would I use the rotary encoder, so do I need those function calls in loop()?

Apologies for this newbie question - I've mostly worked with PIC microcontrollers but want to switch to Arduino as I can code & program them natively on my Mac.

do I even need to have the call to analogWrite() in loop() once the individual brigtnessx values are set?

No once only is enough.

So set the brightness of each LED by using that first code you posted only put it in the setup function.

Only if I want to change the brightness levels would I use the rotary encoder, so do I need those function calls in loop()?

Yes but what are you using a rotary encoder for?

I've mostly worked with PIC microcontrollers

Virtually the same as the Arduino.

Thanks Grumpy_Mike! By moving the code to Setup it makes my life a lot easier - I can then add code to loop() to check for a button press and then change the duty cycle for the applicable LED (updating memory at the same time so it will start up with the new value next time).

I want to use 1 encoder to vary the brightness for all the LEDs rather than have 1 pot per LED. I have to use an encoder because each LED value could be different, and when I start to change the brightness I want it to start with the current value rather than jump to wherever the pot's setting is. There will be code to handle the lower/upper limits of 0 & 255.

Another technical question - since my project ultimately requires me to control the brightness of 16 LEDs independently, I was planning on just using 2 Arduino Pro Minis but someone suggested TI's TLC5940 as it has 16 channels. What I can't quite figure out yet is whether you can specify different duty cycles for each of the channels - does anyone here know if that's possible?

The TLC5940 can independently control 16 PWM channels.

TLC5940 has 12-bit PWM control.
WS2803 has 8-bit PWM and 18 channels.
Bit simpler to use, just 18 SPI.transfer()s to send the data out.

for (x=0; x<18; x=x+1){
SPI.transfer(brightnessArray[x]); // D13 to clock pin, D11 to data pin
}

for example, with brightnessArray[] holding the 18 levels you want to send out.

Thanks both.

CrossRoads - is there an Arduino library for the WS2803? The one for the TLC5940 makes it easy to use.

there an Arduino library for the WS2803? The one for the TLC5940 makes it easy to use.

As he mentioned use SPI.

Just the SPI library.

#include<SPI.h>
byte brightnessArray[] = {125,125,125,125,125,125,125,125,125,125,125,125,125,125,125,125,125,125,}; // some initial values
byte brightnessChanged;

void setup(){
// send out initial values
SPI.begin();
  for (x=0; x<18; x=x+1){
  SPI.transfer(brightnessArray[x]); // D13 to clock pin, D11 to data pin
  }
// whatever other setup you need ...
}

void loop(){
// read buttons, whatever, to change brightnessArray, then:
brightnessChanged = 1;

if (brightnessChanged ==1){   // "Function? We ain't got no function. We don't need no function. I don't have to show you any stinking function!" 
brightnessChanged = 0; // clear flag
// send out new data
  for (x=0; x<18; x=x+1){
  SPI.transfer(brightnessArray[x]); // D13 to clock pin, D11 to data pin
  }
}

// code to do whatever your program does ...

}  // end loop

Got it - thanks!

Is a WS2803 the same as this?

I don't see a D11 or D13.

Is a WS2803 the same as this?

No. That is just a simple array of darlington transistors.

WS2803 is an 18-Channel Constant Current LED Driver With Programmable PWM Outputs. About as different as you get in electronics.

Ah. So the WS2803 is not available from SparkFun. No worries.