Build a Complete AVR System and Play Mastermind using Arduino
The game Mastermind has been around a long time, and I remember getting a board version with colored pegs when I was a kid. I love this game, as it is solvable simply by pure logic. One player (or a computer/microcontroller) chooses a sequence of colors from a pool of available colors and then the other player attempts to figure out what that sequence is.
This is the AVR version of that famous logic game Mastermind. In this game, the MCU will choose four colors out of seven and you will try to deduce what colors the MCU picked. The MCU’s color choices may include duplicates. For example, the AVR may pick “blue red white white.” Your task is to figure out the target color sequence by using logic. The available colors are: blue red yellow green white black orange. The standard game continues for eight rounds.
The MCU will give you two pieces of information upon which to base your next color choice: the number of colors in your sequence that are also in the target sequence and the number of colors that are correct that are also in the correct slot. For instance, if the target sequence is “blue red white white” and you picked “blue white green yellow” the MCU would inform you that you have two
colors that are correct, and one of them is in the right slot. If you chose “white white green yellow” the MCU would inform you that you have two colors correct, but none of them are in the right slot.
High score and configuration information is written to EEPROM.
In this instructable I will show you how to build a complete, regulated power, AVR development environment that you can play the Mastermind game on (as well as having a development environment for all of your other projects), including the firmware to run it. Alternatively, you may run this code on an Arduino or clone without having to build anything. I’m also making AVR Terminal available for download.
Step 1: Equipment List
This firmware can be run on any AVR with 10k flash storage. If you cut and prune the source, you can get it to run on an 8k ATtiny AVR. My system was built from “scratch” and includes a Light Determining Resistor (LDR) to provide enhanced randomization when the MCU picks its colors. If you want to build a stand-alone AVR system with serial terminal capabilities, I’ve included direct links to most of the items so you can buy most everything from the same place (cutting shipping costs). Chances are, if you’ve already been playing with AVR’s and stuff, you might have a bunch of this stuff lying around already.
You will need the following parts:
AVR Stand-alone System
- ATmega328p ( $4.30 from Mouser.com) [you can substitute an ATmega168, ATmega32, or any other AVR that has ~ 10k flash memory]
- 2 x 27pF capacitors ( $0.23 each from mouser.com)
- 3 x 0.1uF tantalum capacitors ( $0.40/ea from mouser.com )
- 47uF electrolytic capacitor ( $0.18 from mouser.com )
- 20 MHz crystal 20pF capacitive load ( $0.57 from mouser.com [you can use 16MHz or other AT-cut crystal]
- 2 x 10k ohm resistors ( $0.12/ea from mouser.com)
- 1 x 1k ohm resistor ($0.12 from mouser.com)
- 1N4002 diode ($0.11 from mouser.com
- 10uH inductor ($0.59 from mouser.com or a smaller sized one if handy)
- tactile switch ($0.26 from mouser.com)
- 2 x 3mm LED (a green one and a red one) * note the red one has an integrated resistor. Any LEDs will do. One is for power indicator and one is for reset indicator.
- light dependent resistor (something like $0.45 from Newark or get a 5-pack for $2.99 from Radioshack)
- pin header to make our ISP header (you want a really long tail or mounting pin. i pick mine up from a local shop. this is the closest i could find online ($2.50 from mouser.com)
- breadboard ($10.71 single strip from mouser or a 4-strip one with jumper wires for ~ $16.00 on Ebay.com )
- wire, wire cutters, needle-nose pliars, and lots of Cheez-it’s
I suggest option 1 because it’s more than just serial communication. You can use it to program an AVR if it has a bootloader on it. It’s also cleaner and requires fewer external components *and* connects via USB.
Option 1 – USB
- USB-BUB ($12.00 or $13.50 with mini-USB cable from Moderndevice.com)
- right angle 6-position male pin header (40-pin for $1.81 at Mouser)
Option 2 – RS232
- USBtiny ($22.00 from Adafruit (you have to build it yourself but has 6-pin and 10-pin connectivity)
— or —
- AVRISP for around $27.00 on Ebay.com. This option has better support if you use AVR Studio, but only comes with a 10-pin connector.
You have several options here. If you already have a 5V regulated power supply, then you’ll just run +5v and ground from it. If you have no power setup already then you’ll want to build one. You can put it on a breadboard or solder one together. If you get a big breadboard setup (ie not just one strip, but something like the 4-strip one I linked above) then you can just build it on your breadboard. Either way, if you don’t have any regulated power and need it (if you don’t have it, IMHO, you need it), I’ll show you how to build a variable-voltage regulator, not just a 5v regulated power supply. This way you can change the voltage from, say, 3V all the way up to 9V. This gives you more latitude on the things you can power (I used mine yesterday to recharge an old cordless phone that has a lithium/ion battery pack that I wanted to pull out and use in another project). You’ll need the following items:
- LM317 3-position variable regulator ($0.57 from mouser.com
- 0.1uF tantalum capacitor (see link above for link)
- 1uF polarized capacitor (tantalum or electrolytic)
- 1k Ohm resistor (see above for link)
- 220 Ohm resistor ($0.19 from mouser)
- diode (either like the one I linked for you above, or you can use a smaller one. In my setup, I used a smaller switching diode because I had a strip of them. I’d probably go with a 1N400* one if I built another one, though. You can get a 25-pack of the 1N4* for $2.79 from [Radioshack] or a 50-pack of the smaller, switching ones for $2.59 from Radioshack.)
- 10k Ohm Cermet potentiometer/trimmer. The cermet gives you finer control of the power you output, down to about 0.00 accuracy. I got one for $2.69 from Radioshack but if you’re ordering a lot of things from Mouser.com you can do a search for one there, if you like.
- an LED for “power on” indicator is nice. Pick a color. 3mm is a good size. Hell, while you’re at it pick up a 20-pack of assorted LED’s for $2.99 at Radioshack.
- power jack. This will depend on the type of wall-wart power supply you have access to. I literally have a box full of different sizes and power ranges from various assorted electronic things that have gone the way of the dinosaur but I kept their power supply. I’ve found the most popular is 2.1mm. You can go for an on-board jack if you’re soldering (the pins won’t fit in a breadboard) like here for $0.63 cents. If you plan to put everything on your board, then I’d suggest going with something like this from Radioshack although it’s more expensive: $2.99 since it’s not right angle and you can solder up some wires to it and wrap it in tape. Ghetto, I know.
Development and Programming Software
You’ll need something to write software on, if you’re planning on building Mastermind (or anything else) from source. On Windows there’s AVR Studio from Atmel and WinAVR. I’d say WinAVR is a must, as it already includes AVR GCC, avrdude (the programming software), and several other utilities like make, etc. If you’re on a FreeBSD system (or Linux), then you probably already have avr-gcc, avr-binutils, etc. See one of my other instructables for instructions on downloading and installing it for FreeBSD/Linux if you need help.
Mastermind should compile in either AVR Studio, WinAVR, or pure avr-gcc. I use both on Windows and avr-gcc on FreeBSD, but wrote it in Programmer’s Notepad under WinAVR. You’ll be using avrdude to configure the fuses on your new AVR development and Mastermind system.
That should be everything you need to build a stand-alone, power regulated, external crystal 20MHz AVR system complete with 6-pin ISP programmer and inductive-filtered analog voltage. Ready to get building?
For more detail: Build a Complete AVR System and Play Mastermind using Arduino