## Arduino Flickering Candle

Update December 24, 2013: Mokus refined his code so that the distribution is now well-behaved (nearly normal) and the PSD no longer turns up at high frequencies). The plots and post have been updated to reflect this change. He will push code to the same link as available.

In my previous post on Candle Flame Flicker I describe the statistics of the optical intensity flicker from a candle in terms of the probability density of the brightness samples and in terms of the power spectral density of the brightness. In this article I discuss how to make an Arduino drive an LED to flicker in a way that is statistically similar to the measurements I took on a real candle.

In the measurements I observed the spectral roll-off of the candle to start between about 5 and 8 Hz, and to decline at a nominal rate of around 44 dB for each decade increase in frequency. The 2nd-order infinite impulse response filter is computationally efficient in terms of using a small amount of memory and requiring few calculations. However, the Arduino is not good at floating point arithmetic. On the Arduino, floating point is done in software, has relatively few bits of precision, and is about 4 to 40 times slower than fixed point (integer) math. It is quite difficult to find useful benchmarks. The basic process is to create white noise and then filter it to the correct spectral shape. Afterward, the signal is scaled to have the appropriate variance and offset with a constant to represent the average brightness of the “flame”.

The approach I used was to design the IIR in Python with scipy’s signal module. I specified a 2nd order lowpass Butterworth filter, specifying a cutoff frequency of 8 Hz, and a sample frequency of 60 Hz. I normalized the coefficients to work in a 16 bit integer space, following Randy Yates’ 2010 Practical Considerations in FIR Fixed Filter Implementations, mainly. From a synthesis perspective, there is some prior art. Philip Ching, a student at Cornell synthesized candle noise quite cleverly, though he neither reported nor replicated the correct statistics. A fellow with the handle Mokus did a very, very tiny implementation for a microcontroller with only 64 bytes of RAM. He helped me modify his code so I could compare his statistics, and after adjustment his spectrum and distribution match fairly well. The high-frequency of his PSD looks a little different from the other methods, but these may not be noticeable to the observer. Finally, there was Eric Evenchick’s coincidental post on hackaday. Mokus reported that Evanchick’s implementation has too slow an update rate; I noticed that Evanchick did not report on the statistics he was targeting nor what he achieved. I did not recreate his work to test.

Then, on to the tests. I really was interested in building and comparing statistics from a 16 bit implementation, a 32 bit implementation in both a Direct Form I and a Direct Form II implementation. Indeed, I had great difficulty getting the filters to perform because I kept misdesigning the integer coefficients and overflowing the registers. As I sought a solution to the 2nd-order filter approach, I also created a 4-stage digital equivalent of an RC filter. The 4-stage RC approach was always stable though it needed a higher sample rate and used much more processor power to create statistically consistent results. On the other hand, it has a more accurate spectrum. A comparison of three different 16-bit methods to Mokus’ and to the actual measurements is shown in the figure below. The legend shows the mean, standard deviation, and their ratio to the right of the label. The All my filters did a credible job of reconstructing the histogram.

The power spectral density (PSD) of the different methods tells a different story. The Direct Form II 16 bit filter is the most visually appealing of the methods I tried. It rolls off more like the physical data than the other methods, except compared to the 4-stage RC filter. The Direct Form II filter is more computationally efficient.

The results for the 32-bit versions show greater variance than the 16-bit versions, but the quality is not markedly better.

I wrote a proof code for the Arduino UNO both to see it flicker and to test the processor speed—separate parts of the code. The results are that compiling with 1.0.3 resulted in a 4,722 byte program that calculated 10,000 new values in 6,292 ms, or 629 microseconds per value. In theory this could produce a sample rate of nearly 1.6 KHz. Or another way of thinking about this is that the final code uses about 629 us/17 ms or about 4% of the processor capability of the Arduino UNO. That leaves a lot of resources available to do other Arduino work or maybe means it can fit in a cheaper processor.

I have released two pieces of code under the GNU Public License 3, you can get the Python I used for filter design and the Arduino test code at the links. If you want the data, please contact me through the comments and I am willing to provide it.

## Bespoke Monitor Stand

I’m using two reams of paper to hold my monitor at the right height. These reams are totally functional. However, I’m trying to learn to make passable hand-cut dovetail joints and I had material from an old keyboard tray that does not fit with my office’s new furniture.

As a tangent, before starting this project I rebuilt the woodworking bench my grandfather gave me before he died. I think he would agree it was an expedient bench, and not an excellent bench. I’m glad to improve it. He made the bench top from unsanded 2×12 inch pine planks, with only moderate knots but with pretty awful warping.

I made my new bench top from his old one. I reground, honed, and lapped the blades, and squared the soles of the two jack planes I inherited. Then I planed out the cup, twist, and bow from the top surface. I planed the bottom surface to a lesser degree, but enough for the bench top to sit true.

I epoxied the handle back together on my inherited Bailey No. 7 jointer plane, reground the blade, honed the blade, and reground the chip breaker. Then I clamped the boards face-to-face, and squared the edges. I glued and clamped the top together, making it effectively a single solid piece of wood that was flatter and stiffer than it even had been.

It was connecting to use the hand planes I inherited, sharpened, lapped, and repaired the handles. More connection to square a benchtop I also inherited. I feel good that somewhere in the roughly 40 gallons of wood shavings (no exaggeration), are dents and oil stains my father made as a boy. And now my daughter and son are leaving dings in the new surface, and I feel good about that too.

I made bench dogs using oak dowel and springy stainless I repurposed from an old windshield wiper blade. The work great and cost about 25 cents a piece. Funny that I seriously considered buying brass ones at over \$10 each until I learned how easy these are to make.

This post, though, is not really about the bench. The working bench was a nice foundation on which to build…a bespoke dovetail monitor stand.

Hand cut dovetails are not intellectually challenging. You can learn the concepts of how to do it with a few hours browsing tutorials. You need a good saw, but I made do with a mediocre one. You need to have a set of chisels and they need to be sharp. So, in a few hours you know how to make handcut dovetails. Trouble is, you can lean how to play piano the same way.

The guys who cut these in four seconds flat while whistling are like Rachmaninoff, only they’re dustier than he was when he did his work. I’m working up to Peanuts’ Schroeder.

It is made from an oak veneer birch plywood, but not multiply. The dovetails are cut at 14 degrees, as clearly indicated by Veritas’s sales literature for dovetail marking guides. The effect of the dovetails with sheet goods is rather cool. It makes the wood look hinged on the ends. It is pretty strong too, though I wouldn’t want a child to stand on the top.

The better fitting parts, like the example above, are really pretty good. Over the length of the joint there are places that gap a little. The thin oak veneer flaked off at the joints in some cases, and so the structural gaps are actually smaller than the surface gaps.

## Sushi Dinner

Tonight we made a sushi dinner, and for the first time included kid-specific items. I used sweetened egg, tamago, sheet cut and pressed with rice in cookie cutters. We made stars and sushi boys. They love the California rolls, shrimp pieces, and vegetable-tamago rolls as much, but it was still fun to make.

My rice recipe and the idea for the shaped pieces came to me from Barber and Takemura’s Sushi, Taste and Technique. It turned out a double recipe of rice was not enough, so you can see at the back pinwheels. Probably we should call them tortilla maki.

## Food Storage and Refried Beans

A solid storage plan includes food, and also anything else you need to make the stored food into something edible. The difficulty is that none of the food storage guides or recipe books I have list how much fuel to store in order to make their recipes.

To rectify this, I have begun measuring fuel consumption when preparing recipes roughly as I would in no-gas and no-power emergency. Perhaps others will find this useful, and quantify their recipes.

The first step is to reduce the total fuel as much as possible, and the first target food is beans. Beans are a nice target because I have a lot of them—something like 15 lbs of kidney beans that are probably older than me. Furthermore, they offer excellent nutrition, and finally they cook for ages. The two techniques I know to reduce fuel use are pressure cooking and pre-soaking.

The test recipe today was for refried beans. The result was more delicious than any canned refries I have ever eaten, though the texture was much lumpier. The recipe is below. I cooked on my Coleman dual-fuel stove, using white gas. Cooking is performed in two stages, the first cooks the beans under pressure, and the second cooks the onions and “fries” the beans.

Total Fuel Use: 122 g (about 6 fl oz) of white gas

Pressure Cooking the Beans: 70 g (about 3.4 fl oz) of white gas

Refrying: 52 g (about 2.5 fl oz) of white gas

Pressure cooker seated on the stove in the back yard.

Pressure cooker loaded with the beans and other ingredients, prior to pressure cooking.

Pressure cooker at pressure; the heat is too high as shown by the copious steam jet.

## Recipe

Adapted from Vickie Smith’s recipe for Refried Beans

### Step 1

1 lb dried pinto beans, soaked at least 4 hours

4 cups pork or beef broth, stock, or water

Add beans and broth to pressure cooker, plus enough water to cover beans by about 2 inches. Stir to mix, lock the lid, and bring to pressure (15 psi) on high heat. Reduce heat to lowest setting that will maintain pressure, and cook for 12 minutes—I cook for 13 at 1 mile altitude. Remove from heat and let pressure drop naturally. Drain beans and mash them with a potato masher until they are to your taste in lumpiness.

### Step 2

1/4 cup bacon fat

2 onions, finely chopped

1 mild poblano, pasilla, or Anaheim chile, seeded and chopped

2 cloves garlic, crushed

1 1/2 teaspoon ground cumin

Heat the fat in the pressure cook, add the onions, chiles, and garlic, cumin and cook, stirring, until they are very soft. Add the mashed beans in two or three batches, stirring to mix.

## Broken Clamp

Sometimes things happen. Fortunately, there were no injuries. I was using this lump of crud to clamp some wood pieces while the glue cured. Or rather, I was trying to do so. The tension I had on the clamp was modest; certainly didn’t have bear down on the screw. Oh well, cheap tools. This is a 4 inch C-clamp. Note that the pivot separated too.

There isn’t any sign of currosion or other pre-existing fracture. The crystaline structure in the metal is quite variable, which implies the casting temperature wa poorly managed.