Hello Every one !
In this instructable I will be showing you how I made a two players Connect 4 with an arduino nano. RGB Led displays player's pawn and player choose where to place it with buttons.
The trick of this instructable is to control a high amount of inputs and outputs : 49 RGB Leds and 3 buttons. I recommend you to read all the steps before to begin your own Connect4. They are lot of tricky parts and you will be blocked if you don't etablished the right protocol.
I'm sorry I didn't take a lot of photos when I was building, this is my first instructable so I will forbid some crucial steps. Be kind and alert me!
Feel free to comment if I made some mistakes. I will fix it if needed.
Step 1: Planning
The first step is planning.
You want to do an Arduino game, but you have to choose some component before. This instructable isn't expensive, Connect 4 is composed by Leds and an arduino nano. So feel free to choose a consistent box or electronic circuit.
If you're a sore loser gets an iron one, more resistant !
Be aware that you will use 49 RGB Leds, that you will have to manage and wire. So you will need space and flexibility.
In some case I had cardboard box that fit size I wanted for my game. I used it but you can chose a wooden box.
Be creative !
Step 2: What You Will Need
- 49x RGB Leds
- 2x 74HC595
- 7x NPN transistor pn2222
- 3x buttons
- 1x power switch
- 7x 100Ω resistors
- 7x 1kΩ resistors
- 3x 10kΩ resistors
- 2x boards
- a box
- 1x 9V battery
- Soldering iron
- Wire cutter
Be sure to prepare evrything, you will need it!
Step 3: Does Arduino Nano Fit - Shift Register
A classic Connect4's play-board is composed of 7 columns and 6 lines. We have an additional line to pick where we want to play. In fact, we have to construct a 7x7 grid.
Okay, now real things begin. How to control 49 RGB Leds with only an Arduino Nano ? Do we need 49 outputs ? More ?
We have 2 colors, 49 Leds : 49*2 = 98 pins to manage for Leds if all ground are connected together!! A kind reminder : Arduino Nano have 18 outputs !
One way to get around this is to split the board in line. All the LEDs aligned in a vertical column share a common anode of one color (+).All the LEDs on a horizontal layer share a common cathode (-).
Now if i want to light up the LED in the upper left corner (A1), I just supply GND (-) to the A line, and VCC (+) to the color in 1 line.
The way to work around it is to only light up one line at a time, but do it so fast that the eye doesn't recognize that only one line is lit at any time!
The number of outputs needed go down from 49*3 =147 to 7*2 + 7 = 28 outputs. Arduino Nano only has 12 digital outputs and 6 analog outputs (that can be used as digital output). Obviously 28 > 18 and we need to remember we have 3 inputs (validation, choose left, choose right).
We will use a Shift Register to extend port. You can understand how it works here. But mainly it is composed by 3 inputs and 8 outputs. When SH_CP goes from LOW to HIGH, DS is read and transmitted to Q1 to Q8. And the output can be readed when ST_CP goes from LOW to HIGH.
So we can control our 7 columns with 3 inputs. Because we have to color we will need to Shift Register.
Let's see how many pins remain:
- 7 grounds
- 3 for red color
- 3 for green color
- 3 for buttons
We have now 16/18 pins used. In order to optimise the program we will use the same pin for the SH_CP's and the same pin for ST_CP's. So 14 pins used. With this cabling we can be sure that only green leds will be turned on or only red ones.
Step 4: Circuit Diagram
This the Diagram of our Connect 4. I used Fritzing (free) to design it. You have to set 7 lines of led with transistors.
This is the Arduino's pins :
- D0 : unused
- D1 : unused
- D2 : line 1
- D3 : line 2
- D4 : line 3
- D5 : line 4
- D6 : line 5
- D7 : line 6
- D8 : line 7
- D9 : unused
- D10 : right button
- D11 : left button
- D12 : valid button
- D13 : SH_CP
- A0 : ST_CP
- A1 : red DS
- A2 : green DS
- A3 - A7: unused
And the Shift Register's pins :
- 1 : led 2
- 2 : led 3
- 3 : led 4
- 4 : led 5
- 5 : led 6
- 6 : led 7
- 7 : unused
- 8 : ground
- 9 : unused
- 10 : 10K resistor and +5V
- 11 : Arduino D13
- 12 : Arduino A1 or A2
- 13 : ground
- 14 : Arduino A0
- 15 : led 1
- 16 : +5V
Step 5: Mount Leds
My grids of LEDs look terrible, it was my first project be gently!
I think you can find a better solution to mount LEDs on your box. In this step you have to be creative and ingenious. I can't really help you because I didn't find a good solution ...
Keep in mind that you will have to solder all LEDs' pins together and wire lines and columns. It have to be accessible, Arduino and Register will be connected with these.
I recommend you to test every Leds before to solder it, after it will be too late... More over you can use different line of your board: if you shift the ground pin it will be easier to connect them together.
Step 6: Solder Circuit
I use 2 boards : one to connect LEDs together and an other for the circuit.
If you were meticulous and farsighted your lines and columns are easily accessible and can be soldered to your main board.
Take your time ! It's the key to succes !
Step 7: Program
You now have your Connect4. To make use of it, you need to upload some code. Mine is fully functionnal and can be used.
Download it from here and tranfer it to your Arduino Nano.
Be aware of what pins you have used, you will need to change some code if needed.
Some upgrade can be made : AI, time to play, ...