How can I make use of multiple instances in one function inside a library?
Say I have a library similar to the one shown here and 2 LEDs connected to pin 11 and 12, but both LEDs have their GND-pin connected to pin 1 so that I can switch them also with pin 1. I know this is a silly way to connect LEDs, but this way I can explain what I mean, so please consider it just as an illustrating example. My begin() function would look like this:
void Morse::begin(int pin) {
pinMode(pin, OUTPUT);
digitalWrite(pin, HIGH);
pinMode(1, OUTPUT);
digitalWrite(1, LOW);
delay(200);
digitalWrite(pin, LOW);
}
Now I create 2 instances:
Morse morse1(11); // LED at pin 11
Morse morse2(12); // LED at pin 12
To begin() them I need to do this in my main sketch:
morse1.begin();
morse2.begin();
The result will be:
pinMode(11, OUTPUT);
digitalWrite(11, HIGH);
pinMode(1, OUTPUT);
digitalWrite(1, LOW);
delay(200);
digitalWrite(11, LOW);
pinMode(12, OUTPUT);
digitalWrite(12, HIGH);
pinMode(1, OUTPUT);
digitalWrite(1, LOW);
delay(200);
digitalWrite(12, LOW);
But I want it nested like this:
pinMode(11, OUTPUT);
pinMode(12, OUTPUT);
digitalWrite(11, HIGH);
digitalWrite(12, HIGH);
pinMode(1, OUTPUT);
digitalWrite(1, LOW);
delay(200);
digitalWrite(11, LOW);
digitalWrite(12, LOW);
So I need a begin() function in the library that does something like:
void Morse::begin() {
pinMode(pins_of_all_instances, OUTPUT);
digitalWrite(pins_of_all_instances, HIGH);
pinMode(1, OUTPUT);
digitalWrite(1, LOW);
delay(200);
digitalWrite(pins_of_all_instances, LOW);
}
Is that possible?