The above example is a good one for learning how to use arrays. You might not want to go for the extra challenge, but I'd use an array of class objects.
class Pulser
{
int p;
long l;
long w;
public:
Pulser(int pin, long wait = 10)
{
p = pin;
w = wait;
l = -1;
}
void pulse()
{
digitalWrite(p, HIGH);
l = millis();
}
void loop()
{
if (l >= 0 && millis() >= l + w)
{
l = -1;
digitalWrite(p, LOW);
}
}
};
This is a class I have called Pulser. You can make many "objects" which are independent of each other, but have the same functions available for them. In this case, it needs a mandatory pin number, and can optionally take an auto-off wait parameter, 10 milliseconds if not given.
#define countof(a) (sizeof(a)/sizeof(*(a)))
Pulser outs[3] =
{
Pulser(2), // support pin 2
Pulser(5), // support pin 5
Pulser(7, 100), // pin 7 auto-offs after a longer pulse
};
Then you just need to use them.
void loop()
{
// decide if any outs need to be pulsed HIGH
if (buttonA())
outs[0].pulse();
if (buttonB())
outs[1].pulse();
if (buttonEscape())
outs[2].pulse();
// update all pulse auto-off status
for (i = 0; i < countof(outs); i++)
outs[i].loop();
}