Diferencia entre revisiones de «Arrays»

De ArduWiki
Saltar a: navegación, buscar
Línea 1: Línea 1:
 +
Esta variación en el ejemplo For Iop de iteración muestra cómo usar una matriz. Una matriz es una variable con varias partes. Si piensas en una variable como una taza que contiene valores, podrías pensar en una matriz como una bandeja de cubitos de hielo. Es como una serie de tazas vinculadas, todas las cuales pueden tener el mismo valor máximo.
 +
 +
El ejemplo For Iop de iteración muestra cómo encender seis (6) LED conectados a los pines 2 a 7 de la placa Arduino o Genuino, con ciertas limitaciones (los pines deben numerarse contiguamente, y los LED deben encenderse en secuencia).
 +
 +
Este ejemplo muestra cómo puede activar una secuencia de pines cuyos números no son contiguos ni necesariamente secuenciales. Para hacer esto, puede poner los números de pin en una matriz y luego usar los bucles para iterar sobre la matriz.
 +
 +
Esta técnica de poner los pines en una matriz es muy útil. No tiene que tener los pines secuenciales entre sí, o incluso en el mismo orden. Los puedes reorganizar en el orden que desee.
  
 
== Placa aplicable ==
 
== Placa aplicable ==
Línea 4: Línea 11:
  
 
== Código ==
 
== Código ==
 +
Este ejemplo usa 6 LED conectados a los pines digitales 2~7 en la placa con resistencias de 220 ohm, como en For Loop. Sin embargo, aquí el orden de los LED está determinado por su orden en la matriz, no por su orden físico.
 +
 
<syntaxhighlight lang="c++">
 
<syntaxhighlight lang="c++">
 +
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() {
 +
  // 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--) {
 +
      digitalWrite(ledPins[thisPin], HIGH); //Prende LED
 +
      delay(timer);
 +
      // turn the pin off:
 +
      digitalWrite(ledPins[thisPin], LOW);  //Apaga LED
 +
  }
 +
}
 
</syntaxhighlight>
 
</syntaxhighlight>
  
Línea 15: Línea 53:
 
* [[Fox loop iteration]]
 
* [[Fox loop iteration]]
 
* [[While statement conditional]]
 
* [[While statement conditional]]
 +
 +
== Referencias ==
 +
* [https://www.arduino.cc/en/Tutorial/Arrays Ejemplos de Arduino]
  
 
[[Category:Estructuras de control]]
 
[[Category:Estructuras de control]]

Revisión del 17:49 26 jun 2018

Esta variación en el ejemplo For Iop de iteración muestra cómo usar una matriz. Una matriz es una variable con varias partes. Si piensas en una variable como una taza que contiene valores, podrías pensar en una matriz como una bandeja de cubitos de hielo. Es como una serie de tazas vinculadas, todas las cuales pueden tener el mismo valor máximo.

El ejemplo For Iop de iteración muestra cómo encender seis (6) LED conectados a los pines 2 a 7 de la placa Arduino o Genuino, con ciertas limitaciones (los pines deben numerarse contiguamente, y los LED deben encenderse en secuencia).

Este ejemplo muestra cómo puede activar una secuencia de pines cuyos números no son contiguos ni necesariamente secuenciales. Para hacer esto, puede poner los números de pin en una matriz y luego usar los bucles para iterar sobre la matriz.

Esta técnica de poner los pines en una matriz es muy útil. No tiene que tener los pines secuenciales entre sí, o incluso en el mismo orden. Los puedes reorganizar en el orden que desee.

Placa aplicable

Todas.

Código

Este ejemplo usa 6 LED conectados a los pines digitales 2~7 en la placa con resistencias de 220 ohm, como en For Loop. Sin embargo, aquí el orden de los LED está determinado por su orden en la matriz, no por su orden físico.

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() {
   // 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--) {
      digitalWrite(ledPins[thisPin], HIGH); //Prende LED
      delay(timer);
      // turn the pin off:
      digitalWrite(ledPins[thisPin], LOW);  //Apaga LED
   }
}

Comentarios

Vea también

Referencias