# Turn three pins LOW as esay as posible

I wounder if someone knows how to turn three different pins LOW insted of writing digitalWrite three times?

And can you turn pins LOW in a function?

You could write digitalWrite once, and call it three times in a for loop.
If the pins are all on the same port, you can use direct port manipulation.

And can you turn pins LOW in a function?

setup() and loop() are functions. So, obviously, you can.

Even if not in the same port, one can use direct port manipulation.
For example:

``````PORTD = PORTD & 0b11111011; // port D, bit 2 low
PORTC = PORTC & 0b11110111; //port C, bit 3 low
PORTB = PORTB & 0b11011111; // port B, bit 5 low
``````

Will happen in 3 clock cycles, nearly instantaneous from user's perception
Digitally, they will occur 62.5nS apart of course.
Make sure all pins use pinMode to set to output in setup.

Doing this in a function will slow it down a lot as stuff is pushed/popped on stack, all the function jumping around stuff.
Do it inline for faster operation.

``````void LedOff(){
Â  int i;
Â  Â  for(i = 0; i < 3; i++){
Â  Â  int pin = 9 + i;
Â  Â  digitalWrite(pin, LOW);
Â  Â  }
Â  i= 0;
}
``````
``````void LedOff(){
Â  Â  for( int pin = 9; pin < 12; pin++){
Â  Â  Â  digitalWrite(pin, LOW);
Â  Â  }
}
``````

Why not just

``````for(i = 9; i < 12; i++){
``````

for pins 9,10,11?

This will execute faster:

``````void LedOff(){
digitalWrite(9, LOW);
digitalWrite(10, LOW);
digitalWrite(11, LOW);
}
``````

AWOL:

``````void LedOff(){
``````

for( int pin = 9; pin < 12; pin++){
Â  Â  Â  digitalWrite(pin, LOW);
Â  Â  }
}

True

``````inline void LedOff() Â {
// on a 328, Arduino Pins 9 .. 11 are PortB 1..3
Â PORTB & = ~ 0b00001110 ; // clear these 3 bitsÂ
}
``````

The main issue is that digitalWrite with a variable pin is considerably slower than with constant pins,
which is slower than direct port manipulations.

But your concern is easy, not fast. And as such, AWOLâ€™s solution is more Arduino style.

michael_x:

``````inline void LedOff() Â {
``````

// on a 328, Arduino Pins 9 .. 11 are PortB 1..3
Â PORTB & = ~ 0b00001110 ; // clear these 3 bitsÂ
}

``````

The main issue is that digitalWrite with a variable pin is considerably slower than with constant pins,
which is slower than direct port manipulations.

But your concern is *easy*, not *fast*. And as such, AWOL's solution is more Arduino style.
``````

But by turning portb low dosent that turn pin 8, 12 and 13 low to.

digitalWrite is also portable across different Arduino boards and will work across ports on the same board, whereas direct port manipulation does neither.
Easier to read and understand too.

@AWOL: I fully agree.
However, if you port something from an UNO to a MEGA or an attiny, you usually modify the pins anyway.
But a 16MHz Arduino is usually fast enough to turn leds off that way.

But by turning portb low dosent that turn pin 8, 12 and 13 low to.

Perhaps you miss the "subtle beauty" of

`PORTB & = ~ 0b00001110 ; `

which is just short for

`PORTBÂ  = PORTB & 0b11110001 ; // leave Port B bits 7..4 and bit 0 untouched`

If you're writing pins low that implies that you are also doing other things to those pins such as changing their pin mode or reading from them or setting them high. In that case you should use defined constants for the pin numbers or range of pin numbers and you should be using those constants here, not the literal numbers.

In case you ever need to support pin numbers which are not consecutive and if the pins are going to be used in a similar way, it would be good practice to put the pin numbers in an array and just iterate through the array.