Table of Contents

Dual-Controller OLED Tic-Tac-Toe

Introduction

Dual-Controller OLED Tic-Tac-Toe is a portable handheld gaming device specifically designed for two players or player versus bot. This project brings the classic game of Tic-Tac-Toe to a custom electronic platform built around an Arduino Uno board.

Project Goals:

General Description

The project consists of several integrated subsystems that handle input processing, game logic execution, and visual feedback:

Hardware Components:

Software Components:

Hardware Design

This section describes the electronic components used to build the gaming console and their interconnections.

Bill of Materials (BOM)

Component Quantity Description / Role
Arduino Uno R3 1 Central Processing Unit (ATmega328P microcontroller).
OLED Display (SSD1306) 1 128×64 pixel monochrome screen, I2C interface, for rendering the game grid.
Joystick KY-023 2 Analog modules for cursor control (X/Y) and selection (integrated button).
Passive Buzzer KY-006 1 Provides audio feedback (PWM tones) for moves, errors, and victory.
Breadboard (400 points) 1 Platform for solderless electrical connections.
Dupont Wires 1 set Interconnecting wires for the components.

Electrical Schematic

Software Design

The firmware is designed using a Finite State Machine (FSM) approach to manage game transitions, coupled with real-time polling of analog sensors.

Development Environment

Arduino IDE 2.3.x: Used for coding, compiling, and flashing the firmware onto the ATmega328P microcontroller. Serial Monitor: Utilized during the debugging phase to calibrate joystick thresholds and I2C address discovery.

3rd-party Libraries

Adafruit_SSD1306: Essential for handling the display buffer and I2C communication protocol for the SSD1306 OLED.

Adafruit_GFX: Provides the graphics core for rendering lines, circles, and text characters.

Wire.h: The standard Arduino I2C library used for data transmission between the MCU and the display.

Algorithms and Data Structures

2D Array Representation: The game board is stored in a int board[3][3] matrix, where $0$ is empty, $1$ is 'X', and $2$ is 'O'.

Threshold Detection Algorithm: Analog joystick values ($0-1023$) are processed using a hysteresis-like logic ($<300$ and $>700$) to prevent “ghost” movements.

Win-Check Algorithm: A deterministic function that scans 8 possible vectors (3 rows, 3 columns, 2 diagonals) for identical non-zero values.

Randomized AI Logic: A pseudo-random selection algorithm that identifies available indices in the board matrix to perform automated moves.

Implemented Functions

setup(): Initializes I2C communication, sets pin modes for joysticks, and seeds the random generator using noise from an open analog pin (A5).

drawMenu(): Handles the UI for mode selection (Single Player vs Multiplayer) and processes Joystick 1 input for navigation.

tttGameLoop(): The core engine that manages turns, calls the input mapping functions, and updates the game state.

botMove(): Implements the automated opponent logic with an artificial delay to simulate human-like interaction.

checkWin() & checkDraw(): Logic functions that return boolean values to trigger the end-of-game state.

drawTTTGrid(): A dedicated rendering function that draws the board lines and iterates through the matrix to display X and O symbols.

playVictorySound() / playDrawSound(): PWM-based functions that use the tone() command to provide audio feedback through the buzzer.

Results

The project resulted in a fully functional handheld console. The system successfully handles real-time input from two controllers without latency. The OLED display provides a clear interface, and the integrated Bot offers a challenging solo experience. The memory optimizations (using the F() macro) ensured the system remains stable during long play sessions.

Conclusions

todo

Source Code

todo

Bibliography/Resources

Software Resources

Adafruit GFX Library Documentation https://cdn-learn.adafruit.com/downloads/pdf/adafruit-gfx-graphics-library.pdf

Adafruit SSD1306 Library https://github.com/adafruit/Adafruit_SSD1306

Arduino Reference - Tone Function https://docs.arduino.cc/language-reference/en/functions/advanced-io/tone/

Arduino Reference - RandomSeed https://docs.arduino.cc/language-reference/en/functions/random-numbers/randomSeed/

Memory Optimization (F macro) https://docs.arduino.cc/language-reference/en/variables/utilities/PROGMEM/

Hardware Resources

Atmel ATmega328P Datasheet https://ww1.microchip.com/downloads/en/DeviceDoc/Atmel-7810-Automotive-Microcontrollers-ATmega328P_Datasheet.pdf

SSD1306 OLED Controller Datasheet https://cdn-shop.adafruit.com/datasheets/SSD1306.pdf

Joystick Module (KY-023) Guide https://arduinomodules.info/ky-023-joystick-dual-axis-module/