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

Thursday, August 2, 2012

Some Math, or Way Too Much Text

Okay, so I'm out of tech and need to start back in on this subject. I need to buy another Arduino because mine is buried in the set, but I can still write about some details that are important.

Let's say you've got a mess of LEDs and a request to make some things light up. You don't need any fancy effects, just static light in some practicals or mounted somewhere on your set. It's not as straightforward as connecting the LEDs to a battery or wall wart and calling it done. An LED is not a resistive load, like a light bulb. The current through an LED is determined by the resistive elements of the circuit, while the voltage drop across its terminals is constant (Anywhere from 1.7V up to 3.5V are common values, depending on the color and brightness of the LED). What this means is that wherever you're using an LED, you'll probably use what is known as a current limiting resistor. This resistor is how you select the current flowing through your LED.

You're likely at least passingly familiar with Ohm's law, that is: V = IR. This is probably the most basic electrical relationship, and one every electrician should know about. Voltage equals Current times Resistance. There's a load of resources out there on the web that will tell you more than you ever wanted to know, so I'll speak specifically to how we want to manipulate this equation to calculate the value of our current limiting resistors.

So you've got a boatload of these diffused LEDs, like the ones I used in the buildings for As You Like It last year. If you scroll down that page, you'll see the specifications, and these are important. The big numbers to be aware of are the Forward Voltage and Continuous Forward Current. The forward voltage is the value required to make the LED light up (yes, it will probably glow at a lower level, but the color will be wonky and it will be very dim) and the forward current is how much current the LED is capable of handling before bad things happen. You can vary the current to vary the brightness (there are several techniques to accomplish this, and we'll talk about it), but you'll always need to provide these particular LEDs with 3.4V.

So say you're building a little glowing hand-prop. You already have rechargeable AA batteries on hand, and some 4 cell battery holders, so that's how you'd like to power the LED going into this prop. You want it as bright as possible, so you'd like to calculate the value of current limiting resistor that will provide you the 350mA of current we're looking for. It's simple. Our 4 AA batteries, in series, will provide us 4.8V (many rechargeable types provide only 1.2 volts per battery, rather than 1.5). We subtract the forward voltage of the LED, 3.4V and we have 1.4V 'left over.' Plug that into Ohm's law, along with our forward current of 350mA (For the record, the equation is in Volts, Amps, and Ohms, meaning we'll plug in 0.35 for our current), then divide to isolate R, giving us a value of 4.

This is a good place to mention that resistors come in sort of goofy values(wonder why?), so instead of our ideal 4 ohm resistor, we'll find either a 3.9 or 4.3 value. Typically err on the side of more resistance.

So now we've got our LED, our batteries, and a resistor value. Next we should determine what power rating we need for the resistor. If you push too much current through a resistor, it'll get quite hot (that's exactly how your toaster or electric oven works), so we want to be sure we have some headroom. Power can be calculated with one of two easy equations, either P = VI [power equals voltage times current] or P = I^2 * R [power equals current squared times resistance]. Using either of these, we discover that the power dissipated in our resistor is 0.49W. Resistors commonly come in 1/8, 1/4, 1/2 and 1 watt packages, so we could probably go with a 1/2W unit and be okay. Typically I will increase the resistance value, lowering the current below the maximum continuous forward current, this allows me more headroom with my resistors, reduces heat output of the LEDs (which is not completely negligible with these higher-powered units, and becomes something to account for with very high power units) and probably makes them last longer (though that's hardly a concern.)

There also exist numerous web-based calculators for finding these values, only a google search away. They'll often even spit out the 'real' resistor value and power rating. That's no fun, though.

The last thing you need to know is that an LED could be hooked up two ways, but will only function in one orientation. Don't worry, if you hook it up backwards, you won't hurt anything, it just won't light up, turn it around and try again. The two legs of the LED are commonly referred to as the anode and the cathode, the former being the 'positive' side and the latter being the 'negative.' The two legs will be different lengths, the longer one is the anode. You can also identify which is which visually (and you'll find yourself doing this at some point, no question), as I've indicated in the following photos. I've drawn an arrow pointing at the cathode, in each case.



What this means is that you'll connect the positive terminal of your battery or battery pack to the anode, and the negative terminal the cathode, with the resistor somewhere in between, it doesn't matter whether it goes before or after the LED, as long as it's in series. 

Your LED should be lit up, and once you add some sort of switching mechanism (just interrupt one of the wires going to the battery pack with the switch) your static LED should be stage-ready. If you find that it's too bright, you can increase the value of your resistor (and conversely, to make it brighter, reduce the value, but be aware that if you try to run more current than the continuous value in the specifications of the LED, you will probably significantly shorten the life of the LED, and possibly destroy it outright.) 

I hope this is helpful to some of you, look for more on the topic soonish. 

Sunday, July 15, 2012

Bits and Baubles, or the components you'll use.

If you follow me on twitter, you probably have seen me ramble on about LEDs. I think they're great, and believe they're one of the singularly most useful components a theatrical artist can keep stocked. I get most of mine from Super Bright LEDs. They have a good selection of products suitable for use in props and practicals, and their store is easy to browse, providing good information about each part. (I am a big fan of these in particular).

If you're getting into electronics for the first time, I recommend picking up some kind of starter kits for basic components: resistorscapacitors and maybe some other sundry bits. These basic components are important when it comes time to build any circuits. Don't forget to pick up that new Arduino!

That kit at SparkFun includes a few linear regulators, which will be an important thing to have on hand if you build your LED related props the way I do.

Getting power to the project is something you can accomplish in a few different ways. If it's a hand prop, then batteries are the obvious choice. The Arduino board contains regulators, and can easily powered with anything from 6-20v. This can be batteries, or a simple wall-wart if you project doesn't need to be mobile.

For initial design phases, I really recommend a decent-sized breadboard, this will allow you to build circuits without soldering, to test ideas or just play around with electrical concepts. You'll also want a multimeter, either a basic one, which will serve you just fine as long as you remember to turn it off, or a slightly fancier one. Auto-ranging is a nice feature to have, and might save you from blowing a fuse.

A soldering iron is important, and I will never recommend something from radio shack. This seems like a decent option, for relatively little cash. I'll admit my own soldering station is fancier, but I'm particular about my tools.

Basic hand tools, diagonal cutters, needlenose pliers, maybe one of those fancy automatic wire strippers. Hookup wire. Maybe some switches and cheap, low-power LEDs for output purposes. If you stick with electronics you'll see yourself gathering a fair bit of stuff, but it's all useful.

I don't think I've missed anything that you'll want to get started playing, but if you've got any questions definitely let me know.

Saturday, July 14, 2012

The Arduino in theatre, or why you should be putting computers in your sets.

I've decided that this subject might need to be split into a couple parts, for my own sanity in the organization of information, if nothing else.

For my money, the Arduino is the greatest thing to be available to theatre props and electrics departments for a long time. This $30 board gives anyone with a computer easy access to low-level computing. Microcontrollers have a lot going for them: They're cheap, they're tiny, and you can power them just about however you'd like. Until the development of the Arduino, there was a significant technical barrier to entry. One had to purchase a programmer and deal with datasheets hundreds of pages long. To accomplish basic tasks, registers had to be massaged and timers coaxed.

An example of running an atmega328 on protoboard.
The only support components here are the crystal just
to the right of the chip, the two capacitors, and whatever
you decide to use to provide 5v power.
No more. The Arduino platform offers the user an inexpensive, all-in-one prototyping and development platform. Using the capable Atmega 328, the Arduino offers plenty of I/O, flash and RAM for any prop/practical related purposes. Taking your Arduino projects off the dev board is trivial, and with the ability to purchase mega328 chips with the Arduino bootloader pre-installed for $5.50 (with the required support components costing another few dollars) any number of places online means your projects can fit anywhere you can find space for a small protoboard, available at your local decently-stocked Radio Shack.

A microcontroller is a small computer, built into a single chip. These computers contain everything needed to run programs, and offer basic logic-level outputs and inputs. They can be used for something as simple as blinking some LEDs in a repeated pattern, or as complex as your imagination allows. In the world of theatre, they allow us to build small, low-power dynamic effects that can fit anywhere, be built very quickly, and updated 'in-system' for fine-tuning.

Arduino is programmed with easy-to-read, C++-like syntax called Wiring. Here's a sample, from my latest project:


void increment() {
    for(j = 0; j < 4; j++) 
      values[j] += directions[j]; 
      analogWrite(leds[j], values[j]); 
      delay(10);     
}   
    for(i = 0; i < 4; i++) { 
      if (values[i] < 25 or values[i] > 175) { 
      directions[i] *= -1; 
    }
  }
}

As you can see, if you've done any programming at all in the past, this is pretty straightforward. The really useful additions are commands like 'analogWrite()' which offer easy, one-line access to powerful features of the microcontroller. analogWrite() is used to set a PWM (pulse-width modulation) level to one of the output pins, allowing you to easily dim an LED or control the speed of a motor.

The biggest strength of this approach to dynamic effects, in my mind, is that they can be programmed and put into place, then powered from a dimmer. This means you can have complex effects anywhere you have a free dimmer, without the need to run a control signal. Alternatively, plenty of folks have implemented DMX on the Arduino and other microcontrollers (and this is actually on my list of projects to tackle), meaning you could easily assemble inexpensive automation controllers or anything else you'd like to control from your light console.
Stuff everything in a box and hide it.
In my next post, in the next day or two, I intend to delve into the basic electronics knowledge you'll need to start building the most interesting props and practicals you've ever used. Stay tuned.