Set All Pins to Output

Hi All,

// Set all pins as output
for(int i=2; i<=9; i++) {
pinMode(i, OUTPUT);
}

I found this online, and trying to reverse engineer this. Can someone explain how it turns pins on? I was originally going to list each individual pin as output but thought there was an easier way to do this.

My ultimate goal is to turn digital pins 2 - 13 and A0 - A5 pins as output in the void setup

Thanks in advance

I suggest you look at this:
http://arduino.cc/en/Reference/For

Are you asking how the for-loop works?

Also, you wonder how to set all pins as outputs in an easier way than actually coding them one-by-one?
Can't help you with that last one, but explaining the for-loop is simple.

Get all the pins, 2 to 19:

for(int i=2; i<=19; i++) {
pinMode(i, OUTPUT);
}

enanthate:
Are you asking how the for-loop works?

Also, you wonder how to set all pins as outputs in an easier way than actually coding them one-by-one?
Can’t help you with that last one, but explaining the for-loop is simple.

Yes I want to know how the loop works.

for(int i=2; i<=19; i++) {

for the second part, here’s what I have. I already have these defined to specific pins.

pinMode(redLed, OUTPUT);
pinMode(zone1, OUTPUT);
pinMode(zone2, OUTPUT);
pinMode(zone3, OUTPUT);
pinMode(zone4, OUTPUT);
pinMode(zone5, OUTPUT);
pinMode(zone6, OUTPUT);
pinMode(zone7, OUTPUT);
pinMode(zone8, OUTPUT);
pinMode(zone9, OUTPUT);
pinMode(zone10, OUTPUT);
pinMode(zone11, OUTPUT);
pinMode(zone12, OUTPUT);
pinMode(zone13, OUTPUT);
pinMode(zone14, OUTPUT);
pinMode(zone15, OUTPUT);
pinMode(zone16, OUTPUT);

There has to be an easier way to do this, right?

CrossRoads:
Get all the pins, 2 to 19:

for(int i=2; i<=19; i++) {
pinMode(i, OUTPUT);
}

I think CrossRoads helped me out with defining all pins XD

I would still like a breakdown explanation, more - so this part: i<=19; i++

eros:
I would still like a breakdown explanation, more - so this part: i<=19; i++

As LarryD suggested, did you read this: http://arduino.cc/en/Reference/For ?

The “i<=19” part is your test… while i is <= 19, do the increment (the “i++” part) and go again. Once i>19, you’re done.

for loop has 3 parts:
start condition, i=2
test condition to see if done, i <= 19
how the variable changes each pass thru loop, i++, same as i=i+1

So the code is doing this basically

i = 2;
start
pinMode (i, OUTPUT);
i=i+1;
if (i <= 19) {goto start}

Thanks for the help guys, I'm taking a look at the reference and it's very helpful.

So the code is doing this basically
Code:
i = 2;
start
pinMode (i, OUTPUT);
i=i+1;
if (i <= 19) {goto start}

Noooooo!

The test is at the TOP of the loop.

AWOL:

So the code is doing this basically
Code:
i = 2;
start
pinMode (i, OUTPUT);
i=i+1;
if (i <= 19) {goto start}

Noooooo!

The test is at the TOP of the loop.

Yup, he made a

do
{
}
while( test );

sort of.

I would also like to point out that using int for an index that will be less than 256 is a baaaaad AVR habit.

Test at top, test at bottom, where is not really important. Idea was to show the 3 steps + action being done - initialize, perform action, increment, test.

Test at top, test at bottom, where is not really important.

Yes, it absolutely is!
Test at bottom (do...while) will always execute the body of the loop at least once.
Test at the top (while...) may not execute the body of the loop at all.

for(condition == true)
{
//Here the code to execute repeatedly (looping) while the for condition is true
}

example here uses uint8_t for the numircal value, which is a number between 0 and 255

uint8_t counterValue = 1; // define initial value
uint8_t maxValue = 10; // define max value

for(counterValue < maxValue) { //Clear example
Serial.println(counterValue); // Print the counter value to serial
counterValue++; // Each time loop is true, add 1 to counter value
}

++ behind a numerical variable name increases it with 1, – behind a numerical value decreases it with 1.

AWOL:

Test at top, test at bottom, where is not really important.

Yes, it absolutely is!
Test at bottom (do...while) will always execute the body of the loop at least once.
Test at the top (while...) may not execute the body of the loop at all.

Think about that - why would you initialize a for loop that you never intended to run?

I don't care if you wrote it this way instead:

i = 2;
if (i > 19){ goto end}
pinMode (i, OUTPUT);
i=i+1;
end

The for:next concept is still explained.

Think about that - why would you initialize a for loop that you never intended to run?

Often you don't know in advance (though not here, obviously) if the loop is intended to run or not - empty list or string handling come to mind.
Dereferencing an invalid or null pointer in a do..while might be catastrophic.

Lennaert:
for(condition == true)
{
//Here the code to execute repeatedly (looping) while the for condition is true
}

Command names aren't like animal species. They're just conveniences for programmers.

Please note that what Arduino source code is compiled into is the reality. There is no for-loop in AVR.
All that for-loop is is a while loop with built-in counter. That's it.

You've managed to degrade a for-loop down to a while-loop, not do some kind of magic.

char i, j; // classic index names as signed 8 bit indexes
int  arry[ 10 ][ 10 ];

void setup( void )
{
  Serial.begin( 115200 );
  Serial.println( );

  for ( i = 0; i < 10; i++ )
  {
    for ( j = 0; j < 10; j++ )
    {
      arry[ i ][ j ] = i * 10 + j;
    }
  }

  i = j = 0;
}

void loop( void )
{
  if ( arry[ i ][ j ] < 10 )
  {
    Serial.print( " " );
  }
  Serial.print( arry[ i ][ j ] );
  if (( i < 9 ) || ( j < 9 ))
  {
    Serial.print( ", " );
  }
  if ( ++j == 10 )
  {
    Serial.println( );
    i++;
    if ( i == 10 )
    {
      while( 1 );
    }
    j = 0;
  }
}

Sometimes an example says more than many times as many words.

And other times, some comments can really help.

CrossRoads:

Sometimes an example says more than many times as many words.

And other times, some comments can really help.

Sorry. I usually don't comment the obvious.
But then I also consider the code to be the real comments.

I'm just showing how loop() can serve where nested while-loops or for-loops would do.

The thing is that when I write event code, I try to avoid while and for loops, especially nested, that block timing.
This is an example of how that is done, just to show that nobody HAS to do it in some learned way.
It's a bit like saying don't use an int where a byte will do just because you "grew up" using int.

Funny thing though... it's a bit like the principle of what you've been showing earlier in this thread.