Go Down

Topic: On one line (Read 1 time) previous topic - next topic

donde

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?

guix

#1
Oct 12, 2012, 07:05 am Last Edit: Oct 12, 2012, 07:07 am by guix Reason: 1
Using a for loop:
Code: [Select]

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


Well not really one line but almost :)

Modifying guix's suggestion:

Code: [Select]
for ( byte i = 2; i <= 10; i++ )  pinMode( i, OUTPUT );


One line. :P
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

And without spaces?

Code: [Select]

for(byte i=2;i<=10;i++)pinMode(i,OUTPUT);
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

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... :)

You can invent your own language if you like:

Code: [Select]

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


But it isn't C.

Quote
The big bad FOR shows it head again


Loops are a fundamental part of programming. May as well accept it.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

Graynomad

Quote
The big bad FOR shows it head again!

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

______
Rob
Rob Gray aka the GRAYnomad www.robgray.com

dhenry

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


How about this?

Code: [Select]

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.

bperrybap


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

PaulS

Quote
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.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

donde

Quote
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.

pekkaa


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


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

Code: [Select]
byte i=2; while (i<=10) pinMode(i++,OUTPUT);

pekkaa

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

Code: [Select]

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

donde

#14
Oct 15, 2012, 06:12 am Last Edit: Oct 15, 2012, 06:15 am by donde Reason: 1
Quote
If you hate "for", use "while" instead:


And:

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


WOW ! 2 other ways. Super   :)

Go Up