mpg28
Published

Knight Rider LED's Variable Speed

LED's go back and forth and their speed controlled by the knob.

BeginnerShowcase (no instructions)656
Knight Rider LED's Variable Speed

Things used in this project

Hardware components

LED (generic)
LED (generic)
×16
Resistor 330 ohm
Resistor 330 ohm
×16
Rotary potentiometer (generic)
Rotary potentiometer (generic)
×1
Arduino UNO
Arduino UNO
×1

Story

Read more

Schematics

Knight Rider

Code

Knight Rider

Arduino
//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);
  }
}

Credits

mpg28
7 projects • 2 followers
Contact

Comments

Please log in or sign up to comment.