Pages: [1] 2   Go Down
Author Topic: On one line  (Read 1027 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 46
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

France
Offline Offline
God Member
*****
Karma: 29
Posts: 898
Scientia potentia est.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Using a for loop:
Code:
for ( byte i = 2; i <= 10; i++ )
{
  pinMode( i, OUTPUT );
  digitalWrite( i, LOW );
}

Well not really one line but almost smiley
« Last Edit: October 12, 2012, 12:07:02 am by guix » Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Modifying guix's suggestion:

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

One line. smiley-razz
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

And without spaces?

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

Offline Offline
Newbie
*
Karma: 0
Posts: 46
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You can invent your own language if you like:

Code:
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.
Logged

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 121
Posts: 8445
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
The big bad FOR shows it head again!
If that worries you you're going to have a hard time programming smiley

______
Rob
Logged

Rob Gray aka the GRAYnomad www.robgray.com

Offline Offline
Edison Member
*
Karma: 116
Posts: 2205
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

How about this?

Code:
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.
Logged

Dallas, TX USA
Offline Offline
Edison Member
*
Karma: 47
Posts: 2336
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 549
Posts: 46090
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
And without spaces?
Please don't to that. Too much unreadable code is already posted here.
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Reply #1 is the recommended answer.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 46
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Finland
Offline Offline
Sr. Member
****
Karma: 0
Posts: 270
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

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

Finland
Offline Offline
Sr. Member
****
Karma: 0
Posts: 270
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

Offline Offline
Newbie
*
Karma: 0
Posts: 46
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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   smiley
« Last Edit: October 14, 2012, 11:15:46 pm by donde » Logged

Pages: [1] 2   Go Up
Jump to: