TicTacToe in Arduino - Help

Hi everyone,

I am creating TicTacToe using an Arduino for a project in my engineering class. I am looking for some guidance solving a few problems.

Everyone knows how to play Tic Tac Toe, but I will explain the logic driving the program, and how the hardware is designed.

I am using 9 dual-color LED's to form a 3x3 matrix. There are 4 buttons on either side of this matrix, used to make moves.

The idea is this: Player 1 is the color red, and the Player 2 is the color green. Player 1 always goes first (the same sort of principle as white starts in chess).

When Player 1 presses a specific button, or button combination, the corresponding LED on the matrix lights up red. If Player 2 then tried to light this same LED up, it would blink red, and Player 2 would have to make a different move. When any three colors form a horizontal, diagonal or vertical line of 3, they win, the three LED's blink a few times, and then the game resets.

The main problem I am having is controlling each color on each LED using these push buttons. I have written my code as though there are 18 LED's 8 buttons, and 26 IO pins being used. I would like to keep the IO pin usage to a minimum, so that I can upload this program onto one of this Trinket boards. This will require using a multiplexer, and this adds another layer of complexity to the program.

So, my problem is this:

How do I control each color of each LED using a push button, wired through a multiplexer. I will include snippets of my code below to help address this problem.

//Momentary Switch Pins
//Will map these through a multiplexer
// -xy- -y- -yb-
// -x- -xyab- -b-
// -ax- -a- -ab-
#define buttonPin1_a = 1;
#define buttonPin1_b = 2;
#define buttonPin1_x = 3;
#define buttonPin1_y = 4;
#define buttonPin2_a = 5;
#define buttonPin2_b = 6;
#define buttonPin2_x = 7;
#define buttonPin2_y = 8;
//Button States 
//Button is off by default
bool buttonPin1_aState = LOW;
bool buttonPin1_bState = LOW;
bool buttonPin1_xState = LOW;
bool buttonPin1_yState = LOW;
bool buttonPin2_aState = LOW;
bool buttonPin2_bState = LOW;
bool buttonPin2_xState = LOW;
bool buttonPin2_yState = LOW;
//If Player 1 presses the button x and button y, LED 1 will light up red if the LED is not already blue.
//But if the first LED is blue, Player 1 must make a different move.
    if (buttonPin1_xState == HIGH && buttonPin1_yState == HIGH && bluePin1 == LOW) {
    digitalWrite (redPin1, HIGH);
    else if (buttonPin1_xState == HIGH && buttonPin1_yState == HIGH && bluePin1 == HIGH) {
  //When any three LED's of the same color form a straight line, all three of those lights will blink rapidly, and then the game resets.
   if (redPin1 == HIGH && redPin2 == HIGH && redPin3 == HIGH) {          
        digitalWrite(redPin1, LOW);
        digitalWrite(bluePin1, LOW);
        digitalWrite(redPin2, LOW);
        digitalWrite(bluePin2, LOW);
        digitalWrite(redPin3, LOW);
        digitalWrite(bluePin3, LOW);
        digitalWrite(redPin4, LOW);
        digitalWrite(bluePin4, LOW);
        digitalWrite(redPin5, LOW);
        digitalWrite(bluePin5, LOW);
        digitalWrite(redPin6, LOW);
        digitalWrite(bluePin6, LOW);
        digitalWrite(redPin7, LOW);
        digitalWrite(bluePin7, LOW);
        digitalWrite(redPin8, LOW);
        digitalWrite(bluePin8, LOW);
        digitalWrite(redPin9, LOW);
        digitalWrite(bluePin9, LOW);
  // -x- -x- -x-
  // --- --- ---
  // --- --- ---

Any help is greatly appreciated.

You could start by wiring your buttons/leds into a matrix - so 9 leds only needs 6 connections for rows and columns . This will reduce the I/O count to 24(?) which will help ( leaves you bit short for direct control)

If you google “ Arduino led matrix “ there are a lot of examples of how to wire up and code using shift registers. You might have to start again with your sketch !

Googling “ Arduino tic tac toe “ Is fruitful too !