const int channel[] = {
8, 9, 10, 11};
// the output pin channel (mux's input):
const int outputPin = 6;
void setup() {
// set up all pins as output:
for (int thisPin = 6; thisPin < 12; thisPin++) {
pinMode(thisPin, OUTPUT);
}
}
void loop() {
// iterate over the 16 channels of the multiplexer:
for (int thisChannel = 0; thisChannel < 16; thisChannel++) {
// set the channel pins based on the channel you want:
muxWrite(thisChannel);
// fade the current channel up:
for (int brightness = 0; brightness < 256; brightness++) {
analogWrite(outputPin, brightness);
delay(5);
}
}
}
void muxWrite(int whichChannel) {
// iterate over the number of pins you're using:
for (int thisPin = 0; thisPin < 4; thisPin++) {
// calculate the state of this pin based on
// its bit value in whichChannel:
int pinState = bitRead(whichChannel, thisPin);
// turn the pin on or off:
digitalWrite(channel[thisPin],pinState);
}
}