I have an LED matrix set up to do various different light display sequences including some PWM stuff. The current code works fine but takes up way too much space and I’m trying to figure out how to condense the code.
Essentially I have a row of LEDs using multiplexing and the coding is set up to fade one LED up and then back down, then move on to the next LED in the row. The only way I’ve figured out how to do this is to write a function for each individual LED and then recall that function in the loop. What I want to do is instead of writing a function for each LED, I want to be able to do a digitalWrite but have the pin set up to be a variable that increments.
The code that works looks like this:
void top1Fade ()
digitalWrite (1, HIGH);
int x = 1;
for (int i = 1; i > -1; i = i + x)
digitalWrite (1, LOW);
As I said, I’m multiplexing the LEDs, so pin 5 is my PWM to the base of one transistor that is connecting the power rail to several of the anodes of the LEDs in one row, and pin 1 is set up to the base of another transistor connected to the cathode of the specific LED i’m trying to light up and ground the circuit.
My question is, how could I write code where it would look something like:
“digitalWrite (z, HIGH);”
where “z” would be the pin and it would increment through pins 0 - 13. I realize I’m utilizing some pins for PWM so it would actually only go from 0-2, 4, and 6-13. I’ve attached a quick mock up of the circuit so the multiplexing will make more sense. Thanks ahead of time!