Tutorial.Array History

Hide minor edits - Show changes to markup

June 04, 2010, at 07:22 PM by Equipo Traduccion -
Changed lines 1-2 from:

Examples > Control Structures

to:

Ejemplos > Estructuras de control

Changed lines 5-17 from:

This variation on the For Loop example shows how to use an array. An array is a variable with multiple parts. If you think of a variable as a cup that holds values, you might think of an array as an ice cube tray. It's like a series of linked cups, all of which can hold the same maximum value.

The For Loop example shows you how to light up a series of LEDs attached to pins 2 through 7 of the Arduino. But the limitation is that the pins have to be numbered contiguously, and they have to be turned on in sequence. This example shows you how you can turn on a sequence of pins whose numbers are neither contiguous nor necessarily sequential. To do this is, you can put the pin numbers in an array and then use for loops to iterate over the array.

This example makes use of 6 LEDs connected to the pins 2 - 7 on the board using 220 Ohm resistors, just like in the For Loop. However, here the order of the LEDs is determined by their order in the array, not by their physical order.

This technique of putting the pins in an array is very handy. You don't have to have the pins sequential to one another, or even in the same order. You can rearrange them however you want.

Circuit

click the image to enlarge

to:

Esta variación del ejemplo For Loop muestra como usar un array. Un array o matriz es una variable con múltiples partes. Si piensas en una variable como en una taza que contiene valores, podrías pensar en un array como una bandeja de cubitos de hielo. Es como una serie de tazas relacionadas que pueden contener el mismo valor máximo.

El ejemplo For Loop enseña como encender una serie de LEDs conectados a los pines desde el 2 hasta el 7 de Arduino, pero con la limitación de que deben ser todos los pines consecutivos y se tienen que encender secuencialmente. Este ejemplo sin embargo, enseña como puedes encender una secuencia de pines cuyos numeros no son ni contiguos ni necesariamente secuenciales. Para hacer esto, puedes poner los numeros de los pines en un array y usar un bucle for para recorrer el array.

Este ejemplo hace uso de 6 LEDs conectados a los pines desde el 2 hasta el 7 en la placa usando resistencias de 220 Ohmios, igual que en el For Loop. Sin embargo, el orden de los LEDs es determinado por su orden en el array, no por su ordenamiento físico.

Esta técnica de poner los números de pin en un array es muy útil. No necesitas tener los pines secuencialmente ni en el mismo orden. Además puedes re-ordenarlos cuando quieras y como quieras.

Circuito

Pulsa en la imagen para agrandar

Changed lines 19-25 from:

image developed using Fritzing. For more circuit examples, see the Fritzing project page

Schematic:

click the image to enlarge

to:

image developed using Fritzing. Para más circuitos de ejemplo visita: Fritzing project page

Esquema:

Pulsa en la imagen para agrandar

Changed line 28 from:

Code

to:

Código

February 24, 2010, at 04:07 AM by Tom Igoe -
Changed lines 32-51 from:
 /*
   Arrays
  
  Demonstrates the use of  an array to hold pin numbers
  in order to iterate over the pins in a sequence. 
  Lights multiple LEDs in sequence, then in reverse.
  
  Unlike the For Loop tutorial, where the pins have to be
  contiguous, here the pins can be in any random order.
  
  The circuit:
  * LEDs from pins 2 through 7 to ground
  
  created 2006
  by David A. Mellis
  modified 5 Jul 2009
  by Tom Igoe 
  
  http://www.arduino.cc/en/Tutorial/Array
  */
to:
Deleted lines 33-67:
 int timer = 100;           // The higher the number, the slower the timing.
 int ledPins[] = { 
   2, 7, 4, 6, 5, 3 };       // an array of pin numbers to which LEDs are attached
 int pinCount = 6;           // the number of pins (i.e. the length of the array)

 void setup() {
   int thisPin;
   // the array elements are numbered from 0 to (pinCount - 1).
   // use a for loop to initialize each pin as an output:
   for (int thisPin = 0; thisPin < pinCount; thisPin++)  {
     pinMode(ledPins[thisPin], OUTPUT);      
   }
 }

 void loop() {
   // loop from the lowest pin to the highest:
   for (int thisPin = 0; thisPin < pinCount; thisPin++) { 
     // turn the pin on:
     digitalWrite(ledPins[thisPin], HIGH);   
     delay(timer);                  
     // turn the pin off:
     digitalWrite(ledPins[thisPin], LOW);    

   }

   // loop from the highest pin to the lowest:
   for (int thisPin = pinCount - 1; thisPin >= 0; thisPin--) { 
     // turn the pin on:
     digitalWrite(ledPins[thisPin], HIGH);
     delay(timer);
     // turn the pin off:
     digitalWrite(ledPins[thisPin], LOW);
   }
 }
August 27, 2009, at 08:47 PM by Tom Igoe -
Added lines 20-22:

image developed using Fritzing. For more circuit examples, see the Fritzing project page

July 06, 2009, at 08:12 PM by Tom Igoe -
Changed line 66 from:
   for (int thisPin = 0; i < pinCount; thisPin++) { 
to:
   for (int thisPin = 0; thisPin < pinCount; thisPin++) { 
Changed line 76 from:
   for (thisPin = pinCount - 1; thisPin >= 0; thisPin--) { 
to:
   for (int thisPin = pinCount - 1; thisPin >= 0; thisPin--) { 
July 05, 2009, at 09:04 PM by Tom Igoe -
Added lines 1-85:

Share