//Knight Rider Variable Speed
int pinArray[] = {0,2,3,4,5,6,7,8,9,10,11,12,13,19,18,17,16,0}; // Increase zeros at end of array for pause.
int pinCount = 18;
int activePin = 2;
int dx = 1;
int pot = 0;
int mintime = 0;
void setup() {
Serial.begin(9600);
for (int i=0; i< pinCount; i++) {
if (pinArray[i] != 0) {
pinMode(pinArray[i], OUTPUT);
}
}
}
void loop() {
for (int i=0; i<pinCount; i++) {
digitalWriteNot0(pinArray[i], LOW);
}
if (activePin == (pinCount-1)) {
activePin = (pinCount - 2);
dx = -1;
}
if (activePin == 0) {
activePin = 1;
dx = 1;
}
activePin += dx;
pot = analogRead(0);
if (pot < 1023) {
digitalWriteNot0(pinArray[activePin], HIGH);
// digitalWriteNot0(pinArray[activePin-dx], HIGH); // Remove for less active LEDs
// digitalWriteNot0(pinArray[activePin-2*dx], HIGH); // Remove for less active LEDs
}
Serial.print(pot);
Serial.print(" - ");
Serial.println(activePin);
delay(mintime+analogRead(0)/4);
}
void digitalWriteNot0(int pin, boolean state) {
if (pin > 0) {
digitalWrite(pin, state);
}
}
Comments
Please log in or sign up to comment.