Halloween!

Claws-down, Halloween is my favorite holiday. Add some electronics to the mix and it’s an unstoppable force of nature.

In this simple no-soldering-required project we’ll make the Adafruit Circuit Playground board act like a flickering candle. Pop this inside a jack-o’-lantern (or any other item that might normally use flame) to create a festive spooky atmosphere.

Sure, one can just buy a fake battery-operated candle, but they’re usually pretty dim. Also, we can take this as an opportunity to learn a bit about code…and once Halloween’s over (how sad!) the Circuit Playground board can be used again in endless other projects.

Items needed:

If this is your first time using Circuit Playground, start with our “Introducing Circuit Playground” guide. There’s some software to set up on your computer and some procedures to learn. Try out a couple of the examples, make sure you know how to upload “sketches” (code) to the board.

Simulated Candle Sketch

Copy and paste the code below into a new Arduino sketch:

#include "Adafruit_CircuitPlayground.h"

void setup() {
  CircuitPlayground.begin();
  CircuitPlayground.setBrightness(255); // LEDs full blast!
}

uint8_t prev = 128;              // Start brightness in middle

void loop() {
  uint8_t lvl = random(64, 192); // End brightness at 128±64
  split(prev, lvl, 32);          // Start subdividing, ±32 at midpoint
  prev = lvl;                    // Assign end brightness to next start
}

void split(uint8_t y1, uint8_t y2, uint8_t offset) {
  if(offset) { // Split further into sub-segments w/midpoint at ±offset
    uint8_t mid = (y1 + y2 + 1) / 2 + random(-offset, offset);
    split(y1 , mid, offset / 2); // First segment (offset is halved)
    split(mid, y2 , offset / 2); // Second segment (ditto)
  } else { // No further subdivision - y1 determines LED brightness
    uint32_t c = (((int)(pow((float)y1 / 255.0, 2.7) * 255.0 + 0.5) // Gamma
                 * 0x1004004) >> 8) & 0xFF3F03; // Expand to 32-bit RGB color
    for(uint8_t i=0; i<10; i++) CircuitPlayground.strip.setPixelColor(i, c);
    CircuitPlayground.strip.show();
    delay(4);
  }
}

 From the “Tools” menu, select “Board→Adafruit Circuit Playground.” Also make sure the correct serial port is selected, then upload the sketch to the board.

If all goes well, the NeoPixels on the Circuit Playground board should start flickering (mostly shades of yellow). There’s our electronic “candle”!

For stand-alone powering the project, our 3xAAA battery holder has a power switch and a JST connector that fits the Circuit Playground board. A fresh set of batteries should run it for about ten hours, maybe even longer. Alternately, a USB phone charger (and micro-B cable) can be used.

Last updated on 2016-10-19 at 02.04.56 PM Published on 2016-10-14 at 04.50.11 PM