22 Commits

Author SHA1 Message Date
andrea
8906b2eab5 first test with CD
Some checks failed
Arduino Pong CD / release (push) Has been cancelled
2026-03-15 18:04:59 +01:00
andrea
5b94e21f21 minor text
Some checks failed
Arduino Pong CI / build (macos-latest) (push) Has been cancelled
Arduino Pong CI / build (ubuntu-latest) (push) Has been cancelled
Arduino Pong CI / build (windows-latest) (push) Has been cancelled
2026-03-15 17:08:59 +01:00
andrea
8a7bd2b192 define matrix led size 2026-03-15 17:07:03 +01:00
andrea
487756978f fix duplicated frame initialization
Some checks failed
Arduino Pong CI / build (macos-latest) (push) Has been cancelled
Arduino Pong CI / build (ubuntu-latest) (push) Has been cancelled
Arduino Pong CI / build (windows-latest) (push) Has been cancelled
2026-03-15 16:22:28 +01:00
andrea
bf2b794421 Add full arduino-cli installation and preparation on Makefile and redefine the CI istructions
Some checks failed
Arduino Pong CI / build (macos-latest) (push) Has been cancelled
Arduino Pong CI / build (ubuntu-latest) (push) Has been cancelled
Arduino Pong CI / build (windows-latest) (push) Has been cancelled
2026-03-15 13:38:54 +01:00
andrea
f0e8a7f05f CI typo 2026-03-15 13:00:09 +01:00
andrea
2727dfb0ef ci.yml
Some checks failed
Arduino Pong CI / build (macos-latest) (push) Has been cancelled
Arduino Pong CI / build (ubuntu-latest) (push) Has been cancelled
Arduino Pong CI / build (windows-latest) (push) Has been cancelled
2026-03-15 12:28:08 +01:00
andrea
bb79c885a9 update arduino/setup-arduino-cli@v2
Some checks failed
Arduino Pong CI / build (macos-latest) (push) Has been cancelled
Arduino Pong CI / build (ubuntu-latest) (push) Has been cancelled
Arduino Pong CI / build (windows-latest) (push) Has been cancelled
2026-03-15 12:13:50 +01:00
andrea
cd9d4289ef update actions/checkout version to use node24 and fix CI warnings
Some checks failed
Arduino Pong CI / build (macos-latest) (push) Has been cancelled
Arduino Pong CI / build (ubuntu-latest) (push) Has been cancelled
Arduino Pong CI / build (windows-latest) (push) Has been cancelled
2026-03-15 12:09:05 +01:00
andrea
d15ae7ba57 exectue CI only when selected files are modified
Some checks failed
Arduino Pong CI / build (macos-latest) (push) Has been cancelled
Arduino Pong CI / build (ubuntu-latest) (push) Has been cancelled
Arduino Pong CI / build (windows-latest) (push) Has been cancelled
2026-03-15 12:05:30 +01:00
andrea
1337269d32 updated README.md with CI actions badge and license bade
Some checks failed
Arduino Pong CI / build (macos-latest) (push) Has been cancelled
Arduino Pong CI / build (ubuntu-latest) (push) Has been cancelled
Arduino Pong CI / build (windows-latest) (push) Has been cancelled
2026-03-15 12:02:14 +01:00
andrea
69386ba4da added CI build test also for mac and windows
Some checks failed
Arduino Pong CI / build (macos-latest) (push) Has been cancelled
Arduino Pong CI / build (ubuntu-latest) (push) Has been cancelled
Arduino Pong CI / build (windows-latest) (push) Has been cancelled
2026-03-15 11:54:10 +01:00
andrea
455014fdff Improved Makefile 2026-03-15 11:52:26 +01:00
andrea
280e681ba6 ci: add github actions workflow for automatic compilation
Some checks failed
Arduino Pong CI / build (push) Has been cancelled
2026-03-15 11:37:03 +01:00
andrea
f710fb2a0e updated README 2026-03-15 11:19:36 +01:00
andrea
748cf6a678 updated preview image 2026-03-15 11:14:13 +01:00
andrea
b1b421c367 updated preview image 2026-03-15 11:08:52 +01:00
andrea
abc381f397 image preview 2026-03-15 10:44:18 +01:00
andrea
5ebf74e044 more detailed README.md 2026-03-15 10:43:35 +01:00
andrea
486ee4df97 gradually increase ball speed if noone scores
when someone scores a point, speed is resetted to the initial speed
2026-03-15 10:05:03 +01:00
andrea
b9bc138cad small refactoring to keep code more cleaning
and fixed a crash when the ball goes out of matrix
in any case now move_ball() function, now prevents
the ball to goes out, if ball coordinates
are out of the matrix, ball restarts from initial position
without assigning any point
2026-03-15 09:39:12 +01:00
andrea
e7f0c3cc1a optimize rerender matrix, do it only if something is changed from the last render 2026-03-14 23:47:44 +01:00
7 changed files with 269 additions and 64 deletions

35
.github/workflows/cd.yml vendored Normal file
View File

@@ -0,0 +1,35 @@
name: Arduino Pong CD
on:
push:
tags:
- 'v*'
jobs:
release:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Install Arduino CLI
run: |
curl -fsSL https://raw.githubusercontent.com/arduino/arduino-cli/master/install.sh | sh
echo "$HOME/bin" >> $GITHUB_PATH
- name: Install Board Core and Libs
run: |
arduino-cli core update-index
arduino-cli core install arduino:renesas_uno
arduino-cli lib install "Arduino_LED_Matrix"
- name: Build Binary
run: make compile
- name: Create Release
uses: softprops/action-gh-release@v2
with:
files: |
build/arduino.renesas_uno.unor4wifi/arduino_pong.ino.bin
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

37
.github/workflows/ci.yml vendored Normal file
View File

@@ -0,0 +1,37 @@
name: Arduino Pong CI
on:
push:
branches: [ master ]
paths:
- 'arduino_pong.ino'
- 'Makefile'
- '.github/workflows/ci.yml'
jobs:
build:
strategy:
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
runs-on: ${{ matrix.os }}
steps:
- name: Checkout Code
uses: actions/checkout@v6
- name: Setup Arduino CLI
#uses: arduino/setup-arduino-cli@v2
shell: bash
run: |
curl -fsSL https://raw.githubusercontent.com/arduino/arduino-cli/master/install.sh | sh
echo "./bin" >> $GITHUB_PATH
- name: Install UNO R4 Core
run: |
arduino-cli core update-index
arduino-cli core install arduino:renesas_uno
- name: Compile Sketch (via Makefile)
shell: bash
run: make prepare_and_compile

3
.gitignore vendored Normal file
View File

@@ -0,0 +1,3 @@
.secrets
.arduino_cache/
bin/

View File

@@ -1,16 +1,58 @@
PORT ?= /dev/ttyACM0
BOARD = arduino:renesas_uno:unor4wifi
SKETCH = arduino_pong.ino
CACHE_DIR = $(shell pwd)/.arduino_cache
BIN_DIR = $(shell pwd)/bin
CLI = $(BIN_DIR)/arduino-cli --config-file $(CACHE_DIR)/arduino-cli.yaml
YELLOW := \033[0;33m
GREEN := \033[0;32m
CLEAR := \033[0m
prepare:
@mkdir -p $(BIN_DIR)
@mkdir -p $(CACHE_DIR)
@if [ ! -f $(BIN_DIR)/arduino-cli ]; then \
echo -e "📥 $(YELLOW)Downloading arduino-cli...$(CLEAR)"; \
curl -fsSL https://raw.githubusercontent.com/arduino/arduino-cli/master/install.sh | BINDIR=$(BIN_DIR) sh; \
fi
@if [ ! -f $(CACHE_DIR)/arduino-cli.yaml ]; then \
echo -e "⚙️ $(YELLOW)Initializing local config...$(CLEAR)"; \
$(CLI) config init --dest-dir $(CACHE_DIR); \
$(CLI) config set directories.data $(CACHE_DIR)/data; \
$(CLI) config set directories.downloads $(CACHE_DIR)/downloads; \
$(CLI) config set directories.user $(CACHE_DIR)/user; \
fi
@if ! $(CLI) core list | grep -q "arduino:renesas_uno"; then \
echo -e "$(YELLOW)📥 Installing board core...$(CLEAR)"; \
$(CLI) core update-index; \
$(CLI) core install arduino:renesas_uno; \
fi
compile:
arduino-cli compile -b arduino:renesas_uno:unor4wifi ./arduino_pong.ino
@echo -e "$(GREEN)🛠️ Compiling $(SKETCH)...$(CLEAR)"
@$(CLI) compile -b $(BOARD) $(SKETCH)
prepare_and_compile:
make prepare --no-print-directory
make compile --no-print-directory
upload:
arduino-cli upload -b arduino:renesas_uno:unor4wifi -p /dev/ttyACM0
@echo -e "$(GREEN)🚀 Uploading $(SKETCH)...$(CLEAR)"
@$(CLI) upload -b $(BOARD) -p $(PORT)
upload_verbose:
arduino-cli upload -b arduino:renesas_uno:unor4wifi -p /dev/ttyACM0 -v
@echo -e "$(GREEN)🚀 Uploading (Verbose) $(SKETCH)...$(CLEAR)"
@$(CLI) upload -b $(BOARD) -p $(PORT) -v
monitor:
arduino-cli monitor -p /dev/ttyACM0
@$(CLI) monitor -p $(PORT)
run:
make compile
make upload
make monitor
make compile --no-print-directory
make upload --no-print-directory
make monitor --no-print-directory
run_init:
make prepare --no-print-directory
make run --no-print-directory

View File

@@ -1,3 +1,67 @@
# arduino_pong
# 🏓 Arduino UNO R4 WiFi Pong
Play Pong on Arduino UNO R4 WiFi LED Matrix
A classic implementation of the Pong game developed specifically for the Arduino UNO R4 WiFi, utilizing its built-in 12×8 LED matrix as the game screen.
[![Arduino Pong CI](https://github.com/Dea1993/arduino_pong/actions/workflows/ci.yml/badge.svg?branch=master)](https://github.com/Dea1993/arduino_pong/actions/workflows/ci.yml)
[![License: GPL v3](https://img.shields.io/badge/License-GPLv3-blue.svg)](LICENSE)
# 📹 Preview
![Pong](preview.png)
Youtube: https://youtu.be/ouLBTDjpKqc
# 📝 Description
This project transforms your Arduino board into a minimalist handheld console. By leveraging the Arduino_LED_Matrix library, the game manages ball physics, collision detection with walls and paddles, and a scoring system displayed via the Serial Monitor.
Key Features:
- Integrated Display: No external modules required; it uses the native 12×8 LED matrix.
- Local Multiplayer: Two-player support using 4 external pushbuttons.
- Dynamic Difficulty: The ball speed periodically increases to keep the gameplay challenging.
- Real-time Scoring: Points are tracked and sent via Serial (9600 baud).
# 🛠️ Hardware Requirements
- Board: Arduino UNO R4 WiFi.
- Buttons: 4x Momentary Pushbuttons.
- Resistors: Not required (uses internal INPUT_PULLUP).
- Breadboard & Jumper wires.
# 🖮 Pinout Configuration
Component Arduino Pin Function
- P1 Button Up D13 Moves Left Paddle Up
- P1 Button Down D12 Moves Left Paddle Down
- P2 Button Up D11 Moves Right Paddle Up
- P2 Button Down D10 Moves Right Paddle Down
- Common GND GND Ground for all buttons
# 🕹️ Game Logic
- Bouncing: When the ball hits a paddle, the X direction is reversed and the hits counter increases.
- Speed Scaling: Every 6 successful hits (hits >= 6), the loop_delay decreases by 20ms, speeding up the action until a minimum limit of 80ms is reached.
- Scoring: If a player misses the ball, the opponent scores a point. The ball then resets to the center with a randomized direction.
# 🛠️ Development
This project includes a `Makefile` to automate the workflow using `arduino-cli`.
### Prerequisites
* [Arduino CLI](https://arduino.github.io/arduino-cli/latest/) installed.
* Arduino UNO R4 core installed:
`arduino-cli core install arduino:renesas_uno`
### Commands
| Command | Description |
| :--- | :--- |
| `make prepare` | Downloads arduino-cli locally and installs the UNO R4 core. |
| `make compile` | Compiles the sketch without uploading. |
| `make upload` | Uploads the compiled binary to `/dev/ttyACM0`. |
| `make monitor` | Opens the Serial Monitor. |
| `make run` | Full cycle: Compile + Upload + Monitor. |
| `make run_init` | Full first-time setup: Prepare env + Compile + Upload + Monitor. |
> **Note:** If your board is on a different port, edit the `Makefile` or override it: `make upload PORT=/dev/ttyUSB0`.

View File

@@ -4,12 +4,14 @@
#define P1_BTN_BOTTOM 12
#define P2_BTN_UP 11
#define P2_BTN_BOTTOM 10
#define MATRIX_WIDTH 12
#define MATRIX_HEIGHT 8
// create LED matrix object
ArduinoLEDMatrix matrix;
// initial pong frame
byte frame[8][12] = {
byte frame[MATRIX_HEIGHT][MATRIX_WIDTH] = {
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
@@ -28,8 +30,8 @@ int p2_start= 4;
int p2_end= 6;
// initials balls coordinates
int ball_reset_x=6;
int ball_reset_y=3;
int ball_reset_x= MATRIX_WIDTH / 2;
int ball_reset_y= MATRIX_HEIGHT / 2;
int ball_x= ball_reset_x;
int ball_y= ball_reset_y;
@@ -42,7 +44,13 @@ int bar_length= 3;
int p1_score= 0;
int p2_score= 0;
int loop_delay= 220;
int need_refresh= 1;
int initial_loop_delay= 200;
int loop_delay= initial_loop_delay;
// used to increase speed when game is too easy
int hits= 0;
long exec_t2= millis();
void setup() {
@@ -59,10 +67,11 @@ void setup() {
}
void render_matrix() {
if (!need_refresh) return;
need_refresh= 0;
// clear
for (int x=0; x < 12; x++) {
for (int y=0; y < 8; y++) {
frame[y][x]= 0;
for (int x=0; x < MATRIX_WIDTH; x++) {
for (int y=0; y < MATRIX_HEIGHT; y++) {
frame[y][x]= 0;
}
}
@@ -72,42 +81,71 @@ void render_matrix() {
frame[i][0]= 1;
}
for (int i= p2_start; i < p2_start+bar_length; i++) {
frame[i][11]= 1;
frame[i][MATRIX_WIDTH-1]= 1;
}
// ball coords
frame[ball_y][ball_x]= 1;
matrix.renderBitmap(frame, 8, 12);
matrix.renderBitmap(frame, MATRIX_HEIGHT, MATRIX_WIDTH);
}
void pong_move_p1() {
if (digitalRead(P1_BTN_UP) == LOW && p1_start > 0) {
p1_start -= 1;
Serial.print("P1 up: ");
Serial.println(p1_start);
}
need_refresh= 1;
}
else if (digitalRead(P1_BTN_BOTTOM) == LOW && p1_start < 5) {
p1_start += 1;
Serial.print("P1 down: ");
Serial.println(p1_start);
need_refresh= 1;
}
}
void pong_move_p2() {
if (digitalRead(P2_BTN_UP) == LOW && p2_start > 0) {
p2_start -= 1;
Serial.print("P2 up: ");
Serial.println(p2_start);
need_refresh= 1;
}
else if (digitalRead(P2_BTN_BOTTOM) == LOW && p2_start < 5) {
p2_start += 1;
Serial.print("P2 down: ");
Serial.println(p2_start);
need_refresh= 1;
}
}
int ball_player_collision(int player) {
for (int p= player; p < player + bar_length; p++) {
if (ball_y == p) {
return 1;
}
}
return 0;
}
void point_scored() {
ball_x= ball_reset_x;
ball_y= ball_reset_y;
Serial.print("P1: ");
Serial.print(p1_score);
Serial.print(" - ");
Serial.print("P2: ");
Serial.print(p2_score);
Serial.println();
hits= 0;
loop_delay= initial_loop_delay;
}
void move_ball() {
need_refresh= 1;
if (ball_x < 0 || ball_x > MATRIX_WIDTH-1 || ball_y < 0 || ball_y > MATRIX_HEIGHT-1) {
// ball out of matrix limits
ball_x= ball_reset_x;
ball_y= ball_reset_y;
return;
}
// if ball is not moving, get random direction
// this is the initial position
if (ball_move_x == 0 || ball_move_y == 0) {
// extract random number between 0 or 1 to select the directions
if (random(2) == 0) ball_move_x= 1;
@@ -118,55 +156,41 @@ void move_ball() {
else if (ball_x == 0) {
// if p1 collision: reverse x, go left
int hit= 0;
for (int p1= p1_start; p1 < p1_start + bar_length; p1++) {
if (ball_y == p1) {
ball_move_x= ball_move_x * -1;
hit= 1;
break;
}
}
if (!hit) {
if (!ball_player_collision(p1_start)) {
// else p2 score, reset board
ball_x= ball_reset_x;
ball_y= ball_reset_y;
p2_score += 1;
Serial.print("Player 2 score: ");
Serial.println(p2_score);
Serial.print("Player 1 score: ");
Serial.println(p1_score);
Serial.println("Player 2 Scores");
point_scored();
}
else {
hits += 1;
ball_move_x= ball_move_x * -1;
}
}
else if (ball_x == 11) {
int hit= 0;
for (int p2= p2_start; p2 < p2_start + bar_length; p2++) {
// if p2 collision: reverse x, go left
if (ball_y == p2) {
ball_move_x= ball_move_x * -1;
hit= 1;
break;
}
}
if (!hit) {
else if (ball_x == MATRIX_WIDTH-1) {
if (!ball_player_collision(p2_start)) {
// else p1 score, reset board
ball_x= ball_reset_x;
ball_y= ball_reset_y;
p1_score += 1;
Serial.print("Player 2: ");
Serial.println(p2_score);
Serial.print("Player 1: ");
Serial.println(p1_score);
Serial.println("Player 1 Scores");
point_scored();
}
else {
hits += 1;
ball_move_x= ball_move_x * -1;
}
}
else if (ball_y == 0 || ball_y == 7) {
if (ball_y == 0 || ball_y == MATRIX_HEIGHT-1) {
// reverse y, go down
ball_move_y= ball_move_y * -1;
}
if (hits >= 6 && loop_delay >= 80) {
// increase ball speed
hits = 0;
loop_delay -= 20;
}
ball_x+= ball_move_x;
ball_y+= ball_move_y;
}
@@ -180,5 +204,5 @@ void loop() {
move_ball();
exec_t2= exec_t1;
}
delay(10);
delay(50);
}

BIN
preview.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 847 KiB