On one line

I want to put this on one line.

pinMode(2, OUTPUT); All the way through to pin 10. Can it be done?

Also, digitalWrite(2, LOW); All the way through to pin 10. Same?

Using a for loop:

for ( byte i = 2; i <= 10; i++ )
{
  pinMode( i, OUTPUT );
  digitalWrite( i, LOW );
}

Well not really one line but almost :slight_smile:

Modifying guix’s suggestion:

for ( byte i = 2; i <= 10; i++ )  pinMode( i, OUTPUT );

One line. :stuck_out_tongue:

And without spaces?

for(byte i=2;i<=10;i++)pinMode(i,OUTPUT);

All right already! The big bad FOR shows it head again! Thought I could do it with these things: ( ), [ ], { } But, guess not. Thanks all... :)

You can invent your own language if you like:

pinMode ( { 2 .. 10 } , OUTPUT);

But it isn't C.

The big bad FOR shows it head again

Loops are a fundamental part of programming. May as well accept it.

The big bad FOR shows it head again!

If that worries you you’re going to have a hard time programming :slight_smile:


Rob

All the way through to pin 10. Can it be done?

How about this?

pinMode(2, OUTPUT); pinMode(3, OUTPUT); pinMode(4, OUTPUT); ...., pinMode(10, OUTPUT);

If those pins are all on the same port, you can ever do it with one statement.

dhenry: If those pins are all on the same port, you can ever do it with one statement.

Not if you want the code to stick to using the Arduino core code routines and remain portable across other boards that use different processors or different variants, i.e. Arduino Uno vs Arduino Leonardo vs Mega vs Teensy vs Sanguino, Chipkit, Maple, and if it is ever released, Arduino DUE.

--- bill

And without spaces?

Please don't to that. Too much unreadable code is already posted here.

Just being playful PaulS. I don't recommend putting it all "on one line" anyway.

Reply #1 is the recommended answer.

If that worries you you're going to have a hard time programming

Hey, I've learned a lot. And I'm not worried about FOR. We all use it many times. Probably a bit over the top me saying "big bad for". Reply # 1 looks good. Just thought I could conserve some ink. I'm done! Thanks all.

donde:
All right already! The big bad FOR shows it head again!
Thought I could do it with these things: ( ), , { }
But, guess not.
Thanks all… :slight_smile:

If you hate “for”, use “while” instead:

byte i=2; while (i<=10) pinMode(i++,OUTPUT);

You can also use direct port manipulation without fors or whiles:

DDRD = DDRD | B0111111;  // pins 2-7
DDRB = DDRB | B10000000; // pin 8

If you hate "for", use "while" instead:

And:

You can also use direct port manipulation without fors or whiles:

WOW ! 2 other ways. Super :)

pekkaa: You can also use direct port manipulation without fors or whiles:

DDRD = DDRD | B0111111;  // pins 2-7
DDRB = DDRB | B10000000; // pin 8

You can use port manipulation, but as mentioned earlier it isn't portable across MCUs as it is stepping outside the Arduino core library code. Even if you are willing to accept that, the above statements still do not do what was requested: Set D2 to D10 as outputs and set D2 to D10 LOW.

This example shows, how easy it is to accidentally incorrectly set/configure the wrong pins when using direct port i/o.

The above statements are only setting the direction bits for 7 pins. Assuming a mega328/168, the above statements set the direction bits to "output" for Arduino pins D0 (Serial RX), D1 (Serial TX), D2, D3, D4, D5, and (XTAL2)

It is not setting the direction bits for pins D6, D7, D8, D9, and D10 and it is trying to set Pins D0 and D1 which are the serial port. Not sure what happens when you try to set the serial port RX pin or the Crystal XTAL2 pin to output. It also does not set any of the pin output levels to LOW as was requested.

The correct statements would be:

DDRD |= B11111100; // pins 2-7 as output
DDRB |= B00000111; // pins 8-10 as output
PORTD &= ~B11111100; // set pins 2-7 as LOW
PORTB &= ~B00000011; // set pins 8-10 as LOW

--- bill

pekkaa: You can also use direct port manipulation without fors or whiles:

DDRD = DDRD | B0111111;  // pins 2-7
DDRB = DDRB | B10000000; // pin 8

I'm inclined to agree with bperrybap here. In general, the pins are split between different ports, so you don't necessarily even achieve the aim of doing things "in one line". It is also less portable, and much less easy to verify at a quick glance that you are setting the right pins.

The couple of microseconds you might save are not generally needed to be saved. In particular, it is rarely likely that you need to set ports to inputs or outputs "in a hurry".

Yes, I totally agree with with you, Nick and bperrybap. I didn't claim that direct port manipulation (or using "while" instead of "for") was a better way to do it. I just meant to demonstrate that there are several ways to do it.

OK, no problem then.