Pumpkin Flicker

We usually carve 6 pumpkins for Halloween. Lucky for me the Arduino has 6 PMW pins! I added a ULN2803 Darlington Array chip to be able to drive high power LEDs. To test the circuit I hooked up the first 6 LEDs that came out of the junk box.

Video:

Code:

// Random Flicker

int      led1Pin = 3;
int led2Pin = 5;
int      led3Pin = 6;
int      led4Pin = 9;
int      led5Pin = 10;
int      led6Pin = 11;

int      curValue1 = 0;
int      curValue2 = 0;
int      curValue3 = 0;
int      curValue4 = 0;
int      curValue5 = 0;
int      curValue6 = 0;

void setup()
{
      curValue1 = 100;
      curValue2 = 100;
      curValue3 = 100;
      curValue4 = 100;
      curValue5 = 100;
      curValue6 = 100;
      
      randomSeed( 5 );
}

void loop()
{
      long      rndValue;
      
      rndValue = random( -40, 50 );
      curValue1 += rndValue;
      if (curValue1 < 0)
      {
            curValue1 = 0;
      }
      if (curValue1 > 255)
      {
            curValue1 = 255;
      }
      analogWrite( led1Pin, curValue1 );

      rndValue = random( -40, 50 );
      curValue2 += rndValue;
      if (curValue2 < 0)
      {
            curValue2 = 0;
      }
      if (curValue2 > 255)
      {
            curValue2 = 255;
      }
      analogWrite( led2Pin, curValue2 );

      rndValue = random( -40, 50 );
      curValue3 += rndValue;
      if (curValue3 < 0)
      {
            curValue3 = 0;
      }
      if (curValue3 > 255)
      {
            curValue3 = 255;
      }
      analogWrite( led3Pin, curValue3 );

      rndValue = random( -40, 50 );
      curValue4 += rndValue;
      if (curValue4 < 0)
      {
            curValue4 = 0;
      }
      if (curValue4 > 255)
      {
            curValue4 = 255;
      }
      analogWrite( led4Pin, curValue4 );

      rndValue = random( -40, 50 );
      curValue5 += rndValue;
      if (curValue5 < 0)
      {
            curValue5 = 0;
      }
      if (curValue5 > 255)
      {
            curValue5 = 255;
      }
      analogWrite( led5Pin, curValue5 );

      rndValue = random( -40, 50 );
      curValue6 += rndValue;
      if (curValue6 < 0)
      {
            curValue6 = 0;
      }
      if (curValue6 > 255)
      {
            curValue6 = 255;
      }
      analogWrite( led6Pin, curValue6 );

      delay( 50 );
}

Nice! Would you mind posting video of the final result?

Apologies for dabbling in code that already works perfectly well, but in my opinion this would be more readable and concise if you were to use arrays to store your pin numbers and current values, something like:

// Random Flicker
#define LEDCOUNT 6
int ledPins[LEDCOUNT] = {3, 5, 6, 9, 10, 11};
int curValue[LEDCOUNT] = {100, 100, 100, 100, 100, 100};

void setup()
{
  randomSeed(analogRead(ledPins[0])); // a little trick for getting more random-like behavior
}

void loop()
{
  for (int i=0; i<LEDCOUNT; ++i)
  {
    long rndValue = random(-40, 50);
    curValue[i] += rndValue;
    if (curValue[i] < 0)
      curValue[i] = 0;
    else if (curValue[i] > 255)
      curValue[i] = 255;
    analogWrite(ledPin[i], curValue[i]);
    delay(50);
  }
}

Good luck with that!

Mikal

It sounds like a nice project.

You can get really fancy and have the 6 lights do related things like a pumpkin-chaser or one fades while one brightens.

Reading your code, it seems like that will give you a bunch of random flickers. This will be a lot harder to code, but you can have the randomizer choose a target value and then have it more gradually fade towards the target (doing this concurrently for 6 values will be a bit tricky to program, but using hardware PWM and interrupts will help a lot).

int ledPins[LEDCOUNT] = {3, 5, 6, 9, 10, 11};
randomSeed(analogRead(ledPins[0])); // a little trick for getting more

This will work just fine, but just to be clear you're doing the analog read on a different pin than the LED is actually on the LED is on digital pin 3, you're reading from analog pin 3. I think it only adds some confusion, and will cause problems if you try to analogRead(ledPins[4]);. Better to use analogReead(2); or some such.

Better to use analogReead(2); or some such.

Oh yes, Oracle is quite right! Sorry for the mistake.

mikalhart is right, I could have cleaned up the code a bunch. My goal was to write the first revision of the code as quickly as possible so I could get the hardware out of the way. I'm concerned about this project completion date slipping past Halloween!

I figured once I had usable hardware, I'd go back and allow myself to work the code over again. That way if version 2 doesn't get done in time, at least I'll have flickering pumpkins of some sort on my porch.

I got a bit nervous when version one of the hardware (with the LEDs hooked directly up to the pwm output pins) suddenly stopped working 2.5 days into a burn in test. All LEDs were at 100%, and no signs of life from the AtMega168. After cycling the power, nothing lit up at all. I put the chip back into an Arduino thinking it was probably toast, but I was able to re-download the sketch and all is well again. So I guess somehow the combined drain of the 6 LEDs (each one was around 20mA) somehow made the AtMega168 loose the sketch? I'm hoping the Darlington array keeps the AtMega168 happy.

Next I need to move the chips to protoboard (at least the AtMega168 will be socketed) so the dog walking by won't be able to pull out wires. Then I'll feel like I can get back to adding features.

I'd love to improve the flicker algorithm, add a wind switch, some sort of proximity detector, etc. I don't know if I'll add a dance pattern. For this year at least I just wanted to simulate the candles so I don't have to keep going out to re-light them.

thanks for sharing :slight_smile:
This looks to be alot of fun. One idea I had while reading this was to just carve the surface of the pumpkin, not all the way through, and use some superbright LED's to shine from the inside and light up the pumpkin carving without there being direct light.

Thanks for reminding me of that. I wondered how that would look as well.

Which brings up another question!

What kind of LEDs should I use? The Darlington Array can handle up to 500mA per LED. I could grab some generic "bright" white LEDs that say they are around 1000mcd, or I could go for a 1W smd LED on the little round pcb for each pumpkin, or I could get the neat RGB LEDs that come with an onboard communications chip.

As for color, white is fine for this year. In the future I'd like to tailor the color to more match a candle, but not yet. So mostly it's just an issue of brightness and angle of illumination. If I get normal LEDs, I'll probably have to diffuse the narrow output. It looks like some smd LEDs have a wider dispersion that might not need to be further diffused? Since I'm buying 6 of them, I'd prefer to keep the cost below $5 each or so.

Does anyone have experience on how much LED output is needed to light up the average pumpkin??

Does anyone have experience on how much LED output is needed to light up the average pumpkin??

Well, one year I bought a candle that was rated at about 1000mcd and that seemed to work pretty well. :smiley:

Mikal

Point taken. ;D

The trouble is that a normal LED directs its output mostly in one direction requiring diffusion. Some light is lost, so I'm guessing it might realistically take more than 1cd if using narrow LEDs.

If I remember right, these have a larger light dispersal pattern than a standard LED
http://www.seeedstudio.com/depot/parts-superflux-c-32_18.html
I think they are around 120 or so degrees, so that should work pretty well at the back of the pumpkin.

edit - found this on a different suppliers page "(Flux LED) with view angle from 70degree to 120 degree."

Well, one year I bought a candle that was rated at about 1000mcd and that seemed to work pretty well

LED brightness in mcd is light per unit area so a highly focused 5000 mcd LED actually puts out a lot less light than a candle.

Still, you did make me laugh.

For a diffuse light, I'd recommend a 1 watt like seeedstudio sells.

Well, I have 3 different types of LEDs from Seeedstudio.
Generic bright directional white LEDs
Pirhana diffuse LEDs
1w Luxon LEDs

So far I've tested the first two. I think either would do the job. It will depend on how much the light bounces around inside a pumpkin!

It's done!

1 Like

Pictures!

Here is the protoboard with the bare Atmel168 and Darlington array chips. I probably should have built a shield instead, but this was faster. I socketed the Atmel168 so I could pull it out and drop it in an Arduino for code updates. Power is coming from a 5V switching wall wart not shown.

Here is the small LED board. There is one inside each pumpkin. I took two through hole DIP breakout boards and cut each up into 4 pieces for a total of 8 small boards just big enough to hold a Phiranha LED from Seeedstudio http://www.seeedstudio.com/depot/parts-phiranha-c-32_18.html and a 47Ohm resistor.

For those of you who count things, your are right there are 7 pumpkins and only 6 PWM outputs. I thought we would only carve 6 pumpkins, but ended up with 7, so 2 of them are being driven by the same output.