Introduction: 4 in a Row / Connect 4

I try to make things which are useful, but, for a change, I decided to make a game!

Its an electronic version of Connect 4 / 4 In A Row.

The component count is minimal (if you don't count each individual led), but the wiring takes a little patience.

Step 1: Components

The project centres around the HT16K33 chip which I purchased from eBay for 0.99p

The 7 x 6 matrix uses 5mm Red/Green LEDs, and the column select/win line comprises of 3mm Red/Green LEDs.

I was going to use a button above each column for selection, but because this made the game awkward to play, I opted for a joystick with push button, again from eBay for 0.99p.

I use 3 x AA 1.5v connected to the VCC pin of the Arduino pro mini for power.

Step 2: The HT16K33 & Matrix

The HT16K33 breakout board pin out is clearly labelled.

As I only want to control a 7 x 6 matrix plus the column select line, I did not use all of the outputs!

The following library's are required to control the HT16K33

#include <wire,h>

#include <Adafruit_GFX.h>

#include "Adafruit_LEDBackpack.h"

There is an example of how to control a Red/Green matrix in the lib.

Basically, the library expects 3 values to control each LED in the matrix - Row, Column & Colour

e.g. 0,0,red will light the bottom right corner red & 0,0,green, will light it green!

To facilitate this, The cathode (K) of the LED (centre leg ) is connected to C0 on the HT16K33 board, the short leg is connected to A0, and the longer leg is connected to A8. Consistency of the orientation of the LEDs is important, ie ALL short legs to A0,A1,A2 etc and the longer leg of each LED to go tto A8,A9,A10 etc.

I connected my Green LEDs to A0, A1 etc so my code is slightly back to front, ie, green lights the red LED & red lights the green!

I wrote a function to make it easy to control the individual LEDs. (///C,R,colour 1= Green 2= red).

Step 3: Wiring

This was the most time consuming part of the project.

Isolating each cathode (K) was a bit fiddly, & I miscounted a pin which meant a bit of re-wiring which is why the soldering is not a neat as I like.

Also I'm not keen on wiring on the copper side of the board, but I had little choice - a PCB would be so much better!

The Arduino uses I2C to communicate with the HT16K33 board.

The joy stick is connected to Arduino Analog pin A0, and the push switch on tn the joystick is connected to Arduino Digital pin 10.

Step 4: The Software

Initially I was going to turn both the red & green LED on together to make a sort of yellow to match the colour of the original games counters - but the yellow was not very yellow and varied slightly from LED to LED. So I stuck with Red & Green!

This would explain why I have two 2 dimensional arrays called red & yel.

These arrays hold the position of each of the colours.

There is a third array which holds the next available position in each column.

My code is not the easiest to read due to me changing my mind about the yellow LEDs and also the way I found better techniques of writing some bits of code as I went along.

On power up, each LED is illuminated, first green & then red to ensure all are working & the top line column selection LED is lit.

The joystick is monitored and the appropriate LED is lit. When the desired column is reached, the joystick button is used to 'drop' the LED into position.

After each turn, the arrays are updated and the 'win' check is initiated

First horizontal 'wins' are evaluated, followed by vertical 'wins' & finally diagonal 'wins'.

If a 'win' is detected, the top row of 3mm LEDs are programmed with a chase pattern in a never ending loop, therefor a reset is required for a new game.

The 'win' check routine could be made faster by remarking out all of the 'Serial.println' lines!

Microcontroller Contest 2017

Participated in the
Microcontroller Contest 2017

Lights Contest 2017

Participated in the
Lights Contest 2017