Make: More Electronics (2014)
Chapter 20. Experiment 20: Decoding Rock, Paper, Scissors
I invite you to inspect Figure 20-1. This circuit is the simplest way I could find to create a fully-featured version of the Rock, Paper, Scissors game, using two decoders to simplify the logic while retaining switches to activate the LEDs and a beeper. Because it uses switches as well as logic gates, you can think of it as a hybrid circuit.
I’ll deal with the logic first. You’ll remember that at the end of Experiment 17, I decided that logic gates were inconvenient and complicated for the task of detecting if either player had cheated. Well, this difficulty has now disappeared.
In the schematic, each switch now has a binary-code place value of 1, 2, or 4, and each set of three switches feeds the inputs on a three-bit decoder. All the possible switch combinations can now be expressed with just eight outputs, numbered 0 through 7. I have shuffled the sequence of the numbers to minimize wiring crossovers in the schematic, and output 0 is currently unconnected.
Let’s look at just one of the decoders, since their connections are so similar. Output value 3 from Decoder A can only be created if Player A presses switch 1 in combination with switch 2. Output value 5 can only be created by pressing switch 1 with switch 4—and so on. Consequently, we now know that if we have output 3 OR output 5 OR output 6 OR output 7 from Decoder A, Player A has cheated. We simply combine these outputs through a quad-input OR gate, and send them to a “cheat” indicator, and that job is done.
The remaining logic is similar to the logic I used before. I have used diagonal lines to connect the logic gates, in an effort to make the connections easier to follow. For example, suppose Player A presses switch 4 (meaning “scissors”). If Player B presses switch 2 (meaning “paper”), scissors cut paper, so Player B wins. This is shown in Figure 20-2 where the red lines are carrying positive power.
Figure 20-1. Mixing decoders, logic gates, and multipole switches may be the simplest way to emulate the Rock, Paper, Scissors game.
Figure 20-2. In this example, Player A chooses “rock” and Player B chooses “scissors,” using the array of switches shown in the previous schematic.
You can follow all the possible combinations this way.
But what if both players press the same-numbered switches, creating a tied game? The tie combinations do not activate any of the AND gates, so the “who wins” indicators stay dark. I chose to use the switches themselves to deal with this possibility, because the wiring is so simple. Arbitrarily, in Figure 20-1, I colored the relevant wires green. You’ll see that if any two switches opposite each other are pressed, this activates a beeper, shown at top-right. Now that the wiring diagram has been simplified, one beeper will do the job.
Meanwhile, the LEDs that show which switch has been pressed are also activated by the switches. Each LED is shown as a yellow circle to save space in the schematic, and they are connected by brown wires. The LEDs are wired in series, as before, so that both players must press switches before any LEDs light up. In the event of a tie, the beeper sounds, and the players can see two LEDs illuminated opposite each other.
I think this is the simplest way to represent the game electronically, although I’m always open to other ideas.
If you come up with a different system, using either logic gates or switches, I’d love to see it. Just make sure it satisfies these criteria:
§ A “cheat” indicator must light up if either player presses more than one button.
§ Two other indicators must show which player wins, and they must only work if no one cheats. Note that my schematic satisfies that requirement, because each of pins 1, 2, and 4 on Decoder A and Decoder B will only have a high output if a single switch is pressed. Multiple switches will create a different decoder output.
§ A beeper should sound if there is a tie.
§ Each switch should light an LED showing that it has been pressed, but not until both players have made their choices.
An Unobtainable Or
The version of the game in Figure 20-1 now requires six switches or pushbuttons, two decoders, two quad two-input AND gates, one triple three-input OR gate, and one dual four-input OR gate. Add it all up, and the chip count is significantly lower than if I had tried to accomplish all this just with logic chips. And, the circuit is easier to build and understand.
If you want to wire this circuit, it should be fairly simple.
But wait a minute. Why am I using that “should” word? Because although it should be simple, it isn’t! The problem is a matter of availability:
§ A dual four-input OR gate is not available (in DIP format) in the HC family. It can only be obtained in the old CMOS 4000 series, which is not really powerful enough to drive an LED.
This is annoying, but it’s not an unusual event. Anyone who designs a circuit may discover that the exact part that he wants does not exist, or has reached what the suppliers refer to as “end of life.”
What about the other parts that I want to use in this particular circuit? Are they available?
The 74HC237 is a three-bit decoder in the HC family; no trouble there.
We have used triple three-input OR chips and quad two-input AND chips in the HC family, so we know they exist.
It’s just the dual four-input OR chip that’s a problem.
What to do? Well, you can always emulate a logic gate by using other logic gates, and OR gates make this particularly easy. Figure 20-3 shows how three two-input OR gates can be combined to perform the same function as a single four-input OR gate. So, I can replace each four-input OR gate in this way.
Figure 20-3. Three two-input OR gates have exactly the same function as a single four-input OR gate when they are combined in this configuration.
It’s annoying, though, that this will require two quad two-input OR chips instead of one dual four-input OR chip. I want all the chips to fit on my breadboard. Is there another option?
Saved by NOR
Each of the (unobtainable) four-input OR gates was saying, “If any of the decoder pins 3, 5, 6, or 7 is high, this means two or more buttons have been pressed, which is cheating.” But let’s think what this means. If 3 OR 5 OR 6 OR 7 is high, this must mean that neither 0 NOR 1 NOR 2 NOR 4 is high—because those are the only other pins available. In other words, instead of looking for a high output on one of the output pins that indicates cheating, we can look for an absence of a high output from any of the other pins. It amounts to the same thing.
A four-input NOR gate on output pins with values 0, 1, 2, and 4 will serve exactly the same function as an OR gate on output pins with values 3, 5, 6, and 7. And guess what: a dual four-input NOR chip does exist in the HC family. It’s the 74HC4002.
Why is there a four-input NOR chip, but not a four-input OR chip? I have no idea. And how do I know which types of chips and gates are manufactured in each family? Because Wikipedia has a page listing every logic chip that exists. Just search for “List of 7400 series integrated circuits.” Very convenient! And after verifying that the chip exists in theory, I can go to a supplier such as http://www.mouser.com to make sure that it is still being manufactured and sold in the real world.
Figure 20-4 shows the wiring diagram revised, substituting the NOR gates for the OR gates. The output pins in each decoder that have values 3, 5, 6, and 7 are left unconnected.
Figure 20-4. Revised wiring diagram using two four-input NOR gates instead of two four-input OR gates, which are unavailable in the HC family, in through-hole packaging.
Boarding the Simplified Scheme
Now that the logic is settled, it’s time to put the chips together. You’ll need two 74HC08 quad two-input AND chips, of the same type which you have used before. Their internal workings were shown in Figure 15-7. (Two of the AND gates in these chips will be left unused.) You will also need one 74HC4075 triple three-input OR chip, which you ran across in Experiment 19 (see Chapter 19). Its internal connections were shown in Figure 19-8. (The third OR gate in that chip will be left unused.) And the last of the logic chips will be be the 74HC4002 dual four-input NOR chip. Its pinouts are shown in Figure 20-5.
Figure 20-5. Pinouts of the 74HC4002 dual quad-input NOR chip.
Of course you will also need two 74HC237 decoder chips. Figure 20-6 shows the pin functions of this component. Note that it has two output-enable pins, one of which is active when high, the other being active when low. Both of them must be active to enable the output. Therefore, Pin 5 should be tied to ground, while Pin 6 should be tied to the positive bus of the breadboard.
Figure 20-6. Pin functions of the 74HC237 decoder chip.
Pin 4 will latch the output, causing the chip to ignore changes in any of its inputs. Since Pin 4 is active high, it should be grounded to allow the decoder to respond.
I have used the same pin-naming vocabulary as in Figure 19-2. The outputs are labelled Out 0 through Out 7. The inputs are identified with the binary place values, as In 1, In 2, and In 4.
With this information, it’s now relatively easy to breadboard the hybrid version of Rock, Paper, Scissors. Once again I have placed the switches in a separate schematic. You’ll find it in Figure 20-7. The hookup wiring of the switches is shown in Figure 20-8.
Figure 20-7. Switch outputs to be connected with equivalent labels in the breadboard schematic.
Figure 20-8. Wiring the slider switches shown in the previous schematic. An edge connector, at right, can be used to link this circuit with the main circuit on the breadboard, including positive and negative sides of the power supply.
Actual slider switches mounted on perforated board are shown in Figure 20-9.
Figure 20-9. Slider switches for connection with the hybrid Rock, Paper, Scissors game. An edge connector to link this board with the main breadboard for the game is visible in the pale area of the background color at right.
The switch outputs (labelled A1, A2, and so on) connect with the chip inputs that have the same labels, in Figure 20-10.
Figure 20-10. Hybrid Rock, Paper, Scissors breadboard schematic.
Normally, as I think you know, I draw schematics that show components approximately where they will be placed on a breadboard. In this case, I had to violate that rule, because there wasn’t room on a printed page to stack all the chips in a vertical column. Therefore, I had to rotate the decoder chips.
Everything will fit on a breadboard, though. The circuit is shown in Figure 20-11. For testing purposes, I used six miniature tactile switches, each of which fits in one column on the breadboard. You can see their little yellow and red buttons at the top of the photograph. Because these are SPST switches, they don’t enable wiring to show a tie or display an LED. You need the multipole switches in Figure 20-9 to do that.
Figure 20-11. Breadboarded version of the hybrid Rock, Paper, Scissors breadboard schematic.
Make Even More
Because I can never resist thinking of little extra features to add to a project, it occurred to me that the switches in this circuit could have an additional function. Triple-pole slide switches or pushbuttons are relatively unusual. Almost certainly, you’ll be using four-pole pushbuttons, leaving one pole in each switch unconnected. Is there anything useful and interesting that you could do with those spare poles?
Well, yes. You could use them to switch on the power for the rest of the circuit. In the all-switches version of Rock, Paper, Scissors, I found it pleasing that there was no separate on-off switch. The pushbuttons connected power at the same time they determined the outcome of each game.
You could do the same thing with the hybrid version. Wire the unused contacts of Player A’s switches to the positive side of the power supply in parallel so that if any switch is pressed, it connects power through to a separate power supply bus feeding the decoders and logic chips. Player B’s switches can be wired the same way. As soon as either person presses a switch, the logic chips are powered up and ready to interpret switch inputs. As soon as the switches are released, the system automatically powers down.
Have I built this enhanced version? No, I didn’t get around to it. That’s why I’m just sketching an outline of it, leaving you to add this feature yourself.
If you look up “decoder” in an online parts supplier, you’ll probably run across three other kinds of chips as well: encoders, multiplexers, and demultiplexers. This can be quite confusing, as the same chip can be described as both a decoder and a multiplexer. What, exactly, is a multiplexer, anyway?
My job here is to eliminate confusion (or at least reduce it to a tolerable level), so this is the topic that I’m going to tackle next.