Wednesday, September 5, 2012

A step by step for 'knight ridering'

I said I'd do a writeup on the 'knight rider' effect I posted on twitter the other day, so here goes.

The circuit is dead simple, it's just six LEDs connected to the PWM pins (marked with a ~ on the board) of the Arduino, through current limiting resistors. If i were doing this in a 'production' setting, I would probably drive the LEDs with transistors and an external regulator/voltage source, to ensure that I'm not drawing too much current from the Arduino. I didn't do that in this case because I only have 5 transistors laying around, and also it was just a quick build.

An overview of the setup. Very simple.

We connect the output of the PWM pins to the anode of the LEDs, that's the long leg, remember, and the cathodes to ground. The resistor value isn't a huge concern in this particular setup, but these are 200 ohms.

The PWM pins (that is, pins which can be controlled with analogWrite() in the Arduino IDE) are marked with a ~.
The code I have for this particular animation was written a year ago, and probably isn't the very best approach, but it works and is fairly readable. If you're not familiar with programming, this might seem fairly esoteric, and for that I apologize. If you have any questions you can feel free to get in touch with me and I'll try to answer them.


 int brightness = 0;   
 //these two variables are used to store our brightness level during manipulations  
 int brightnessRev = 255;  
 int fadeUp = 5;   
 //the values by which each 'step' of the fade is incremented.  
 int fadeDown = 5;  
 int myPins[] = {3, 5, 6, 9, 10, 11};   
 /*here we are storing our output pins in an array, that allows us to   
 access the 'next' pin without hard-coding in the program itself  
 if we wanted to change the pin assignments, we could do it once,   
 right here, and everything else in the code would stay the same.*/  
 void setup() {   
  //our setup is very simple, we just need to configure each pin as an output  
  pinMode(3, OUTPUT);  
  pinMode(5, OUTPUT);  
  pinMode(6, OUTPUT);  
  pinMode(9, OUTPUT);  
  pinMode(10, OUTPUT);  
  pinMode(11, OUTPUT);  
 }  
 void loop() {   
  /*this program basically just increments through each LED, slowly fading it up,   
  then moving on to the next and doing the same, until all LEDs are on, then  
  starting from the first LED, fades them down. Then the process is repeated in   
  reverse, creating a sort of bouncing effect. */  
  for(int i=0; i < 6; i++) {  
   brightness = 0;  
   while(brightness < 255){  
    analogWrite(myPins[i], brightness);  
    brightness += fadeUp;  
    delay(1);  
   }  
  }  
  //basically the goal here is to iterate a 'fade up' routine   
  //on each output pin, which is what this for loop does  
  for(int i=0; i <6; i++) {  
  brightnessRev = 255;   
   while(brightnessRev >= 0){  
    analogWrite(myPins[i], brightnessRev);  
    brightnessRev -= fadeDown;  
    delay(1);  
   }  
  }  
  //this for loop does the same thing,   
  //but fading the LEDs down.  
  delay(125);  
  //this delay gives us a slight pause before   
  //starting the reverse course  
  for(int i=5; i >= 0; i--) {  
   brightness = 0;  
   while(brightness < 255){  
    analogWrite(myPins[i], brightness);  
    brightness += fadeUp;  
    delay(1);  
   }  
  }  
  for(int i=5; i >= 0; i--) {  
  brightnessRev = 255;   
   while(brightnessRev >= 0){  
    analogWrite(myPins[i], brightnessRev);  
    brightnessRev -= fadeDown;  
    delay(1);  
   }  
  }  
  delay(125);  
 }