Skip to content

Instantly share code, notes, and snippets.

@skarcha
Created November 29, 2011 00:12
Show Gist options
  • Select an option

  • Save skarcha/1402728 to your computer and use it in GitHub Desktop.

Select an option

Save skarcha/1402728 to your computer and use it in GitHub Desktop.

Revisions

  1. skarcha created this gist Nov 29, 2011.
    57 changes: 57 additions & 0 deletions gistfile1.ino
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,57 @@
    /*
    Knight Rider led simulation for Arduino
    By Antonio Perez <aperez@skarcha.com>
    Public Domain
    */
    const int analogInPin = A0;
    const int ledPins[] = {3, 5, 6, 9, 10, 11};
    const int pinCount = 6;
    const int brightAmount = (255 / (pinCount / 3));

    int sensorValue = 0;
    int timer = 20;
    int bright = 0;

    void setup() {
    for (int thisPin = 0; thisPin < pinCount; thisPin++) {
    pinMode(ledPins[thisPin], OUTPUT);
    }
    }

    void setBright (int led) {
    analogWrite(ledPins[led], bright);
    bright -= brightAmount;
    if (bright < 0) {
    bright = 0;
    }
    }

    void loop() {
    int i = 0;

    sensorValue = analogRead (analogInPin);
    timer = map(sensorValue, 0, 1023, 20, 1000);

    // loop from the lowest pin to the highest:
    for (int thisPin = 0; thisPin < pinCount; thisPin++) {
    i = thisPin;
    bright = 255;
    while (i >= 0) {
    setBright(i);
    i--;
    }
    delay(timer);
    }

    // loop from the highest pin to the lowest:
    for (int thisPin = pinCount - 1; thisPin >= 0; thisPin--) {
    i = thisPin;
    bright = 255;
    while (i < pinCount) {
    setBright(i);
    i++;
    }
    delay(timer);
    }
    }