Skip to content

Instantly share code, notes, and snippets.

@skarcha
Created November 29, 2011 00:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save skarcha/1402728 to your computer and use it in GitHub Desktop.
Save skarcha/1402728 to your computer and use it in GitHub Desktop.
Knight Rider led simulation for Arduino
/*
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);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment