# 3D Printed Knife Cover

Eating at a picnic spot or wilderness campsite is great, but prep can be tricky.
You still want decent kitchen utensils, but for knives there’s a downside to having something sharp and pointy rattling around in your belongings!

When you have a 3D printer, problems like this have an easy solution…

I want to build a unique mechanical counter and I’ve come across an interesting design challenge.

# Background

There are marble “clocks” that count up the time – typically in a rack of single-minute balls, a rack of 5-minute balls, and a rack of hour balls.
When the last ball reaches a rack, it dumps out all the balls and sends a single ball to the next rack.

I’m thinking this concept would be great for a mechanical decade counter – just have one rack per digit of what you’re counting.

# Examples

## Fancier:

With chimes, strange lifting mechanisms, or “complex just because”

Most of these clocks are like type A – they use racks with a “deer scarer” tipping mechanism – when enough balls land in the rack, they all tip out.

Some like type B use a better alternative – the last ball bounces out and releases a gate that allows all the stored balls to roll through the rack.

# Why not follow these designs?

All the existing designs have a common flaw; balls flow from top to bottom, the least significant rack is at the top. To read the state of the display you have to unintuitively read the racks from bottom to top.

This seems a bit ‘wrong’ to me. Naturally you read numbers from left to right.
An ideal mechanism would:

• Have racks arranged horizontally with the least significant digit on the right.
• Have each rack store the balls vertically to increase density.

I want to design a 100% modular “digit” of such a mechanism, so I can stack as many of them horizontally as needed.

Horizontal, ball enters from the right, currently showing “1 4 2 3”.

# The Challenge:

Conservation of energy.
If the design is like the above picture, where the rail is flat…

• As the ball rolls it will slow down.
• Each rack the ball rolls past needs to be emptied by some mechanical trigger – which will absorb speed.

When a rack is emptied, surely those 9 balls that fall out could be harnessed to transfer energy back to the rolling ball?

The devil being in the details, what would you suggest for the design of this “digit”?

1. Must hold 0-9 balls in a visible way.
2. Each new ball enters horizontally from the right with some initial velocity.
(eg from the ball lifter mechanism)
3. Balls #1-#9 need to be captured.
4. Ball #10 needs to empty the digit, AND
5. Ball #10 (or another ball if you’re fancy) needs to exit horizontally to the left at the same height it entered and at the same speed.

Watch this space…

# Introduction

This article covers one method of attaching wires to a bare lithium-polymer battery pouch. This could also be done using a battery spot welder, clamping, or screwing.

Warning: Lithium Ion and Lithium Polymer batteries can catch on fire if shorted, punctured or overheated.
Be careful when soldering that the battery does not become hot.
Ensure you have an appropriate fire extinguisher nearby.

# Step 1: Wire Prep

Before you solder the terminals, plan your wiring.
Where should the battery wires exit the battery? In what direction will they go, and how far? (A scale printout of the layout may help)

Strip and tin the wire ends.
Each bare end should be slightly shorter than the battery tab’s width.

# Step 2: Tab Prep

Peel back one of the battery terminals.

With a small file, roughen the outer half of the battery tab.

Fold the end of the battery tab over, to create a small ‘hook’.
It should be just large enough to fit the wire in.

Put a tiny drop of flux in the hook.
You can spread it around with a piece of wire.

# Step 3: Soldering

Place the wire into the hook – making sure it matches the polarity – and press it closed.
It may help to clamp a little bit of the insulation in the hook as well.

Carefully, press the soldering iron against the folded hook.
The aim here is to heat up the tab, the wire and the flux, without heating up the battery.
If the battery near the tab is warm, stop and wait 2-3 minutes before attempting again.

Run the solder into the folded hook until it begins to melt and ‘wet’ to the metal.
As soon as it seems that a good contact has been made, stop. The solder should have bonded well to both sides of the hook, and to the wire as well.

# Step 4: Tidying

Roll the tab up until it is inside the battery sled.
Replace the covering tape.

Repeat for the second battery tab and wire.

# Step 5: Finish Up

Once both tabs are soldered, the wires can be fixed in place with hot glue, and the area covered with a small quantity of masking tape. Complete!

# X-Carve Tutorial

There’s a strange machine in the workshop!
It’s big and interesting, but how do you make it do something useful?

This post will answer that, though it won’t make you a master machinist in one go.
I’ll assumes that you already made a design, and now you want to fabricate it.

Here’s one I prepared earlier…

# Part 0: X-Carve and Easel

The X-Carve comes with some cloud-based software called “Easel”.
There’s been plenty of hate directed at it. Easel is nowhere near perfect, but it can be wrangled into submission, and it can even be useful.

If you have complex requirements you might actually need something more advanced.
It’s possible to side-step Easel entirely, but it is way more complex, and not necessary for 95% of users.
Not typically worth it. So use Easel. Grumble if you want.

Easel works in the browser. Boo. On the plus side;

• You can complete a lot of the work (All of Part A and Part B below) before you even get to the workshop or in front of the X-Carve PC.
• If you login to Easel on the X-Carve PC using an “Incognito” browser tab, then you’ll see only your account, and nobody else will mess with or be confused by your stuff after you close the tab.

“I am Easel!”

# Part A: Import

Easel only supports SVG import, not DXF.
Lots of scaling issues can occur when passing SVG files between programs. Blech.

Here’s a whole section just on getting your design into Easel – I have found a process that works:

1. Get source file.
2. Grumble at Easel for not supporting DXF import yet,
and that SVG is a terrible format for parts with physical dimensions.
3. Start a new Inkscape document.
Import the DXF file. (Do not open the DXF file or the dimensions will be correct in Inkscape but wrong in Easel!)
4. Import any other shapes/files you want to cut, and arrange them how you like.
5. Go to document properties, resize drawing to selection, zero margins.
6. Here’s the magic bit:
This joins only the separated parts of each shape together, and allows Easel to know what “inside” and “outside” means.

1. Select all.
2. Ungroup.
3. Select all.
4. Path > Combine.
5. Choose the ‘Edit Path By Nodes’ tool.
6. Drag a selection rectangle around the whole design.
7. Join nodes.
8. Leave node editing mode.
9. Select the design. (now a single path)
10. Path > Break apart.
7. Save as an Inkscape SVG.
8. Select all, look up in the toolbar where it says height and width, set view to millimetres. Jot down what it ways the actual dimensions should be.
Think about whether those numbers are realistic!
9. Start a new Easel project.
Import SVG, choose file.
Check that the dimensions match what you expected.
If not, swear. Try to figure out where you went wrong.

Part B: CAM
CAM stands for “computer aided manufacturing”; how to get from a model to instructions that make a physical part.
This step isn’t particularly difficult, but it’s important that you pass the correct instructions to the machine!

## Steps (Summary)

1. Import the SVG file or files.
2. Specify tool, material, dimensions.
3. Choose cutting settings for each path.
4. “Carve…”

## General Tips

• The Easel interface lets you tell it what sort of material you have and its dimensions.
(It also may need reminding about the total bed size – about 800×800 mm)
• A 3D view of the stock and design show up in the preview window, which you can resize smaller to have more room in the working window.

## Path Planning

• Easel assumes that EVERY drawn path will have one cutting operation on it. Selecting the path shows you the options.
• You can choose from;
• Outline on the line, inside, or outside.
• Fill. (Which seeing as you’re doing the opposite is an odd naming choice)
• There’s no specific “drill” operation; if you want a small round hole you draw a small round hole and choose “inside” outline.
• You can specify the depth of the cut:
• By default each cut goes to the full depth of the material.
• You can adjust depth with the vertical slider.
• You can have a path that does not cut at all by setting the depth to zero. (It will turn white) This can be useful when your stock is already the same size as the outline and you just want to cut inside it.
• There’s no specific “drill” operation; if you want a small round hole you draw a small round hole and choose “inside” outline.
• The 1/8″ bit is versatile, and a good choice for many projects.
Plus; chances are it’ll already be mounted in the spindle. Easy!

## Machining Limitations

• The milling bit can’t reach into corners smaller than its diameter – you may need to file the corners out later, design your thing to have smooth curves, or be fancy and make your own “dog bones“.
(Easel doesn’t know how to do them)
• When cutting holes, the diameter has to be larger than the bit – or Easel will ignore it. Using the 1/8″ bit, the holes should be at least 3.3mm.
(Pressing the “simulate” button will show whether a hole will end up being cut)
• The bed and the material are never perfectly flat. You should overstate the material’s thickness slightly – to ensure that cuts go all the way through.
An extra 0.2 mm is often plenty.
• If a piece is completely cut away, it could jump up and get in the way of the bit. This can be dangerous, noisy and embarrassing, not to mention ruin your piece!
• Easel supports tabs. Tabs are little bits that don’t get cut out, so that the pieces stay put until you cut the last bits by hand.
• There are some small hand-held saws with a hacksaw blade that are perfect for removing tabs – check in the blue tool chest.
• To edit tabs on a path; Click on the path. In the cut menu enable tabs, and choose how many and how large to make them.

This is what >300 holes looks like in ‘simulate’

# Part C: Machining

In order,

1. Spoil Board – Find some material about the same size to go underneath and catch when the bit goes too deep. It’s best if it’s the same material or softer.
Yes, the MDF bed could cope with some abuse, but better to take care of it!
2. Material Alignment – be sure the cuts go where you want them to!
You’ll usually want to align your material so that it’s perpendicular to the board.
One method I like is;

1. Drive two identical screws into the horizontally separated bed holes, leaving them sticking up.
2. Put the spoil board behind them, then pull a straight edge back onto the screws – perpendicular!
3. Place the material on top, and align the edge of the material with the spoil board – now it’s perpendicular too.
3. Clamping – got to stop the material from moving!
This takes some time, be patient and do it properly.

1. Make sure you have plenty of clamping spots.
Especially if the material was a bit warped – it may shift up/down a bit as parts are cut out and mess up your depths or tabs.
2. Make sure the clamps are tightened down well.
If the material moves, the work will be ruined, you’re going to have a bad day, and the machine could be damaged.
3. Make sure the clamps are away from where the cuts will go!
This is important – running into even the wooden part of a clamp might damage the machine or break a bit.
4. Insert Bitskip if already the right size.
1. There are two spanners in the middle drawer.
Place both on the spindle and turn the lower one left, the upper one right.
Carefully loosen until you can get the old bit out.
2. Place the new bit in the spindle, with plenty of shaft inside.
If the shaft is a different diameter too, then the collet (inside the spindle) will need to be changed as well.
3. Tighten back up again until the bit is firmly seated.
5. Head Alignment – set up the head in the right place.
With the machine off (push the big red button) gently pull/push the gantry and the head until the bit is right on the bottom-left corner of your work.

1. If cutting shapes from inside the material, maybe position the head away from the edge. (to avoid the clamps)
If cutting inside an entire piece of material you’ll use, ensure the head is right on the corner of the material.
2. The gantry should be pulled/pushed from the center, so that one side isn’t stressed.
3. The head can be moved up and down by twisting the pulleys on top.
6. Carve… – noise time!
1. Switch the machine back on. (twist the big red button)
2. Check one more time that you’ve got exactly what you want in Easel, then press “Carve…”.
3. You’ll see a series of final checks – go through them one by one.
“Yes, I have clamped down the material…”
4. When asked to start the spindle motor;
1. Click the button on-screen to raise the head, then
2. Flick the spindle switch next to the read button to ‘On’, then
3. Close the lid, then
4. Click the button on-screen to acknowledge that the spindle is now running.
5. Then it will start moving! Hooray!
The initial speed may be alarming, but as long as you have set up the design and stock correctly, it will stop in just the right place to start the first cut.
6. Once started, you can fold the PC screen down and close the drawer.
7. Watch – closely, like you’re babysitting a dozen toddlers in a knife shop.
1. The X-Carve is not like a 3D printer or a laser cutter!
If something goes wrong, it can go wrong badly, quickly.
Here’s some interesting reading on just one kind of failure: http://blog.cnccookbook.com/2014/11/19/survive-first-cnc-router-fire/
2. Stand right there, don’t go back to your seat, don’t go to the bathroom.
Cross your legs if you have to.
3. Know where the big red button is.
Ensure you have a hand free to hit it, if you need to.
4. If you have not selected to use tabs on your cuts,
Be especially wary when the machine finishes cutting a piece completely away. It can jump out and get trapped between the bit and the material. I won’t tell you to reach in there and grab it… use tabs?
8. Cleanup
1. Move the spindle switch to “Off”.
2. Push the big red button to shut down the controller.
3. Now the machine can be considered ‘safe’.
The bit, the spindle motor, or the steppers could still be quite hot.
2. Loosen the clamps.
3. Dust! Everywhere! Shop-vac time.
4. If you used tabs, take a
5. Marvel at your lovely design…

# Honey Cake

Honey Cake

Serves 12 or so?

This lovely cake is very tasty, quick and simple to make, and light but moist.
It was an accidental invention – the first time I made it, I was trying to make biscuits!

Ingredients:

1. 11/2 cups plain flour
2. 1 cup almond meal or “LSA” or almonds ground up in a food processor
3. 1 cup castor sugar
4. 11/2 tsp baking powder
5. 250g butter, softened
6. 2 eggs
7. 1/2 cup honey
8. 75 mL milk
9. 300ml whipping cream

Method:

1. Mix the dry ingredients (A-D) in a bowl,
2. Add everything else except the cream (E-H) and mix well.
3. Line shallow baking trays with baking paper and spread the mixture out thinly.
The recipe covers 2-3 trays.
4. Bake at 180 degrees (170 degrees for fan-forced) for 10-15 minutes, until the edges brown slightly.
5. Cool the layers completely, and cut into even-sized sheets.
6. Whip the cream (I) to stiff peaks.
7. Build the cake in layers, spreading cream between each layer.
It might be easier to make several smaller cakes.

Serve with a hot or cold refreshing drink.

# Chocolate Mousse Cake – vegan

So good! Don’t tell your guests that it’s vegan, and they’ll never guess…

# Vegan Chocolate Mousse Cake

Makes a 20cm dia cake – Serves 8

## Cake

1. 11/2 cups plain flour
2. 1 cup brown sugar
3. 1/4 cup cocoa
4. 1 teaspoon baking soda
5. 1/2 teaspoon salt
6. 1/3 cup vegetable oil
7. 1 teaspoon vanilla essence
8. 1 teaspoon apple cider vinegar
9. 1 cup water
1. Preheat oven to 180 degrees C, and line with paper or grease a 20cm round cake tin.
2. Sift A-E together into a bowl.
3. Add the remaining ingredients, mix with a blender until smooth.
4. Pour into cake tin, bake for 45 minutes, or until cake skewer comes out clean.
5. Remove from oven, allow to cool, remove carefully from the pan for further cooling.

## Mousse

1. 1 block (200-250g) dairy-free dark chocolate, melted.
(Many dark chocolate brands use milk solids, check the ingredients)
(An easy method to melt chocolate; break it into squares and microwave for 20 seconds at a time, stirring well each time, until all melted)
2. 1 packet (250-350g) of silken tofu
3. 2 tbsp milk (coconut, almond, rice etc)
4. 3 tbsp icing sugar
5. 1 tsp vanilla essence
6. Generous pinch of salt
1. Put all ingredients together in a bowl.
2. Blend at high speed for several minutes. The mousse should be completely smooth.

## Assembly

1. Half a jar of strawberry jam
1. Turn the cake out of the pan.
2. Using a long bread knife, cut the cake in half horizontally. Put the lower half on a plate, and the upper half aside.
(The cake will be fragile, so take care to prevent it from cracking)
3. If your cake tin is the spring-form kind, place the ring over the cake.
4. With a spatula, spread the mousse over the lower half of the cake.
5. Place the upper half of the cake over the top.
6. Melt the jam in the microwave (about a minutes) until molten.
(Warning: Hot jam is hot.)
7. Spread/pour the jam over the top of the cake.
8. Chill in the fridge for 1 hour.

Serve on its own or with ice cream/sorbet.
If vegan: Do your best to keep everyone else away from your precious cake. Precious…

In my previous post, I simulated some mechanical logic gates using a novel 3-bar linkage.
The dimensions of the linkage are critical to achieving correct operation;

Inputs $$\{ false\ false \}$$, $$\{ false\ true \}$$, $$\{ true\ false \}$$ must all produce the same output position.

For the simulations, an empirical approach was used. (fiddle with the values until it looks right)

This time, I’m going to attempt to find the exact values to use for a given stroke, separation distance and output position.

Here, you can see the linkage layout for $$\{ false\ false \}$$ and $$\{ true\ false \}$$

# Mathematics

Ahead lurks tough mathematics (which may be unsuitable for liberal-arts majors)
If it’s hard to read, then sorry! It was hard to write!

## Parameters

• $$2 sep$$ Separation between inputs
• $$2 str$$ Stroke length $$false \to true$$
• $$d_i$$ Input link length
• $$d_o$$ Output link length

## Geometry

• $$A(x_a, y_a) = A(0, sep)$$ is the input position when $$A_{state}=false$$
• $$A'(x_a’, y_a’) = A'(2*str, sep)$$ is the input position when $$A_{state}=true$$
• $$B(x_b, y_b) = B(0,-sep)$$ is the input position (The system is symmetric, $$B’$$ does not need to be tested.)
• $$M(x_m, y_m)$$ is the middle position when $$A_{state}=false$$
• $$M'(x_m’, y_m’)$$ is the middle position when $$A_{state}=true$$
• $$O(x_o, y_o)$$ is the output position. ($$O \equiv O’$$ for the gate to function correctly.

## Solving

### Finding $$M$$:

$$M(x_m, y_m) = M( \sqrt{d_i^2 – sep^2}, 0)\\$$

### Finding $$O$$:

\begin{align} O(x_o, y_o) &= O( x_m + d_o, 0 )\\ & = O( \sqrt{d_i^2 – sep^2} + d_o, 0) \end{align}

### Finding $$M’$$:

• $$C'(x_c’, y_c’)$$ is the midpoint of $$BA’$$
• $$b$$ is the length of the line $$BC’$$
• $$h$$ is the length of the line $$C’M’$$
\begin{align} x_c’ &= (x_a’ + x_b)/2 \\ &= str\\ y_c’ &= (y_a’ + y_b)/2 \\ &= 0\\ b &= \sqrt{ sep^2 + str^2 }\\ h &= \sqrt{ d_i^2 – b^2 } && Because BC’ \bot C’M’\\ &= \sqrt{ d_i^2 – sep^2 – str^2 }\\ \end{align}\\

Using this, we can calculate M’:
\begin{align} \frac{h}{b} &= \frac{\sqrt{ d_i^2 – sep^2 – str^2 }}{\sqrt{ sep^2 + str^2 }} \\ &= \sqrt{\frac{ d_i^2 – sep^2 – str^2 }{ sep^2 + str^2 }} \\ &= \sqrt{\frac{d_i^2}{sep^2 + str^2} – 1}\\ \\ x_m’ &= x_c’\ – ( y_c’ – y_b)\frac{h}{b}\\ &= str – \sqrt{\frac{d_i^2}{sep^2 + str^2} – 1} \\ y_m’ &= y_c’ + ( x_c’ – x_b)\frac{h}{b}\\ &= str \sqrt{\frac{d_i^2}{sep^2 + str^2} – 1} \end{align}\\

### Finding $$O’$$:

\begin{align} x_o’ &= x_m’ + \sqrt{ d_o^2 – y_m’^2 }\\ &= ( str – \sqrt{\frac{ d_i^2 }{sep^2 + str^2} – 1}) + \sqrt{ d_o^2 – ( str \sqrt{\frac{d_i^2}{sep^2 + str^2} – 1} )^2 }\\ &= str – \sqrt{\frac{ d_i^2 }{sep^2 + str^2} – 1} + \sqrt{ d_o^2 – str^2 (\frac{d_i^2}{sep^2 + str^2} – 1) }\\ y_o’ &= 0 \end{align} \\

## And then:

Now that $$O$$ and $$O’$$ are found, and we know $$O \equiv O’$$…

### Finding the $$x_o’$$ equation in terms of $$d_o^2$$:

\begin{align} x_o’ &= x_m’ + \sqrt{ d_o^2 – y_m’^2 }\\ d_o^2 – y_m’^2 &= (x_o’ – x_m’)^2 \\ d_o^2 – str^2 (\frac{d_i^2}{sep^2 + str^2} – 1) &= (x_o’ – str + \sqrt{\frac{d_i^2}{sep^2 + str^2} – 1})^2 \\ d_o^2 &= \left( x_o’ – str + \sqrt{\frac{d_i^2}{sep^2 + str^2} – 1} \right)^2 + str^2 (\frac{d_i^2}{sep^2 + str^2} – 1) \\ \end{align}

### Substituting back into the $$x_o$$ equation:

\begin{align} x_o &= \sqrt{d_i^2 – sep^2} + d_o \\ x_o – \sqrt{d_i^2 – sep^2} &= d_o \\ ( x_o – \sqrt{d_i^2 – sep^2} )^2 &= d_o^2 \\ ( x_o – \sqrt{d_i^2 – sep^2} )^2 &= \left( x_o’ – str + \sqrt{\frac{d_i^2}{sep^2 + str^2} – 1} \right)^2 + str^2 (\frac{d_i^2}{sep^2 + str^2} – 1) \end{align}
Ugly, but workable?

# Example:

• $$sep = 50$$ Separation between inputs $$100 mm$$
• $$str = 20$$ Stroke length $$false \to true: 40 mm$$
• $$d_i = ?$$ Input link length
• $$d_o = ?$$ Output link length
• $$x_o = x_o’ = 70$$ Desired output position

\begin{align} ( x_o – \sqrt{d_i^2 – sep^2} )^2 &= \left( x_o’ – str + \sqrt{\frac{d_i^2}{sep^2 + str^2} – 1} \right)^2 + str^2 (\frac{d_i^2}{sep^2 + str^2} – 1) \\ ( 70 – \sqrt{d_i^2 – 50^2} )^2 &= \left( 70 – 20 + \sqrt{\frac{d_i^2}{50^2 + 20^2} – 1} \right)^2 + 20^2 (\frac{d_i^2}{50^2 + 20^2} – 1) \\ \end{align} \\
Pushed through Wolfram Alpha…
\begin{align} \\ d_i &= \sqrt{2900} \\ &= 53.852\\ \text{and}\\ d_o &= x_o – \sqrt{d_i^2 – sep^2} \\ &= 70 – \sqrt{ 2900 – 2500 } \\ &= 50 \end{align}

# Summary:

Phew!

Thusly, one can calculate the input and output linkage lengths for any logic gate.
These dimensions will work for an OR gate too, as the linkages are just reversed.

It’s irksome that I couldn’t work out a symbolic solution for the last equations, especially since $$d_i$$ in the example came out to such a neat value.

I might revisit this further in the future, and it’ll definitely come in handy when building something with these gates.

# Springless Reversible Mechanical Logic

Logic gates! The building blocks of computing as we know it. What if we ditch the electrons?

I want some mechanical logic for an upcoming project, so I did some research…

# Research

There are several examples of mechanical logic out there on the internet:

Keshav Saharia’s Lego Logic Push-pull, needs a spring return.
Randomwraith’s Lego Logic Push-pull with internal rotating elements, reversible.
Mechalogic’s Logic Elements Push-pull, AND & NOT reversible, OR needs a spring return.
Xiaoji Chen’s Linkage Computer Rotating logic, reversible, some mechanical issues with parallelogram weakness.
Spillerrec’s Lego Logic Gates Push-pull, very compact, needs a spring return.
He identifies the problem with needing springs, but doesn’t have a fix. Mentions how AND/OR gates can be reversed to become OR/AND gates.
Zeroumus’s AND Gate Push-pull, needs a spring return.
KNEX XOR Gate Push-pull, needs a spring return.
Carolin Liebl and Lisa Hopf’s NAND Gate Push-pull, reversible, complex.

There are many more ideas out there, even water pipe logic, and of course the early mechanical computers Digi-Comp I and Digi-Comp II.

For my project, I have the following requirements;

• Push-pull logic
• Output movement range must equal input movement range
• No springs/elastic bands, and no forces for inputs to overcome
• No gears!
• Easily chain-able, and simple enough to implement a score of them.

This disqualifies most of the designs I found, particularly the ‘no springs’ requirement.

# AND Gate

Mechalogic’s AND gate design uses an elastic band, but doesn’t appear to need it, so it serves as a good starting point:

It’s time consuming to tweak parameters in the physical world, so I wrote a small simulation: (click to see in action)

Success! Meets all the requirements, and only needs three parts.

The geometry is important – the ratio will determine the output’s position when only one of the inputs is asserted, according to;

• distance between the inputs (red),
• length of the input links (green),
• length of the output link (teal)
• input displacement

Now, can an OR gate be designed with the same success?

# OR Gate

Most OR gates look like this; the output (on the left of this picture) has a flat plate, and either input will push the flat forward. Unfortunately the design needs that elastic band to return the output to zero.

Inspired by some earlier attempts to couple an AND and a backwards OR gate together, I tried flipping the AND gate around; (click to see in action)

Success! Oddly enough, an OR gate is just an AND gate connected up backwards.
Even the linkage lengths are the same!
Best of all, the OR gate is reversible like the AND gate – no springs and no force required.

# AND Then?

As the graphs show, output displacement equals input displacement, so these designs should be chainable. Let’s have a look; (click to see in action)

Success!

Now I just have to build something complex with them…

All the simulations are viewable here.
The code they’re written with is available on GitHub;
https://github.com/MrTrick/logic_gates

# Javascript: Self-replacing Promises

My application has several resources that need to be loaded just once, and used often afterwards.

Here is a pattern that I’m finding quite useful lately:

/**
* Retrieve the resource once only.
* If the resource hasn't been loaded yet,
*   return a deferred that resolves to the resource.
* If the resource has been loaded,
*   return the resource directly.
*/
App.getResource = function() {
//Return the resource or the deferred if it's set already
if (App._resource)
return App._resource;
//Otherwise build, set and return a deferred that
else
return App._resource = $.getJSON("resource/url").then( function(data) { console.debug("Loaded resource"); return App._resource = data; } ); }; • The first time the function is called, it fetches the resource and returns a deferred object. • When the promise resolves, it passes the resource to handlers. • If called again before the promise resolves, it returns the same promise. (won’t fetch twice) • After the promise has resolved, the function instead returns the resource directly. • If the resource can’t be loaded, the promise fails instead, and is not replaced. To use directly; $.when(App.getResource()).done(function(resource) {
//Use the parameter from the promise
resource.doStuff();
});

The $.when call transparently converts a non-promise into a resolved promise with that value. The snippet behaves the same whether App.getResource() has been previously loaded or not. So why replace the promise at all? Sometimes you need to be able to treat the resource like a synchronous value, such as within a library or a function that is called synchronously. //Here's a class with a synchronously-called function var Model = Backbone.Model.extend({ ... validate: function(attrs) { if (App.getResource().exists(attrs[id])) { return "Model with that id already exists"; } else { return null; } } }); var model = new Model(); //Wait for everything necessary to load first.$.when(App.getResource(), model.fetch(), ...).done(
function() {
if (model.isValid()) doFoo();
else doBar();
});

This code waits for App.getResource() to resolve, as well as for any other needed promises. Afterwards, App.getResource() can just be treated like a synchronous function.

This pattern enables flexible and transparent use of remote resources, in both synchronous and asynchronous ways.

The App.getResource() function is also quite similar to a Lazy Promise, as discussed by James Coglan.