Offline
Newbie
Karma: 0
Posts: 45
|
 |
« on: October 12, 2012, 12:02:12 am » |
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
God Member
Karma: 19
Posts: 616
Scientia potentia est.
|
 |
« Reply #1 on: October 12, 2012, 12:05:16 am » |
Using a for loop: for ( byte i = 2; i <= 10; i++ ) { pinMode( i, OUTPUT ); digitalWrite( i, LOW ); } Well not really one line but almost 
|
|
|
|
« Last Edit: October 12, 2012, 12:07:02 am by guix »
|
Logged
|
|
|
|
|
Global Moderator
Melbourne, Australia
Offline
Shannon Member
Karma: 218
Posts: 13897
Lua rocks!
|
 |
« Reply #2 on: October 12, 2012, 12:10:13 am » |
Modifying guix's suggestion: for ( byte i = 2; i <= 10; i++ ) pinMode( i, OUTPUT );
One line. 
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Melbourne, Australia
Offline
Shannon Member
Karma: 218
Posts: 13897
Lua rocks!
|
 |
« Reply #3 on: October 12, 2012, 12:12:00 am » |
And without spaces? for(byte i=2;i<=10;i++)pinMode(i,OUTPUT);
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 45
|
 |
« Reply #4 on: October 12, 2012, 12:36:39 am » |
All right already! The big bad FOR shows it head again! Thought I could do it with these things: ( ), [ ], { } But, guess not. Thanks all...
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Melbourne, Australia
Offline
Shannon Member
Karma: 218
Posts: 13897
Lua rocks!
|
 |
« Reply #5 on: October 12, 2012, 12:47:18 am » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
nr Bundaberg, Australia
Online
Tesla Member
Karma: 71
Posts: 6826
Scattered showers my arse -- Noah, 2348BC.
|
 |
« Reply #6 on: October 12, 2012, 09:51:41 am » |
The big bad FOR shows it head again! If that worries you you're going to have a hard time programming  ______ Rob
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Edison Member
Karma: 114
Posts: 2205
|
 |
« Reply #7 on: October 12, 2012, 10:31:46 am » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
Dallas, TX USA
Offline
Edison Member
Karma: 25
Posts: 1617
|
 |
« Reply #8 on: October 12, 2012, 11:02:04 am » |
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
Brattain Member
Karma: 314
Posts: 35507
Seattle, WA USA
|
 |
« Reply #9 on: October 12, 2012, 06:03:26 pm » |
And without spaces? Please don't to that. Too much unreadable code is already posted here.
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Melbourne, Australia
Offline
Shannon Member
Karma: 218
Posts: 13897
Lua rocks!
|
 |
« Reply #10 on: October 12, 2012, 06:05:21 pm » |
Just being playful PaulS. I don't recommend putting it all "on one line" anyway.
Reply #1 is the recommended answer.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 45
|
 |
« Reply #11 on: October 13, 2012, 09:44:02 pm » |
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
Sr. Member
Karma: 0
Posts: 270
Arduino rocks
|
 |
« Reply #12 on: October 14, 2012, 03:52:58 am » |
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: byte i=2; while (i<=10) pinMode(i++,OUTPUT);
|
|
|
|
|
Logged
|
|
|
|
|
Finland
Offline
Sr. Member
Karma: 0
Posts: 270
Arduino rocks
|
 |
« Reply #13 on: October 14, 2012, 04:06:18 am » |
You can also use direct port manipulation without fors or whiles: DDRD = DDRD | B0111111; // pins 2-7 DDRB = DDRB | B10000000; // pin 8
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 45
|
 |
« Reply #14 on: October 14, 2012, 11:12:57 pm » |
If you hate "for", use "while" instead:
And: You can also use direct port manipulation without fors or whiles:
WOW ! 2 other ways. Super 
|
|
|
|
« Last Edit: October 14, 2012, 11:15:46 pm by donde »
|
Logged
|
|
|
|
|
|