Thilan Dissanayaka Pet Projects May 06

Tic-Tac-Toe Game with Atmega 256 MicroController

In this blog, I’ll walk you through how I built a Tic-Tac-Toe game using an AVR microcontroller, a 4x3 keypad, and a 3x3 grid of LEDs.

This project is a fun way to combine embedded programming, game logic, and hardware interaction into a complete system that brings a classic game to life using simple electronics.

Project Overview

The objective of this project was to create an interactive Tic-Tac-Toe game where:

Players enter their moves using a 4x3 keypad.

The current game state is visualized using a 3x3 LED grid, with each cell lighting up red or green depending on the player's turn.

The game identifies the winner and plays a simple LED animation to celebrate the victory.

Hardware Components

Here’s what I used for the setup:

  • ATmega microcontroller (running at 8 MHz)

  • 4x3 Keypad for user input

  • 3x3 LED grid for game display

  • Resistors, wires, and breadboard for wiring

  • Power supply (USB/5V battery or programmer)

Software Workflow

Let’s break down the development process step-by-step:

Step 1: Setting Up the Keypad

The keypad serves as the input device where each button represents a cell (1–9) on the Tic-Tac-Toe board. I used PORTK for rows and PINK for columns on the ATmega.

void initKeypad() {
    DDRK = 0x0F;    // Lower nibble (rows) as output, upper nibble (columns) as input
    PORTK |= 0xF0;  // Enable internal pull-ups for the inputs
}

The keypad() function handles:

Polling each row/column to detect key presses

Debouncing to eliminate accidental multiple presses

Mapping key values to grid positions (1–9)

Step 2: Initializing the LED Grid The LED grid is controlled through multiple ports. Each cell corresponds to one LED, and different pins on PORTA, PORTC, PORTD, and PORTG are used.

void initGrid() {
    DDRA = 0xFF;
    DDRC = 0xFF;
    DDRD = 0xFF;
    DDRG = 0xFF;
    // Set all grid control ports as output
}

Each LED is controlled individually based on the game state using a helper function like controlLed(row, col, color, state).

♻️ Step 3: Implementing Game Logic The game logic is responsible for:

Keeping track of the player turn (RED or GREEN)

Preventing overwriting already marked cells

Updating the LED grid

void play(int index) {
    int row = (index - 1) / 3;
    int col = (index - 1) % 3;

    if (grid[row][col] == 0) {          // Check if the cell is empty
        grid[row][col] = turn;          // Mark the cell
        controlLed(row, col, turn, ON); // Light up the LED
        turn = (turn == RED) ? GREEN : RED; // Switch turns
    }
}

🏁 Step 4: Checking for a Winner After every move, we check rows, columns, and diagonals to see if a player has won.

int checkWinner() {
    for (int i = 0; i < 3; i++) {
        // Check rows
        if (grid[i][0] != 0 &&
            grid[i][0] == grid[i][1] &&
            grid[i][1] == grid[i][2])
            return grid[i][0];

        // Check columns
        if (grid[0][i] != 0 &&
            grid[0][i] == grid[1][i] &&
            grid[1][i] == grid[2][i])
            return grid[0][i];
    }

    // Check diagonals
    if (grid[0][0] != 0 &&
        grid[0][0] == grid[1][1] &&
        grid[1][1] == grid[2][2])
        return grid[0][0];

    if (grid[0][2] != 0 &&
        grid[0][2] == grid[1][1] &&
        grid[1][1] == grid[2][0])
        return grid[0][2];

    return 0; // No winner
}

🎉 Step 5: Winning Animation When a player wins, a simple animation flashes the LEDs in the winning row/column/diagonal.

void animation(int winner) {
    for (int i = 0; i < 3; i++) {
        controlLed(0, i, winner, ON);
        _delay_ms(300);
        controlLed(0, i, winner, OFF);
        _delay_ms(300);
    }
}
ALSO READ
Application Security - Interview preparation guide
May 27 Interview Guides

# 1. What is application security? Application security refers to the measures and practices implemented to protect applications from security threats throughout their development lifecycle and....

SSRF - Server Side Request Forgery
May 27 Application Security

Server-Side Request Forgery (SSRF) is a web security vulnerability that allows an attacker to induce the server-side application to make HTTP requests to an arbitrary domain of the attacker's....

CSRF - Cross Site Request Forgery
May 27 Application Security

Cross-Site Request Forgery (CSRF) is a web security vulnerability that allows an attacker to induce users to perform actions that they do not intend to perform. It occurs when a malicious website,....

REST API - Interview preparation guide
May 08 Interview Guides

## What is a REST API? A REST (Representational State Transfer) API is an architectural style for designing networked applications. It uses standard HTTP methods to interact with resources, making....

Factory Pattern explained simply
Apr 26 Software Architecture

# Factory Pattern Imagine you want to create objects — but you don't want to expose the creation logic to the client and instead ask a factory class to **create objects for you**. That's....

Template Pattern explained simply
Apr 26 Software Architecture

Ever found yourself writing similar logic over and over, only to change a few steps each time? That’s exactly what the **Template Pattern** helps you solve. The **Template Pattern** is a....