array

Hey guys,

So I have this code:

pinMode (0, OUTPUT);
.....to....
pinMode(9, OUTPUT);

and

digitalWrite(0, HIGH);
...to...
digitalWrite(9, HIGH);

Instead of writing all of the pinMode or digitalWrite, I'm trying to do array so it look more neat.
I have this, but don't know what's missing.

int blue [] = new int [9];

for(int x = 0; x < blue.length; x++)
{
blue[x];
}

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

void loop ()
{
digitalWrite(blue[9], HIGH);
delay(100);
digitalWrite(blue[9],LOW);
delay(100);

}

int pins[] = {1,2,3,4,5};  // for example

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

void loop () {
    digitalWrite (pins[4], HIGH);
    delay(100);
    digitalWrite (pins[4], HIGH);
    delay(100);
}

Rob

Inside the for loop of your setup(), you'll use this:

pinMode (x + 1, OUTPUT); // It's indexed with x + 1 because your array begins with an index of 0, not 1, and your pins begin with 1.

Inside your loop() function, you'll use a similar for loop for both the HIGH and LOW setting of your pins, with the delays in between. Like this:

int x; // declare this in your loop() as well, because it falls out of scope once you're out of the setup() function. 

// loop through your pins, setting them high.
for (x = 0; x < 9; x++)
{
   digitalWrite(x + 1, HIGH);
}

// wait.
delay(100);

// loop through your pins, setting them low.
for (x = 0; x < 9; x++)
{
   digitalWrite(x + 1, LOW);
}

// wait again.
delay(100);

And that's it. :wink:

Of course, I'm making the assumption that you want to set all your pins to high at once, and that all of your pins are attached to LEDs. If that assumption is correct, then you don't even need your array. But if it isn't, you would need your array. Here's what I did for mine:

/* Global Variables */
// PinArray - An array of integers. A value of 0 denotes an unused pin;
//            A value of 1 denotes an LED;
//            A value of 2 denotes a buzzer.
//            A value of 3 denotes a button.
int PinArray[] = {0, 0, 2, 0, 0, 1, 0, 3, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 0};

// SpeakerPin - An integer denoting the pin number of the buzzer/speaker.
int SpeakerPin = 3;

// switchPin - an integer denoting the pin number of the button
int switchPin = 8;

// switchValue - an integer for storing the current state of the button
int switchValue;

// radarState - an integer for storing the current state of the dragon radar
int radarState;

/*********************************** Setup() **********************************/
/* Initializing function for the LilyPad's pins. Sets LED pins and buzzer     */
/* pins to output, and all others to input.                                   */
/*                                                                            */
/* Input Parameters: none                                                     */
/* Return Type: void                                                          */
/******************************************************************************/
void setup() 
{ 
  /* Local Variables */
  // i - Integer loop counter variable.
  int i;  

  // for: Loop through PinArray, setting output/input status to pins 
  for (i = 0; i < 20; i++)
  { // begin for
    // switch: determine which value is set for current pin.
    switch(PinArray[i])
    { // begin switch
      // if the value is 0, set pin to INPUT. This pin is not being used.
      case 0:
        pinMode(i + 1, INPUT);
        break;
        
      // if the value is 1, set pin to OUTPUT. An LED is using this pin.
      case 1:
        pinMode(i + 1, OUTPUT);
        break;
        
      // if the value is 2, set pin to OUTPUT. A buzzer is using this pin.
      case 2:
        pinMode(i + 1, OUTPUT);
        break;
        
      // if the value is 3, set pin to INTPUT. A button is using this pin.
      case 3:
        pinMode(i + 1, INPUT);
        
        // Set up serial communication at 9600bps
        Serial.begin(9600);
        
        // sets the default (unpressed) state of switchPin to HIGH
        digitalWrite(switchPin, HIGH);
        
        // initialize radar to "off".
        radarState = 0;
        
        break;
    } // end switch
  } // end for
} // end setup()

From what I seen so far, here is my array.

void setup() 
{ 
for (int x = 0; x < 9; x++)
  {
  pinMode (x + 1, OUTPUT);   
  }  
}

void loop()
{
  int x;
  
for (x = 0; x < 9; x++)
  {
   digitalWrite(x + 1, HIGH);
  }

delay(100);

for (x = 0; x < 9; x++)
  {
   digitalWrite(x + 1, LOW);
  }
delay(100);

}

Does this array means that from pin 1 to pin 9, every LEDs will turn on for 100milliseconds and turn off for 100milliseconds?

There is no array there, but whatever. You have a couple of loops.

It would look a bit more logical if you stuck to 1 to 9, like this:

for (x = 1; x <= 9; x++)
  {
   digitalWrite(x, HIGH);
  }

Does this mean that... every LEDs will turn on for 100milliseconds and turn off for 100milliseconds?

All the LEDs will turn on, it will wait 100 ms, then they will all turn off (together).

If you want them to turn on individually you reorganize a bit:

for (x = 1; x <= 9; x++)
  {
   digitalWrite(x, HIGH);
   delay(100);
   digitalWrite(x, LOW);
   delay(100);
  }

The good thing about using the array is that pins can be non contiguous and also it's easy to change the pins used in future. Being able to change pins doesn't really matter for this simple example, but if the program grows and/or moves to a Mega it makes life a lot easier.


Rob