Created
November 29, 2011 00:12
-
-
Save skarcha/1402728 to your computer and use it in GitHub Desktop.
Revisions
-
skarcha created this gist
Nov 29, 2011 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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); } }