Build a Complete AVR System and Play Mastermind!

 by nevdull
AVR Mastermind - intro.jpg
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.
 
Remove these adsRemove these ads by Signing Up

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

Serial Communication

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
  • max232n dual RS232 receiver/driver ($0.81 from mouser)
  • 5 x 1uF capacitors ( $0.16/ea for 5mm spacing here or $0.76/ea for 2.54mm spacing here)
  • DE9 d-sub female connector ($3.48 from Mouser or $2.19 at Radioshack)
  • wire, solder, soldering iron, maybe heat-shrink tubing if you're feeling fancy.

Programmer

  • 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.

Power

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?

GregF says: Jan 7, 2011. 7:31 PM
Thanks for putting this together. This is a great reference! You mention that it's possible to program an AVR using the USB-BUB. Can you describe how one would go about doing that?
shaunak says: Aug 17, 2010. 6:25 PM
Thank you so very much for the tip on the AVR clock select fuses. :)
nevdull (author) in reply to shaunakAug 19, 2010. 7:28 PM
Thanks, Glad I could help! :)
JayEdu says: Dec 15, 2009. 10:46 AM
Hello!

Your project seems very cool... How the user interacts?? How he plays?

I have a KIT Workshop - Base level with Arduino Duemilanove x4, it´s a good ideia to use it tp build the game?

Regards

PenguinsInAZ says: Oct 13, 2009. 4:14 PM
If, say, a noob who jumped in too quickly with VMware and AVRStudio were to, say, set the fuses improperly on one of his 328Ps and that theoretical 328P stopped paying attention to calls from the noob's AVR ISP MkII, how might you recover that chip from its apparent "Shaun of the Dead" state?

On a side note... Thanks to your incredible Instructible, I now have a very slick little 20 MHz ATmega328P running on my breadboard with die BlinkenLEDs! Thanks!
nevdull (author) in reply to PenguinsInAZOct 14, 2009. 3:40 AM
 Hi Penguins,
Thanks for the kind words!  Well, hypothetically, if one were tohave a "Shaun of the Dead" AVR (which of course, I would onlyknow of hypothetically), one could reset it via a process called High Voltage Programming. It will reprogram and reset your AVR to default settings. This circuit comes wired on AVR development boards from Atmel, butI did find a way to do this by building the circuit myself when I, um,hypothetically needed to do this. 

Check out www.societyofrobots.com/member_tutorials/node/239 for a step-by-step instruction on setting it up.  Out ofthree fried AVR's, I was able to recover one.  This is probably dueto the fact that the other two were truly fried and not just fubar'dwith the fuses.  Hypothetically, of course.

There's also a schematic floating around (and I think a complete kit) onmaking a high voltage programmer shield for an Arduino.

Good luck (hypothetically)!  :)
PenguinsInAZ in reply to nevdullOct 14, 2009. 12:27 PM
Thanks, nevdull (hypothetically)!

If this were to have really happened, it would have been the fuses thatwere borked. ;)

nevdull (author) in reply to PenguinsInAZOct 14, 2009. 1:42 PM
 Hehe :)  If you're running with BOD make sure you have yourBOD fuses set how you need/want them.  I'm iffy with Brown-OutDetection, but I bricked one of my AVR's when myBOD threshold was too high (or something like that or maybe ileft them unprogrammed...it was a long time ago).  Check table 28-4in the Holy Book (www.atmel.com/dyn/resources/prod_documents/doc8161.pdf) for BODLEVEL voltages if you've got BOD enabled.
jeff-o says: Aug 24, 2009. 4:02 AM
I wonder if this could be made into a handheld unit using RGB LEDs to display the "pegs?" It would be hard to display all the rows you've already guessed, though. That would be a lot of LEDs. Hmmm...
nevdull (author) in reply to jeff-oAug 24, 2009. 1:06 PM
haha yah i actually mention that on one of the pages in this instructable. I wrote this firmware on the way to building the hardware version. i bought 50 RGB LEDs on ebay for $20 and just waiting for them to get in...come to think of it, I may have to hit you up on how to drive the LED's efficiently...8 rows, 4 per row, a row of 8 with each color (to show the colors to chose from) at the bottom with tactile buttons beneath each color, and then 4 small 3mm LEDs to the far right per row to show the "color correct" and "place correct" numbers. I could only come up with doing it in binary, though, if that makes sense:

(LED) (LED) (LED) (LED) o oo o

Hard to format it in this space, but the four little LED's would be 2x2, with the first column = color correct and 2nd column = place correct. and they would light up in binary.

(off)  (on)  (off)  (on)(off)  (off)  (on)  (on)
to represent 0, 1, 2, 3, and both on blinking = 4. I couldn't think of another way to do this and not take up a lot of space. I'd definitely be open to any suggestions you might have since you're the LED/LASER king :)

jeff-o in reply to nevdullAug 24, 2009. 1:15 PM
The best way is using a serial LED driver. I'll have to dig up some part numbers, but I think that Maxim IC makes one that would be suitable. That would work for the main bank of LEDs. To indicate whether the colour selection is correct, you could make the LEDs blink or otherwise change, instead of using a separate LED. Some drivers are even capable of PWM fading... At the bottom, where the user selects the colours for the next guess, the button for each row would just scroll through the available colours. There's no need to indicate which ones are available (IMO).
nevdull (author) in reply to jeff-oAug 24, 2009. 1:34 PM
I think I see what you mean...have only four colors at the bottom (one for each slot) with buttons underneath them and the button would cycle the color, then there would be a separate button (or something) to indicate that you're done analyzing/picking colors and are ready to submit?

Re: correct color selection, it would have to
a) be able to indicate if the color was correct, but place was not
b) if the place and color was correct
c) keep the original chosen colors in the row because the logic of the game depends on them. That is, the player will have to keep looking up to their previously chosen colors and to the "color/place correct" indicators for their logic to be sound.

I'm not sure if those conditions could be met by indicating them in the row itself (although I've only thought about it since you mentioned it).

Check out here for an online java version. They use black/white pegs to indicate color/place correct, but I didn't like the idea of "off" being a color but I could probably use a 2-color 3mm LED.

I'll check out the max chip...that seems vaguely familiar..i've taken a look through a lot of their catalog of chips and think I may have seen something like a driver (or does it sink?) in there.
jeff-o in reply to nevdullAug 24, 2009. 1:45 PM
Ah, I see. My memory was foggy and I forgot that it doesn't tell you individually which peg is correct. Well, you could still use the LEDs in the row as a display, but I think it would get confusing to look at once you have a few rows filled up! Hmmm, there must be a way to do it without adding another 32 LEDs... And yes, a separate button would act as a submit button when you're done.
nevdull (author) in reply to jeff-oAug 24, 2009. 1:56 PM
Yah, I agree; it might start looking very busy, but I did get the diffused RGB's since I always thought they were easier on the eyes and their colors were more easily discernible. Oh, and look what I *just* got from my mailbox in between our conversations...

RGB LEDs.JPG
jeff-o in reply to nevdullAug 24, 2009. 2:11 PM
Nice! I happen to have exactly the same package myself (from the same seller!), purchased for a project that never came to fruition.
nevdull (author) in reply to jeff-oAug 24, 2009. 2:14 PM
LOL small world! At least you have a bunch of RGB LEDs lying about to use when the whim strikes you tho! It took a looong time getting here from Hong Kong, but well worth the wait for the price!
jeff-o in reply to nevdullAug 24, 2009. 2:26 PM
Yeah, they take quite a while to arrive but the price is right. I'm not sure what I'll make out of them now. I'll wait for inspiration to strike.
Pro

Get More Out of Instructables

Already have an Account?

close

PDF Downloads
As a Pro member, you will gain access to download any Instructable in the PDF format. You also have the ability to customize your PDF download.

Upgrade to Pro today!