En aquesta pràctica veurem com podem fer que una seqüencia de leds es vagin encenen i apagant seguint un ordre. Aquest efecte es coneix com a “Kinght rider” ja que simula l’escaner que portava el Pontiac Firebird de la famosa sèrie dels anys 80.

Aquí podeu veure un vídeo on ens mostra l’efecte que fa:

Material necessari:

  • Un Arduino
  • Una protoboard
  • 6 leds
  • 6 resistències de 330 ohms
  • Cables per connectar

 

El muntatge seria el següent:

Loop_bb

I el codi de programa seria el següent:

/*
  For Loop Iteration

 Demonstrates the use of a for() loop.
 Lights multiple LEDs in sequence, then in reverse.

 The circuit:
 * LEDs from pins 2 through 7 to ground

 created 2006
 by David A. Mellis
 modified 30 Aug 2011
 by Tom Igoe 

This example code is in the public domain.

 http://www.arduino.cc/en/Tutorial/ForLoop
 */

int timer = 100;           // The higher the number, the slower the timing.

void setup() {
  // use a for loop to initialize each pin as an output:
  for (int thisPin = 2; thisPin < 8; thisPin++)  {
    pinMode(thisPin, OUTPUT);
  }
}

void loop() {
  // loop from the lowest pin to the highest:
  for (int thisPin = 2; thisPin < 8; thisPin++) { // turn the pin on: digitalWrite(thisPin, HIGH); delay(timer); // turn the pin off: digitalWrite(thisPin, LOW); } // loop from the highest pin to the lowest: for (int thisPin = 7; thisPin >= 2; thisPin--) {
    // turn the pin on:
    digitalWrite(thisPin, HIGH);
    delay(timer);
    // turn the pin off:
    digitalWrite(thisPin, LOW);
  }
}
Comparteix això: