I love things that glow; that flicker and twinkle, fade and pulse.  Obviously then, I love LEDs!  Soft circuitry and electronic clothing facinates me, but like great swathes of fashion, I’m convinced that they’re usually doing it wrong.  The LEDs you see on clothing, frequently flashes, blinks or blinds – these LEDs I do not care for.  How, and why, I made the gentle glowing LEDs above, is below.

So I’m a fusspot – I admit this.  The only way I forgive myself for my episodes of fussiness is if I actually get off my backside and do better.  Otherwise, I’m just another armchair critic.

I’m in the process of having a new winter coat custom made – because I couldn’t find anything in the shops that I liked.  One of the things I’m talking to the designers about, is integrating some tasteful glowing LEDs.  No flashing, thank you.  So this weekend, I broke out the Arduino UNO, a handful of green LEDs and some 330Ω resistors and with the help of my bf, got the above, rather natural looking glowing effect.

It reminds me of the glow of fireflies, and I’m quite happy with it.

It uses sinewaves that have been wrangled a bit to create more of a ‘hanging garland’ shape.  I noticed that when trying a simple triangle wave (adding a set value all the way up, and then subtracting it all the way down), that the upper levels of glowiness weren’t very interesting to look at.  The interesting bit was in the first few moments after the LED turned on.  Thus, the garland wave shape minimizes time at maximum brightness, and hangs around longer at the lower brightnesses.

The Arduino sketch/code is below.  I’ve tidied it up a little, but it’s still code in a blog post.

// PWM pins are 3, 5, 6, 9, 10, 11, 12
// Master value of how many LEDs I want to have connected
int const howManyLeds = 5;
// The analogWrite command goes from 0 to 255
int const maxBrightness = 255;
// Which pins on the Arduino UNO support Pulse Witdh Modulation (PWM).
int pwmPins[] = {3,5,6,9,10,11,12};
// Create our array of actually used pins with LEDs attached.
int usedPins[howManyLeds];
// Each LED/pin has a different fade speed, we keep that speed multiplier in here.
float currentFadeAmount[howManyLeds];
// For each pin we are using, we also need a current brightness.
int currentBrightness[howManyLeds];
// The counter that we add to, and feed into the sin() function.
float timeAroundLoop;
// Number we add to our timeAroundLoop counter.  Make this bigger for faster pulses.
float timeAdditionValue = 0.005;
float tempSinVal;

// ------------------------------------------------------------

void setup(){  
// usedPins is our master destination list of pins.
  for (int i=0; i < howManyLeds; i++)
    // Fill my new array with the available PWM pins.
    usedPins[i] = pwmPins[i];
 // currentFadeAmount is our increment/decrement to our currentBrightness val.
  for (int i = 0; i < howManyLeds; i++)
    // Set the fading speed to be somewhat randomized.
    currentFadeAmount[i] = random(0.0,15.0);
 // Prepare the pins as outputs.  (This may not be needed, but it doesn't hurt.)
  for (int i = 0; i < howManyLeds; i ++)
    pinMode(usedPins[i], OUTPUT);

// ------------------------------------------------------------
void loop()
   // Increment our time counter every loop.
  timeAroundLoop = timeAroundLoop + timeAdditionValue;
  for (int i = 0; i < howManyLeds; i++)
    // Each LED fades fater or slower than each other - multiply time by speed.
    tempSinVal = timeAroundLoop*currentFadeAmount[i];

    // This gives us a sinewave value of the adjusted time.  Values from -1 to 1.
    tempSinVal = sin(tempSinVal);

    // Absolute the sine wave (no negatives) to give a bouncing ball style wave.
    tempSinVal = abs(tempSinVal);

    // Do 1-value, to turn the bouncing ball upside down.
    // It now looks like a hanging garland, just touching the ground.
    tempSinVal = 1-tempSinVal;

    // Set the brightness to this value, multiplied by our max brightness.
    // The scale now goes from 0-255 instead of 0-1.
    currentBrightness[i] = tempSinVal * maxBrightness;
    // Write this value to the pin.
    analogWrite(usedPins[i], currentBrightness[i]);

  // I probably don't need to do a delay here, I could just slow down the timeAroundLoop more.

I intend to get this working with the Arduino Lilypad that is winging its way to me, as well as some little LED beads.  Then, it’ll be a ‘simple’ matter of sewing them into patch-like add-ons for the coat, and wiring it up using snap-connectors.  What could possibly go wrong!