for () Statement question.

This sketch im using has this for () statement in the void setup()

void setup()
{
randomSeed(analogRead(0))
for (int z=1; z<7; z++)
{
pinMode(z, OUTPUT);
}
}

Is only one pin an output at a time or is it looping in the setup and eventually all 6 are outputs after the set up and this is just an better way than pinMode() for all 6 pins used? Just a little confused.

thanks for any help!

for (int z=1; z<7; z++)
   {
     pinMode(z, OUTPUT);
   }

This part is counting up and setting pins z = 1 through z = 6 to OUTPUT.

It is a simple way to set the six versus the 6 lines of code you would need to do it one at a time

If the pins are on the same port, you can use the Data Direction Register (DDR) and set all 6 at one time.

Example, DDRD = 0b00000001; // set bit 0 as input, set bits 1 to 7 as outputs Or maybe 1 is output, and 0 is input, need to check the datasheet. I use pinMode myself as it's easier to remember, and generally this only runs 1 time in setup.

Here we go:

14.2.1 Configuring the Pin Each port pin consists of three register bits: DDxn, PORTxn, and PINxn. As shown in ”Register Description” on page 92, the DDxn bits are accessed at the DDRx I/O address, the PORTxn bits at the PORTx I/O address, and the PINxn bits at the PINx I/O address. The DDxn bit in the DDRx Register selects the direction of this pin. If DDxn is written logic one, Pxn is configured as an output pin. If DDxn is written logic zero, Pxn is configured as an input pin. If PORTxn is written logic one when the pin is configured as an input pin, the pull-up resistor is activated. To switch the pull-up resistor off, PORTxn has to be written logic zero or the pin has to be configured as an output pin. The port pins are tri-stated when reset condition becomes active, even if no clocks are running. If PORTxn is written logic one when the pin is configured as an output pin, the port pin is driven high (one). If PORTxn is written logic zero when the pin is configured as an output pin, the port pin is driven low (zero).

11Trevor11: the void setup()

There is no such thing as "the void setup()"

There is a function called setup with a return type of void.

CrossRoads: If DDxn is written logic one, Pxn is configured as an output pin. If DDxn is written logic zero, Pxn is configured as an input pin.

That seems so backwards to me. I much prefer the PIC way:

1 = I = Input 0 = O = Output

So much easier to remember.

Ah well...

11Trevor11:
This sketch im using has this for () statement in the void setup()

void setup()
{
randomSeed(analogRead(0))
for (int z=1; z<7; z++)
{
pinMode(z, OUTPUT);
}
}

Is only one pin an output at a time or is it looping in the setup and eventually all 6 are outputs after the set up and this is just an better way than pinMode() for all 6 pins used? Just a little confused.

thanks for any help!

Missing semicolon on the randomSeed line.

Its setting each pin to be an output without the space cost of a line of code
for each one - not an uncommon idiom.

Its more portable than using DDRx resisters.

It would be even more portable to name the pins rather than using hard-coded
numbers - #define or whatever, and to also add a comment alongside the
#defines saying they are assumed to be contiguously numbered.

MarkT:

11Trevor11:
This sketch im using has this for () statement in the void setup()

void setup()
{
randomSeed(analogRead(0))
for (int z=1; z<7; z++)
{
pinMode(z, OUTPUT);
}
}

Is only one pin an output at a time or is it looping in the setup and eventually all 6 are outputs after the set up and this is just an better way than pinMode() for all 6 pins used? Just a little confused.

thanks for any help!

Missing semicolon on the randomSeed line.

Its setting each pin to be an output without the space cost of a line of code
for each one - not an uncommon idiom.

Its more portable than using DDRx resisters.

It would be even more portable to name the pins rather than using hard-coded
numbers - #define or whatever, and to also add a comment alongside the
#defines saying they are assumed to be contiguously numbered.

Better still would be to put the named pins into an array so you can still loop through them.

Why is better?

less code?

more reliable?

more portable?

less RAM?

less headache?

easier debug?

less snarky comments from other forum members? :D

CrossRoads: If the pins are on the same port, you can use the Data Direction Register (DDR) and set all 6 at one time.

Example, DDRD = 0b00000001; // set bit 0 as input, set bits 1 to 7 as outputs Or maybe 1 is output, and 0 is input, need to check the datasheet. I use pinMode myself as it's easier to remember, and generally this only runs 1 time in setup.

Port register mapping is not consistent between processors. There are quite a few commonly used processors on different Arduino boards.

For a simple case like setup, I would suggest using portMode commands, not port registers.

I only use port register based code for situations where I need sub-milliscond timing, like firing a camera flash at the instant a laser beam is broken. (a case I've actually coded.)

BulldogLowell:

for (int z=1; z<7; z++)

{
     pinMode(z, OUTPUT);
   }




This part is *counting up* and setting pins z = 1 through z = 6 to OUTPUT.

It is a simple way to set the six versus the 6 lines of code you would need to do it one at a time

The for loop is equivalent to

     pinMode(1, OUTPUT);
     pinMode(2, OUTPUT);
     pinMode(3, OUTPUT);
     pinMode(4, OUTPUT);
     pinMode(5, OUTPUT);
     pinMode(6, OUTPUT);

The loop approach is very normal coding style. For a small number of steps like this, you could argue either way.

The for loop generates a slightly shorter program.

The approach of doing each statement independently is probably slightly faster.

If you have dozens of repeated lines, the for loop is much easier to read and maintain.

Using loops is slower tho - each pass thru adds ~ 12uS. I don't mind where it is just one occurrence in setup. Like you Duncan, I don't use loops where timing is tight.

Majenkotech? I seem to recall a member who went by the name of Majenko… 8)

majenkotech:

MarkT:

11Trevor11:
This sketch im using has this for () statement in the void setup()

void setup()
{
randomSeed(analogRead(0))
for (int z=1; z<7; z++)
{
pinMode(z, OUTPUT);
}
}

Is only one pin an output at a time or is it looping in the setup and eventually all 6 are outputs after the set up and this is just an better way than pinMode() for all 6 pins used? Just a little confused.

thanks for any help!

Missing semicolon on the randomSeed line.

Its setting each pin to be an output without the space cost of a line of code
for each one - not an uncommon idiom.

Its more portable than using DDRx resisters.

It would be even more portable to name the pins rather than using hard-coded
numbers - #define or whatever, and to also add a comment alongside the
#defines saying they are assumed to be contiguously numbered.

Better still would be to put the named pins into an array so you can still loop through them.

Yup, you can name values.
Like by using an enum. The attribute ((packed)) part makes them 8 bit values instead of 16 bit.

__attribute__ ((__packed__)) enum  buttonNames 
{
  fSlowDown = 0, fSpeedUp, fButton2, fButton3, fButton4, fButton5, fButton6, fBlinkToggle 
};

any that don’t = a value get the last enum’s value + 1.
And no, the = 0 of the first one doesn’t have to be there, it is the default.

CrossRoads: Using loops is slower tho - each pass thru adds ~ 12uS. I don't mind where it is just one occurrence in setup. Like you Duncan, I don't use loops where timing is tight.

Wow! That much! Even 12 cycles would be bad after a while.

BulldogLowell:
less snarky comments from other forum members? :smiley:

Is it snarky to say you forgot less typing? ]:smiley:

11Trevor11:
This sketch im using has this for () statement in the void setup()

void setup()
{
randomSeed(analogRead(0))
for (int z=1; z<7; z++)
{
pinMode(z, OUTPUT);
}
}

Is only one pin an output at a time or is it looping in the setup and eventually all 6 are outputs after the set up and this is just an better way than pinMode() for all 6 pins used? Just a little confused.

thanks for any help!

pinMode(), digitalWrite() and all the set a pin some way commands are like light switches.
They stay flipped until you change them.

If you code that way then adding pins means having to make fewer code changes.
It also makes smaller though a bit slower code.

I learned a little AVR trick from Nick Gammon a while back. You can toggle any port pin by writing the corresponding bit in the PINx register.

For this example I hope you can forgive the sin of using delay()....

Go ahead, test it. I did.

byte ledPin = 13;

void setup() 
{                
  pinMode( ledPin, OUTPUT );     
}

void loop() 
{
  PINB |= 32;  // tada! pin 13 is port B pin 5, this line toggles pin 13
  delay( 1000 ); 
}

Edit: erroneous comment changed, thanks AWOL!

32 is 2^5

No, 2^5 is 7 ;)

AWOL: 32 is 2^5

No, 2^5 is 7 ;)

You're right, I wrote in the wrong language! I used (pretty sure that's) BASIC! Someone get the whip, I've been a baaaad programmer!

By 2^5 I meant 2 to the 5th power.