Thursday, September 01, 2011

Minecraft - door with switches.

Although fun to lay redstone wire down and build your own circuits, sometimes it's a case of why re-invent the wheel. The Minecraft Wiki provides set templates for various logic circuits and sometimes it's just a case of connecting them up the right way.

To provide an example a forum member posed a challenge. He had a piston-activated door. To enter he wanted to press a button, have the door open and stay open until he entered then have pressure plates in the floor close the door behind him; so far so easy. However he always wanted it so that when leaving the internal pressure plates that previously closed the door would now open it and external plates would now close it behind him. That's more challenging.

I've provided a solution that I'll detail later, but it's worth working from basic principles with a different model. First of all why buttons and not levers? Attach an ON lever to the door for it to be closed, turn the lever OFF and the door opens. Great for one off actions, but I want to be able to walk in through the door then close it behind me. So I turn the lever outside OFF and the door opens. I then turn the lever inside ON and the door closes. But what happens if I exit via a different door? The lever outside is already OFF; if I turn it on it will make no difference to the door. The simplest way around this is to have a switch that turns itself off by itself and that's either a button or a pressure plate.

As the button by itself provides no power I'll attach it to a NOT gate. With no power input, power is output and the door is closed. When I press the button the power is turned off and the door opens; I step forward and get squashed as the button has now turned off and the power is back on. I could run some delays to keep the door open for longer, but it's inelegant and getting the timing wrong means squishing. I need something that once I send power to it will retain that power until I turn it off and there are two main circuits that will do that.

The basic T Flip-Flop has one input and one output. Provide power to the input and power will be outputted continuously. Provide power again to the input and the output will stop; it's basically a very big lever. The advantage is clear - if I wire both buttons to the same input then pressing one turns the output on which, through my NOT gate, results in the doors opening; and they stay open. Walk through and press the other button and the doors close. In fact I can open and close the doors using the same button. We have a solution to the simplest model - opening and closing a door. However, there's another solution using the other circuit an RS-NOR latch.

The basic RS-NOR latch has two inputs and two outputs. The inputs are marked as S for Supply and R for Reset; the outputs as Q and ~Q. When inactive Q provides power and ~Q doesn't. Feed an input into S and the outputs reverse with Q becoming unpowered and ~Q providing power. This persists until the Reset input is fed power upon which they return to their original state.

So connect the door directly to Q and the doors are closed. Tie the outside button to S and the inside button to R. Press the button outside and the outputs reverse and the door opens. Walk through and press the button inside and the latch resets and the door closes. Excellent except now I can't get out. Pressing the inside button does nothing to change the circuit. What I want is for the button to open the door if closed, but close it if opened and I can detect that using the two different outputs.

When a button is pressed and Q is powered I want the latch to switch over and the door to open; when a button is pressed and ~Q is powered I want the latch to reset and the door to close. The key word there is AND. Connect both buttons to the same wire and feed them into two AND gates; feed Q into the second input of one and ~Q into the second input of the other. Feed the output from our Q AND into S and the output from our ~Q AND into R.

Now with the doors shut I press the button outside. Q is powered and the button provides power to that AND gate that feeds S and the latch reverses the door opens. Step in and press the button inside ~Q is powered and the AND gate provides power to R and the latch resets and the door closes.

Again just like the first design pressing the button will open or close the door.

Now the RS-NOR and two AND gates seems much more complicated than just one T Flip-Flop, but in terms of size and materials it's much smaller so it's the best for this simple model. As per usual it's not quite that simple. If this circuit is used with pressure plates a problem arises.

A pressure plate provides power while a player stands on it and only stops once a player leaves; consider how that works with the latch design. Step on the plate and the Q AND is activated opening the door; however if the player continues to stand on the plate the ~Q AND then activates and the door closes; except then the Q AND is activated and the door opens again. Open, close, open, close while standing on the plate.

So what does this mean for the original problem asked. Well break down the sequence of what is required. The button opens the door (and possibly closes it too as that's not specified); the internal plate closes the door if it's open; but opens it if closed; the external plate closes the door, but only if the internal plate opened it.

So from my examples the fact we're using plates means a T Flip flop is the better option to control the actual opening and closing of the door; in fact we can just wire the button and internal plates directly to the input. However the external plate causes a problem. We don't want it to close the doors just because they're open, but only if they've been opened by the internal plate.

Somehow it is necessary to record whether the internal plate has been stepped on or not and for that we have an ideal candidate in the form of the RS-NOR latch. So take a second feed from the internal plate into a latch and take the unpowered ~Q output and feed it along with the input from the external plate into an AND gate; then finally feed that into the T flip-flop input.

Press the button and the flip-flop NOT turns the power off and the door opens. Step on the external plate and as the latch isn't active it does nothing. Step on the internal plate and the flip-flop powers up closing the door and the latch turns on. Step on the internal plate and the flip-flop powers down and the door opens; the latch is already on. Step on the external plate and the door closes as the flip-flop is fed from the output of the AND gate.

Two small problems now arise. The latch is still on so stepping on the external plate will active the flip-flop and open the door. That can be remedied by taking another feed from the external plate and attaching it to the latch Reset. Use a repeater to add a small delay to ensure that the AND gate is triggered first. The second problem is akin to the first in that stepping on the internal plate to close the door also activates the latch and thus the external plate will, again, open the door. So the latch should only activate if the door is closed that is if power is flowing to the door. Take a feed from there and plug it into another AND gate along with the internal plate and feed that into the Supply of the latch add a delay from the door power to allow time to step off the plate.

So one T flip-flop; one RS NOR latch and two AND gates provides a solution; most importantly having started from first principles it should be clear exactly what each component is doing and provide the means to build different more complicated circuits rather than me just providing the solution and saying "There that works build that!"

[Update - I posted this solution and uploaded a world showing it. I separated out all the components and kept it all one level (as much as possible) to show how each worked with the other. I got a thank-you from the poster and complaints from others about how large it was and how they could reproduce it much more compactly. Now you know me if someone makes my design better or just comes up with a better design from scratch I'm happy; so I  looked at their solutions - sadly none of them followed the brief or they acted in an inconsistent manner.]