Why would you want to waste two PWM pins for what is a simple digital input application? This code uses pins 11 and 12 but any pins will do. You would put the second encoder on another two pins and just double up the code, at least that is the most straightforward way. If you had lots of encoders, you could refactor it out into a function, of course. If you can to do that I will leave it up to you.
Assuming the code you provided works with one encoder and one LED, something like this should work for two encoders and two LEDs. I haven't tested this nor even compiled it, but, pin 10 is the second LED, pin 7 and 8 are the A and B for encoder #2. You can change these.
int brightness1 = 120; // how bright the LED is, start at half brightness
int brightness2 = 120; // how bright the LED is, start at half brightness
int fadeAmount = 10; // how many points to fade the LED by
unsigned long currentTime;
unsigned long loopTime;
const int pin_A1 = 12; // pin 12
const int pin_B1 = 11; // pin 11
const int pin_A2 = 7; // pin 7
const int pin_B2 = 8; // pin 8
unsigned char encoder_A1;
unsigned char encoder_B1;
unsigned char encoder_A1_prev=0;
unsigned char encoder_A2;
unsigned char encoder_B2;
unsigned char encoder_A2_prev=0;
void setup() {
// declare pin 9 to be an output:
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
pinMode(pin_A1, INPUT);
pinMode(pin_B1, INPUT);
pinMode(pin_A2, INPUT);
pinMode(pin_B2, INPUT);
currentTime = millis();
loopTime = currentTime;
}
void loop() {
// get the current elapsed time
currentTime = millis();
if(currentTime >= (loopTime + 5)){
// 5ms since last check of encoder = 200Hz
encoder_A1 = digitalRead(pin_A1); // Read encoder pins
encoder_B1 = digitalRead(pin_B1);
if((!encoder_A1) && (encoder_A1_prev)){
// A has gone from high to low
if(encoder_B1) {
// B is high so clockwise
// increase the brightness, dont go over 255
if(brightness1 + fadeAmount <= 255) brightness1 += fadeAmount;
}
else {
// B is low so counter-clockwise
// decrease the brightness, dont go below 0
if(brightness1 - fadeAmount >= 0) brightness1 -= fadeAmount;
}
}
encoder_A1_prev = encoder_A1; // Store value of A for next time
// set the brightness of pin 9:
analogWrite(9, brightness1);
// 5ms since last check of encoder = 200Hz
encoder_A2 = digitalRead(pin_A2); // Read encoder pins
encoder_B2 = digitalRead(pin_B2);
if((!encoder_A2) && (encoder_A2_prev)){
// A has gone from high to low
if(encoder_B2) {
// B is high so clockwise
// increase the brightness, dont go over 255
if(brightness2 + fadeAmount <= 255) brightness2 += fadeAmount;
}
else {
// B is low so counter-clockwise
// decrease the brightness, dont go below 0
if(brightness2 - fadeAmount >= 0) brightness2 -= fadeAmount;
}
}
encoder_A2_prev = encoder_A2; // Store value of A for next time
// set the brightness of pin 10:
analogWrite(10, brightness2);
}
// Other processing can be done here
}