Multiplexed PWM

Hey gang, I’m working on controlling about 50 LEDs setup to flicker like a candle using PWM. I thought about using a whole bunch of TINY13s, but would like to try multiplexing the arduinos 6 PWM outputs first. I tried software PWM, but evidently I suck at that, and can’t get it to work at all, so hardware PWM it is. The code I’m using for the flicker I found here posted by jerrya.net and it’s very convincing stuff, but I think I’m screwing something up with the multiplexing part. Instead of a nice smooth transition, it gets very jerky and flashy and some of the digital IO lines just dont seem to work at all. Any thoughts?

/* Multiplexed PWM LED candles

  • Using digital IO pins as ground, this can multiplex up to 84 LEDs
    */

// Array of PWM pins.
int PWMPin = {
3, 5, 6, 9, 10, 11};
// Array of digital pins.
int DigPin = {
0, 1, 2, 4, 7, 8, 12, 13, 14, 15, 16, 17, 18, 19};

int TotalPWM = 6; // Total number of PWM pins, is there a lenof function in here somewhere?
int TotalDigital = 14; //Total number of digital pins
int PWMValue[84]; //The PWM value of each LED (TotalPWM * TotalDigital)
long randNumber; //A random number

void setup() {
for (int i=0; i<=TotalPWM; i++){ // set PWM pins as output
pinMode(PWMPin*, OUTPUT);*

  • }*
  • for (int i=0; i<=TotalDigital; i++){ // set Digital pins as output*
    _ pinMode(DigPin*, OUTPUT);_
    _
    }_
    _
    randomSeed(analogRead(0)); // seed the rnd generator with noise from unused pin*_
    * for (int i=0; i<=TotalPWM; i++){ // init each PWM with a random value*
    _ PWMValue* = random(20, 200); // A random number*
    * }
    }
    void loop() { //do work son
    for (int i=0; i<=TotalDigital; i++) { //For each digital pin*
    * digitalWrite(i, LOW); //Bring the pin to ground*
    * for (int j=0; j<=TotalPWM; j++) { //For each PWM pin*
    * int PWMPOS = j + 1;
    int DIGPOS = i + 1;
    int ARRAYPOS = PWMPOS * DIGPOS;
    analogWrite(PWMPin[j], PWMValue[ARRAYPOS]);
    randNumber = random(-37, 57 ); //generate a random number to add to our PWM value*
    * PWMValue[ARRAYPOS] += randNumber; //add it*
    * if (PWMValue[ARRAYPOS] > 203) {
    PWMValue[ARRAYPOS] = 203;
    }
    if (PWMValue[ARRAYPOS] < 10) {
    PWMValue[ARRAYPOS] = 10;
    }
    }
    digitalWrite(i, HIGH); //Bring the pin high, stop current*
    * }
    delay(50);
    }
    [/quote]*_

seems that just may be a speed limit on the arduino. I can multiplex 6 pwm by up to 4 digital lines before things become too strobe light. That gives me 24 so I would just need 2 arduinos, which is a far cry better than 48 TINY13s.