Lauflicht

Die nachfolgenden Programme animieren ein aus mehreren Leuchdioden bestehendes Lauflicht.

Die Leuchtdioden werden an den Pins 2 bis 9 angeschlossen

Lauflicht in eine Richtung

const int DELAY_MS = 500;
 
const int PIN[] = { 2, 3, 4, 5 };
const int PIN_COUNT = 4;
 
int currentLed;
 
void setup() {
    int i = 0;
    while (i < PIN_COUNT) {
        pinMode(PIN[i], OUTPUT);
        i = i + 1;
    }
 
    currentLed = 0;
    digitalWrite(PIN[currentLed], HIGH);
}
 
void loop() {
    digitalWrite(PIN[currentLed], LOW);
    currentLed = currentLed + 1;
    if (PIN_COUNT <= currentLed) {
        currentLed = 0;
    }
 
    digitalWrite(PIN[currentLed], HIGH);
    delay(DELAY_MS);
}

Hin- und herbewegendes Lauflicht

Bei dieser Variante bewegt sich das Lauflicht hin und her.

const int DELAY_MS = 100;
const int PIN[] = { 2, 3, 4, 5, 6, 7, 8, 9 };
const int PIN_COUNT = 8;
 
int currentLed;
int dir;
 
void setup() {
    int i = 0;
    while (i < PIN_COUNT) {
        pinMode(PIN[i], OUTPUT);
        i = i + 1;
    }
 
    currentLed = 0;
    dir = 1;
    digitalWrite(PIN[currentLed], HIGH);
}
 
void loop() {
    digitalWrite(PIN[currentLed], LOW);
    currentLed = currentLed + dir;
    if (currentLed < 0 || PIN_COUNT <= currentLed) {
        dir = -dir;
        currentLed = currentLed + dir;
    }
 
    digitalWrite(PIN[currentLed], HIGH);
    delay(DELAY_MS);
}

Harmonische Schwingung

Bei dieser Variante stellt die Bewegung eine harmonische Schwingung dar.

const int DELAY_MS = 100;
const int PIN[] = { 2, 3, 4, 5, 6, 7, 8, 9 };
const int PIN_COUNT = 8;
 
int currentLed;
int dir;
 
void setup() {
    int i = 0;
    while (i < PIN_COUNT) {
        pinMode(PIN[i], OUTPUT);
        i = i + 1;
    }
 
    currentLed = 0;
    dir = 1;
}
 
void loop() {
    digitalWrite(PIN[currentLed], LOW);
    currentLed = currentLed + dir;
    if (currentLed < 0 || PIN_COUNT <= currentLed) {
        dir = -dir;
        currentLed = currentLed + dir;
    }
 
    digitalWrite(PIN[currentLed], HIGH);
    double maxAmp = PIN_COUNT / 2.0;
    double currAmp = currentLed - maxAmp;
    delay(DELAY_MS * abs(sin(currAmp / maxAmp)) + 100);
}