Diferencia entre revisiones de «Arrays»

De ArduWiki
Saltar a: navegación, buscar
(Código)
Línea 14: Línea 14:
  
 
<syntaxhighlight lang="c++">
 
<syntaxhighlight lang="c++">
int timer = 100;          // The higher the number, the slower the timing.
+
cons byte led[] = {2, 7, 4, 6, 5, 3};   //Pines de cada LED
int ledPins[] = {2, 7, 4, 6, 5, 3};       // an array of pin numbers to which LEDs are attached
+
cons byte pinCount = 6;                 //Numero de LEDs
int pinCount = 6;           // the number of pins (i.e. the length of the array)
+
cons byte tiempo = 300;                  //Retardo
  
 
void setup() {
 
void setup() {
  // the array elements are numbered from 0 to (pinCount - 1).
+
   for (byte n=0; n<pinCount; n++) {
  // use a for loop to initialize each pin as an output:
+
       pinMode(led[n], OUTPUT);
   for (int thisPin = 0; thisPin<pinCount; thisPin++) {
 
       pinMode(ledPins[thisPin], OUTPUT);
 
 
   }
 
   }
 
}
 
}
  
 
void loop() {
 
void loop() {
   // loop from the lowest pin to the highest:
+
   //Ascendente
   for (int thisPin = 0; thisPin < pinCount; thisPin++) {
+
   for (byte n=0; n<pinCount; n++) {
      // turn the pin on:
+
       digitalWrite(led[n], HIGH); //Prende LED
       digitalWrite(ledPins[thisPin], HIGH);
+
       delay(tiempo);
       delay(timer);
+
       digitalWrite(led[n], LOW);   //Apaga LED
      // turn the pin off:
 
       digitalWrite(ledPins[thisPin], LOW);
 
 
   }
 
   }
   // loop from the highest pin to the lowest:
+
   //Descendente
   for (int thisPin=pinCount - 1; thisPin>=0; thisPin--) {
+
   for (byte n=pinCount - 1; n>=0; n--) {
       digitalWrite(ledPins[thisPin], HIGH); //Prende LED
+
       digitalWrite(led[n], HIGH); //Prende LED
       delay(timer);
+
       delay(tiempo);
      // turn the pin off:
+
       digitalWrite(led[n], LOW);   //Apaga LED
       digitalWrite(ledPins[thisPin], LOW); //Apaga LED
 
 
   }
 
   }
 
}
 
}

Revisión del 17:58 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.

cons byte led[] = {2, 7, 4, 6, 5, 3};    //Pines de cada LED
cons byte pinCount = 6;                  //Numero de LEDs
cons byte tiempo = 300;                   //Retardo

void setup() {
   for (byte n=0; n<pinCount; n++) {
      pinMode(led[n], OUTPUT);
   }
}

void loop() {
   //Ascendente
   for (byte n=0; n<pinCount; n++) {
      digitalWrite(led[n], HIGH);  //Prende LED
      delay(tiempo);
      digitalWrite(led[n], LOW);   //Apaga LED
   }
   //Descendente
   for (byte n=pinCount - 1; n>=0; n--) {
      digitalWrite(led[n], HIGH);  //Prende LED
      delay(tiempo);
      digitalWrite(led[n], LOW);   //Apaga LED
   }
}

Comentarios

Vea también

Referencias