I got inspired by the recent post from someone who wanted to copy an input bit to an output. So I thought "why not use that to fade all 20 pins?".
This sketch is the result. It's based on the Fade example, but by adding in a pin change interrupt, we detect when the pin (pin 9) is pulsed and make all the other pins mirror it. Thus, all 20 pins fade in and out together.
int brightness = 0; // how bright the LED is
int fadeAmount = 5; // how many points to fade the LED by
const byte pwmPin = 9;
const byte maxPin = 19;
ISR (PCINT0_vect)
{
static byte val = 0;
val = !val;
for (byte i = 0; i <= maxPin; i++)
if (i != pwmPin)
digitalWrite (i, val);
}
void setup()
{
for (byte i = 0; i <= maxPin; i++)
pinMode (i, OUTPUT);
// pin change interrupt
PCMSK0 = _BV (PCINT1); // only want pin 9
PCIFR = _BV (PCIF0); // clear any outstanding interrupts
PCICR |= _BV (PCIE0); // enable pin change interrupts for PCINT7..0
} // end of setup
void loop()
{
// set the brightness of pin 9:
analogWrite(pwmPin, brightness);
// change the brightness for next time through the loop:
brightness = brightness + fadeAmount;
// reverse the direction of the fading at the ends of the fade:
if (brightness == 0 || brightness == 255)
fadeAmount = -fadeAmount ;
// wait for 30 milliseconds to see the dimming effect
delay(30);
} // end of loop