A function to write several digital outputs at once (C++ level)

Please, find below a function that can be useful to output a set of digital values "at the same time".

Instead of doing a set of digitalWrites, you could use this function and one array with multiple values.

void emitPins(int val2emit, int pinIni, int pinFinal)
{
  int nPins = pinFinal - pinIni + 1;
  int pin2emit = pinIni;

  for(int i=0; i <nPins; i++){
	if(bitRead(val2emit, i) == 1)
		digitalWrite(pin2emit, HIGH);
	else
		digitalWrite(pin2emit, LOW);
	pin2emit ++;
  }

}

For example, you can employ it to light a set of patterns on 8 LEDs connected to digital pins 2 to 9, using a loop.

int LEDS[8] = {1,2,4,8,16,32,64, 128};

for(int i=0; i<8;i++){
       emitPins(LEDS[i], 2,9);
       delay(1000);
}

I am just sharing a function that could be useful for the community.
I have no doubts or questions regarding this code. Just sharing.

Thanks.

Do you have a question?
Code tags?
Formatting?

Thanks TMFKA-AWOL. It was just an (incomplete) suggestion.

A suggestion for what?

A suggestion for ignoring the contents of an array?

Code tags?
Formatting?

I am just sharing a function that could be useful for the community.

I have no doubts or questions regarding this code. Just sharing.

It was incomplete at the time you answered. I hope it is more complete and understandable now.

Thanks.

It is not complete without code tags.

Hello
This is a sketch on beginer level.

1 Like

Done, thanks

I think the code would be better if it allowed non-sequential output pins.
All it would take would be another array.

That would be another variant, in which you could use a second array for the non-sequential pins.

The code below compiles OK, but is not checked on actual hardware. Hopefully, it should work.

void emitPinsRandom(int val2emit, int pins[], int pinIni, int pinFinal)
{
  int nPins = pinFinal - pinIni + 1;
  int pin2emit = pinIni;

  for(int i=0; i <nPins; i++){
    if(bitRead(val2emit, i) == 1)
      digitalWrite(pins[pin2emit], HIGH);
    else
      digitalWrite(pins[pin2emit], LOW);
    pin2emit ++;
  }

}

For example, you can employ it to light a set of patterns on 8 LEDs connected to digital pins 2 to 9 (not sequentially or to any set of pins), using a loop.

void loop() {
  // put your main code here, to run repeatedly:
  int LEDS[8] = {1,2,4,8,16,32,64, 128};
  int pins[8] = {2,7,8,9,3,4,5,6};

  for(int i=0; i<8;i++){
       emitPinsRandom(LEDS[i], pins, 0, 7);
       delay(1000);
  }
}

Thanks.

You may wish to revise your array indices.

You could use it at the beginner level if you set pinMode for the outputs.

void setup() {
  // put your setup code here, to run once:
  for(int i=2; i<10;i++){
    pinMode(i, OUTPUT);
  }
}

Done, thanks

You may wish to revise your array indices
(Hint: You don't need pinIni and pinFinal, because specifying them incorrectly will break your function))

Until the person noticed and changed them properly... :slightly_smiling_face:

It is more general. For example, if you want to update just a subset of the pins.

In which case, you probably don't want to start the for loop in the function from 0 ...

Please, notice that the loop is just for the number of pins, not for their locations, which are specified by pinIni and pinFinal.

Then:

void emitPins(int val2emit, int pinIni, int pinFinal)
{
  for(int i = pinIni; i <= pinFinal; i++) {
	if(bitRead(val2emit, i-pinIni) == 1)
		digitalWrite(i, HIGH);
	else
		digitalWrite(i, LOW);
  }
}

Or, until the enums break it

void emitPins(int val2emit, int pinIni, int pinFinal)
{
  for(int i = pinIni; i <= pinFinal; i++) {
     digitalWrite(i, bitRead(val2emit, i-pinIni));
  }
}

:smiley:

1 Like

Nice optimization, thanks. Now, it would be nice that people could widely use it when needed.