Linear dimming (PWM) with IR remote on white LEDs.

Hey All,
I’ve got a sketch that uses the IRremote library to receive signals to turn on and off and dim an array of pins to control white LEDs. I’m currently doing this with simple PWM but it’s clunky and not smooth. I’ve been doing some reading on ways to smooth this out but I haven’t found anything exactly like I’m doing.

I’ve found this which gives a nice fade but I’m not sure how to make this work with my sketch.
http://www.electroschematics.com/9368/arduino-multiple-lights-dimmer/

How can I get smooth dimming in my sketch below?
Thanks!
Rich

#include <IRremote.h>

int RECV_PIN = A1;  //  IR Receiver
IRrecv irrecv(RECV_PIN);
decode_results results;

int statLED1 = 5; //LED 1
int statLED2 = 3; //LED 2
int statLED3 = 9; //LED 3

int statLED_state1 = LOW;  //mac
int statLED_state2 = LOW;  //mac
int statLED_state3 = LOW;  //mac

int pins[] = {  // array of the pins
  3, 5, 9};

int arrayLevel = 0;

//Button Stuff
int buttonPin = 6;    // physical button
int buttonState;             // the current reading from the input pin
int lastButtonState = LOW;   // the previous reading from the input pin
long lastDebounceTime = 0;  // the last time the output pin was toggled
long debounceDelay = 50;    // the debounce time; increase if the output flickers

void setup() {
  pinMode(RECV_PIN, INPUT);
  irrecv.enableIRIn(); // Start the receiver

  pinMode(statLED1, OUTPUT);
  pinMode(statLED2, OUTPUT);
  pinMode(statLED3, OUTPUT);

  pinMode(3, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(9, OUTPUT);

  digitalWrite(statLED1, statLED_state1);  //mac
  digitalWrite(statLED2, statLED_state2);  //mac
  digitalWrite(statLED3, statLED_state3);  //mac

  digitalWrite(3, LOW);
  digitalWrite(5, LOW);
  digitalWrite(9, LOW);

  Serial.begin(9600);
  Serial.println("Waiting: ");
}

void loop() {
  int reading = digitalRead(buttonPin);
  // If the switch changed, due to noise or pressing:
  if (reading != lastButtonState) {
    // reset the debouncing timer
    lastDebounceTime = millis();
      Serial.println("This prints to the console when button is pushed");
  } 

  if ((millis() - lastDebounceTime) > debounceDelay) {
      Serial.println("This prints to the console for infinity");
    // if the button state has changed:
    if (reading != buttonState) {
      Serial.println("This DOES NOT print to the console");
      // only toggle the LED if the new button state is HIGH
        if(arrayLevel > 0)
          arrayLevel = 0; // if statLED_state1 is greater than 0
        else
          arrayLevel = 255; // otherwise statLED_state1 is 0 or less.
      
      arrayWriteAnalog(arrayLevel); // compact of the above IF/ELSE statement
      Serial.println("Button On/Off");
    }
  }

  unsigned long key=0;
  if(irrecv.decode(&results))
  {
    key = results.value;         //Fetch the key
    if(key != 0) //Ignore keys that are zero
    {
      Serial.println("Key Recieved: ");
      Serial.println(key);

      switch(key)
      {
      case 0xA55A21DE:
        if(arrayLevel > 0) 
          arrayLevel = 0; // if statLED_state1 is greater than 0
        else
          arrayLevel = 255; // otherwise statLED_state1 is 0 or less.

        arrayWriteAnalog(arrayLevel); // compact of the above IF/ELSE statement
        Serial.println("Remote On/Off");
        delay(250);
        break;

      case 0xA55A01FE:
        Serial.println("Brightening lights up");
        arrayLevel = arrayLevel + 15;

        if (arrayLevel < 0) arrayLevel = 0;
        if (arrayLevel > 255) arrayLevel = 255;

        arrayWriteAnalog(arrayLevel);
        Serial.println(arrayLevel);
        break;

      case 0xA55A817E:
        Serial.println("Dimming lights down");
        arrayLevel = arrayLevel - 15;

        if (arrayLevel < 0) arrayLevel = 0;
        if (arrayLevel > 255) arrayLevel = 255;

        arrayWriteAnalog(arrayLevel);
        Serial.println(arrayLevel);
        break;
      }
      irrecv.resume(); // Receive the next value
    }  
  }
}

void arrayWriteAnalog(int val) {
  val = constrain(val, 0, 255);
  for (int i = 0; i < sizeof(pins) / sizeof(int); i++) {
    Serial.print("analogWrite ");
    Serial.print(pins[i]);
    Serial.print(" ");
    Serial.println(val);
    Serial.flush();
    analogWrite(pins[i], val);
  } 
}

void arrayWrite(int pins[], int val) {
  for (int i = 0; i <= (sizeof(pins)/sizeof(int)); i++) {
    digitalWrite(pins[i], val);
  }
}

I'm currently doing this with simple PWM but it's clunky and not smooth.

"Clunky" and "smooth" are not in the index of my C++ book.

You'll need to define what you mean by "clunky". From that, we can infer what you mean by smooth.

@Richie; instead of reporting him to the moderators, wouldn't it be simpler just to answer the man's perfectly legitimate questions?