Controlling Multiple Pins at the same time

Is it possible to control multiple pins at once..

ex

int ledPinRed = 1,2,3 ;

or even such as

digitalWrite(13-15, HIGH );

???

To answer your question... YES

But not like you put it :

int ledPinRed = 1,2,3 ;

or even such as

digitalWrite(13-15, HIGH );

You can with these methodes.

Methode 1. See http://www.arduino.cc/playground/Learning/PortManipulation

Methode 2. See http://arduino.cc/en/Tutorial/ShiftOut

But if you insist of you method, the right way is :

int led1=12;
int led2=11;
int led3;10

void setup()
{
   pinMode( led1, OUTPUT);
   pinMode( led2, OUTPUT);
   pinMode( led3, OUTPUT);

}

void loop()
{

   digitalWrite(led1, HIGH);
  digitalWrite( led2, HIGH);
  digitalWrite( led3, HIGH);

// The rest of the code

}

The pins will not be "exactly" at the same time, but very close, depending of the application.

thank you ,, i guess the standard way is the best way.

int led1=12;
int led2=11;
int led3;10

void setup()
{
   pinMode( led1, OUTPUT);
   pinMode( led2, OUTPUT);
   pinMode( led3, OUTPUT);

}

void loop()
{

   digitalWrite(led1, HIGH);
  digitalWrite( led2, HIGH);
  digitalWrite( led3, HIGH);

// The rest of the code

}

i was trying to setup not for the fact to have them change simultaneously but because my code is very long and im trying to make it smaller. i figured if i could do it this way it would make it smaller.

int led1=12;
int led2=11;
int led3=10;
int leds = led1,led2,led3;

void setup()
{
   pinMode( leds, OUTPUT);
   

}

void loop()
{

   digitalWrite(leds, HIGH);
  

// The rest of the code

}

How about something like this…

int leds[] = {12,11,10};

void setup()
{
  for (int i=0; i<3; i++)
  {
    pinMode(leds[i], OUTPUT);
  }

}

void loop()
{
  for (int i=0; i<3; i++)
  {
    digitalWrite(leds[i], HIGH);
  }

  // The rest of the code

}

thats actually exactly what i want.

int leds[] = {12,11,10};

void setup()
{
     pinMode(leds[], OUTPUT);
  }

void loop()
{
     digitalWrite(leds[], HIGH);
  }

would it be possible anyway to not use the for statement… can u call an array to do something??

im new to c i write php and xml for a living… c andc++ is greek to me.

No you will have to use the for loop. In the code you just posted, i hasn’t been declared a variable at all and certainly hasn’t been given a value.

alright i think tht actually does make my code small enough i was over by about 10bytes this just may help’

could u quickly xplain how the array works here…

int i=0; i<3; i++)

so defining i as 0 we do it 3 times and go up by 2 each time??++ y the (2) ++

thnk you.
RKN2008

The ++ means increment by one its the same as i = i + 1;

Arrays work like a p.o. box. The value is the mail inside, the number like : data[0], data[1],data[2], ect are the box numbers. The for () loop simply the mailman puting the mail inside of each mail box number. And 0, is the first address box of an arrays.

if you don't want to write out the for loop everywhere you can just make your own function that takes an array and pin mode and does the loop.

If you want to reduce the size of your code, post it - your earlier comments make it likely that there is potential for shrinking it.

Something like this would work

Void multipin( int pinstart, int pinend, int status){
for(int i = pinstart; i <= pinend, i++){
digitalWrite(i,status);
}
}

Then when you want pins 3-6 on you put
multipin(3,6,1);
then off multipin(3,6,0);

@winner10920

Clever idea.

But at the end of the day, all these clever manipulations aren't true "simultaneous" capability. It would be interesting to know if there's a way to manipulate several pins truly simultaneously. I would be especially interested to know because I'm doing fast logic-controlled switching and true simultaneity would be a small but non-trivial performance increase.

I would be especially interested to know because I'm doing fast logic-controlled switching and true simultaneity would be a small but non-trivial performance increase.

If you utilize direct port access I/O commands, writing directly to the pin port(s) registers you can have up to 8 output pins change simultaneously, if they share the same port.

http://www.arduino.cc/playground/Learning/PortManipulation

Verdris:
I would be especially interested to know because I’m doing fast logic-controlled switching and true simultaneity would be a small but non-trivial performance increase.

You might be surprised (at least I was) Obviously it depends on specifically what you’re doing, but for instance converting the LiquidCrystal library to use a fastDigitalWrite variant alone gains 30% performance improvement. Changing the fastDigitalWrites to direct port manipulation only increases performance an additional 0.06% from there.