21 Commits

Author SHA1 Message Date
andrea
c1cbc07e9c fix conflicts 2026-03-15 22:48:25 +01:00
andrea
6e5fc8ea10 change digital pins to avoid collisions with pin13 for L Led on the board
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
Arduino Pong CD / release (push) Has been cancelled
2026-03-15 22:43:00 +01:00
andrea
a48db73abb experiment with webserver, the goal is to show the score on a web server 2026-03-15 22:35:59 +01:00
andrea
28f029ce03 update ci.yml with new libs structure
Some checks failed
Arduino Pong CI / build (ubuntu-latest) (push) Has been cancelled
Arduino Pong CI / build (macos-latest) (push) Has been cancelled
Arduino Pong CI / build (windows-latest) (push) Has been cancelled
2026-03-15 21:15:49 +01:00
andrea
cb42a45aa7 move all libs into src
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 21:14:04 +01:00
andrea
c47291e258 minors
Some checks failed
Arduino Pong CI / build (ubuntu-latest) (push) Has been cancelled
Arduino Pong CI / build (windows-latest) (push) Has been cancelled
Arduino Pong CI / build (macos-latest) (push) Has been cancelled
Arduino Pong CD / release (push) Has been cancelled
2026-03-15 20:29:56 +01:00
andrea
fec4db0c5d improve rerender logics
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 20:23:52 +01:00
andrea
7c065d8799 updated 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 20:09:21 +01:00
andrea
31891bbc0e minors
Some checks failed
Arduino Pong CD / release (push) Has been cancelled
2026-03-15 20:02:27 +01:00
andrea
5eeb735364 split main .ino fine into multiple sub files
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 19:56:20 +01:00
andrea
a938adff31 generate build files for cd
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
Arduino Pong CD / release (push) Has been cancelled
2026-03-15 18:19:48 +01:00
andrea
1f143520a8 add cd status on README 2026-03-15 18:13:58 +01:00
andrea
cbdfcce354 cd permissions
Some checks failed
Arduino Pong CD / release (push) Has been cancelled
2026-03-15 18:11:53 +01:00
andrea
66957b5e19 remove unecessary lib from cd
Some checks failed
Arduino Pong CD / release (push) Has been cancelled
2026-03-15 18:09:13 +01:00
andrea
f12af76845 minor
Some checks failed
Arduino Pong CD / release (push) Has been cancelled
2026-03-15 18:07:15 +01:00
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
16 changed files with 464 additions and 176 deletions

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

@@ -0,0 +1,37 @@
name: Arduino Pong CD
on:
push:
tags:
- 'v*'
permissions:
contents: write
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 "./bin" >> $GITHUB_PATH
- name: Install Board Core and Libs
run: |
arduino-cli core update-index
arduino-cli core install arduino:renesas_uno
- name: Build Binary
run: make compile
- name: Create Release
uses: softprops/action-gh-release@v2
with:
files: |
build/*.bin
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

View File

@@ -4,9 +4,11 @@ on:
push: push:
branches: [ master ] branches: [ master ]
paths: paths:
- 'arduino_pong.ini' - 'arduino_pong.ino'
- 'Makefile' - 'Makefile'
- '.github/workflows/ci.yml' - '.github/workflows/ci.yml'
- 'src/*.cpp'
- 'src/*.h'
jobs: jobs:
build: build:
@@ -34,4 +36,4 @@ jobs:
- name: Compile Sketch (via Makefile) - name: Compile Sketch (via Makefile)
shell: bash shell: bash
run: make compile run: make prepare_and_compile

5
.gitignore vendored
View File

@@ -1 +1,4 @@
.secrets .arduino_cache/
bin/
build/
src/secrets.h

View File

@@ -1,20 +1,63 @@
PORT ?= /dev/ttyACM0 PORT ?= /dev/ttyACM0
BOARD = arduino:renesas_uno:unor4wifi BOARD = arduino:renesas_uno:unor4wifi
SKETCH = arduino_pong.ino 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: compile:
arduino-cli compile -b $(BOARD) $(SKETCH) @mkdir -p build
@echo -e "$(GREEN)🛠️ Compiling $(SKETCH)...$(CLEAR)"
@$(CLI) compile -b $(BOARD) --output-dir build $(SKETCH)
clean:
@echo -e "$(GREEN)🛠️ Removing build files...$(CLEAR)"
@rm -rf build/
prepare_and_compile:
make prepare --no-print-directory
make compile --no-print-directory
upload: upload:
arduino-cli upload -b $(BOARD) -p $(PORT) @echo -e "$(GREEN)🚀 Uploading $(SKETCH)...$(CLEAR)"
@$(CLI) upload -b $(BOARD) -p $(PORT)
upload_verbose: upload_verbose:
arduino-cli upload -b $(BOARD) -p $(PORT) -v @echo -e "$(GREEN)🚀 Uploading (Verbose) $(SKETCH)...$(CLEAR)"
@$(CLI) upload -b $(BOARD) -p $(PORT) -v
monitor: monitor:
arduino-cli monitor -p $(PORT) @$(CLI) monitor -p $(PORT)
run: run:
make compile make compile --no-print-directory
make upload make upload --no-print-directory
make monitor make monitor --no-print-directory
run_init:
make prepare --no-print-directory
make run --no-print-directory

View File

@@ -3,11 +3,12 @@
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. 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) [![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)
[![Arduino Pong CD](https://github.com/Dea1993/arduino_pong/actions/workflows/cd.yml/badge.svg)](https://github.com/Dea1993/arduino_pong/actions/workflows/cd.yml)
[![License: GPL v3](https://img.shields.io/badge/License-GPLv3-blue.svg)](LICENSE) [![License: GPL v3](https://img.shields.io/badge/License-GPLv3-blue.svg)](LICENSE)
# 📹 Preview # 📹 Preview
![Pong](preview.png) ![Pong](assets/preview.png)
Youtube: https://youtu.be/ouLBTDjpKqc Youtube: https://youtu.be/ouLBTDjpKqc
@@ -57,9 +58,11 @@ This project includes a `Makefile` to automate the workflow using `arduino-cli`.
### Commands ### Commands
| Command | Description | | Command | Description |
| :--- | :--- | | :--- | :--- |
| `make prepare` | Downloads arduino-cli locally and installs the UNO R4 core. |
| `make compile` | Compiles the sketch without uploading. | | `make compile` | Compiles the sketch without uploading. |
| `make upload` | Uploads the compiled binary to `/dev/ttyACM0`. | | `make upload` | Uploads the compiled binary to `/dev/ttyACM0`. |
| `make monitor` | Opens the Serial Monitor. | | `make monitor` | Opens the Serial Monitor. |
| `make run` | Full cycle: Compile + Upload + 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`. > **Note:** If your board is on a different port, edit the `Makefile` or override it: `make upload PORT=/dev/ttyUSB0`.

View File

@@ -1,20 +1,21 @@
#include "Arduino_LED_Matrix.h" #include "Arduino_LED_Matrix.h"
#define P1_BTN_UP 13 #include "src/config.h"
#define P1_BTN_BOTTOM 12 #include "src/pong_render.h"
#define P2_BTN_UP 11 #include "src/pong_player.h"
#define P2_BTN_BOTTOM 10 #include "src/pong_ball.h"
#include "src/arduino_network.h"
// create LED matrix object // create LED matrix object
ArduinoLEDMatrix matrix; ArduinoLEDMatrix matrix;
// initial pong frame // initial pong frame matrix
byte frame[8][12] = { byte frame[MATRIX_HEIGHT][MATRIX_WIDTH] = {
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 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 },
{ 1, 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, 1, 0, 0, 0, 0, 0 }, { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, { 0, 0, 0, 0, 0, 0, 1, 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, 1 },
{ 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, 0 } { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
@@ -22,186 +23,47 @@ byte frame[8][12] = {
// players coordinates // players coordinates
int p1_start= 1; int p1_start= 1;
int p1_end= 3;
int p2_start= 4; int p2_start= 4;
int p2_end= 6;
// initials balls coordinates // initials balls coordinates
int ball_reset_x=6; int ball_x= BALL_RESET_X;
int ball_reset_y=3; int ball_y= BALL_RESET_Y;
int ball_x= ball_reset_x;
int ball_y= ball_reset_y;
// initially ball has no movements
// once game/round starts, balls gets random x and y movements
int ball_move_x= 0;
int ball_move_y= 0;
int bar_length= 3;
int p1_score= 0;
int p2_score= 0;
int need_refresh= 1; int need_refresh= 1;
int initial_loop_delay= 200; int ball_delay= INITIAL_BALL_DELAY;
int loop_delay= initial_loop_delay;
// used to increase speed when game is too easy
int hits= 0;
long exec_t2= millis(); long exec_t2= millis();
void setup() { void setup() {
//Serial.begin(115200);
Serial.begin(9600); Serial.begin(9600);
// stard LED matrix // start LED matrix
matrix.begin(); matrix.begin();
pinMode(P1_BTN_UP, INPUT_PULLUP); pinMode(P1_BTN_UP, INPUT_PULLUP);
pinMode(P1_BTN_BOTTOM, INPUT_PULLUP); pinMode(P1_BTN_BOTTOM, INPUT_PULLUP);
pinMode(P2_BTN_UP, INPUT_PULLUP); pinMode(P2_BTN_UP, INPUT_PULLUP);
pinMode(P2_BTN_BOTTOM, INPUT_PULLUP); pinMode(P2_BTN_BOTTOM, INPUT_PULLUP);
randomSeed(analogRead(0)); randomSeed(millis());
}
void render_matrix() { setup_network();
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;
frame[y][x]= 0;
}
}
// players coords
for (int i= p1_start; i < p1_start+bar_length; i++) {
frame[i][0]= 1;
}
for (int i= p2_start; i < p2_start+bar_length; i++) {
frame[i][11]= 1;
}
// ball coords
frame[ball_y][ball_x]= 1;
matrix.renderBitmap(frame, 8, 12);
}
void pong_move_p1() {
if (digitalRead(P1_BTN_UP) == LOW && p1_start > 0) {
p1_start -= 1;
need_refresh= 1;
}
else if (digitalRead(P1_BTN_BOTTOM) == LOW && p1_start < 5) {
p1_start += 1;
need_refresh= 1;
}
}
void pong_move_p2() {
if (digitalRead(P2_BTN_UP) == LOW && p2_start > 0) {
p2_start -= 1;
need_refresh= 1;
}
else if (digitalRead(P2_BTN_BOTTOM) == LOW && p2_start < 5) {
p2_start += 1;
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 > 11 || ball_y < 0 || ball_y > 7) {
// 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;
else ball_move_x= -1;
if (random(2) == 0) ball_move_y= 1;
else ball_move_y= -1;
}
else if (ball_x == 0) {
// if p1 collision: reverse x, go left
if (!ball_player_collision(p1_start)) {
// else p2 score, reset board
p2_score += 1;
Serial.println("Player 2 Point");
point_scored();
}
else {
hits += 1;
ball_move_x= ball_move_x * -1;
}
}
else if (ball_x == 11) {
if (!ball_player_collision(p2_start)) {
// else p1 score, reset board
p1_score += 1;
Serial.println("Player 1 Point");
point_scored();
}
else {
hits += 1;
ball_move_x= ball_move_x * -1;
}
}
if (ball_y == 0 || ball_y == 7) {
// 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;
} }
void loop() { void loop() {
long exec_t1= millis(); long exec_t1= millis();
pong_move_p1(); pong_move_p1(p1_start, need_refresh);
pong_move_p2(); pong_move_p2(p2_start, need_refresh);
render_matrix(); if (exec_t1 - exec_t2 > ball_delay) {
if (exec_t1 - exec_t2 > loop_delay) { move_ball(ball_x, ball_y, ball_delay, p1_start, p2_start, need_refresh);
move_ball();
exec_t2= exec_t1; exec_t2= exec_t1;
} }
delay(50); if (need_refresh) {
render_matrix(frame, p1_start, p2_start, ball_x, ball_y);
matrix.renderBitmap(frame, MATRIX_HEIGHT, MATRIX_WIDTH);
need_refresh= 0;
}
delay(50);
web_server();
} }

View File

Before

Width:  |  Height:  |  Size: 847 KiB

After

Width:  |  Height:  |  Size: 847 KiB

141
src/arduino_network.cpp Normal file
View File

@@ -0,0 +1,141 @@
#include "config.h"
#include "Arduino.h"
#include "WiFiS3.h"
char ssid[]= SECRET_SSID;
char pass[]= SECRET_PASS;
int status= WL_IDLE_STATUS;
WiFiServer server(80);
void printMacAddress(byte mac[]) {
for (int i = 0; i < 6; i++) {
if (i > 0) {
Serial.print(":");
}
if (mac[i] < 16) {
Serial.print("0");
}
Serial.print(mac[i], HEX);
}
Serial.println();
}
void printWifiData() {
// print your board's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
// print your MAC address:
byte mac[6];
WiFi.macAddress(mac);
Serial.print("MAC address: ");
printMacAddress(mac);
}
void printCurrentNet() {
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print the MAC address of the router you're attached to:
byte bssid[6];
WiFi.BSSID(bssid);
Serial.print("BSSID: ");
printMacAddress(bssid);
// print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print("signal strength (RSSI):");
Serial.println(rssi);
// print the encryption type:
byte encryption = WiFi.encryptionType();
Serial.print("Encryption Type:");
Serial.println(encryption, HEX);
Serial.println();
}
void setup_network() {
// check for the WiFi module:
if (WiFi.status() == WL_NO_MODULE) {
Serial.println("Communication with WiFi module failed!");
// don't continue
while (true);
}
String fv = WiFi.firmwareVersion();
Serial.print("Firmware version: ");
Serial.println(fv);
if (fv < WIFI_FIRMWARE_LATEST_VERSION) {
Serial.println("Please upgrade the firmware");
}
// attempt to connect to WiFi network:
// Definisci i parametri di rete prima del loop di connessione
IPAddress local_IP(192, 168, 1, 200);
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 255, 0);
IPAddress dns(9,9,9,9);
WiFi.config(local_IP, gateway, subnet, dns);
while (status != WL_CONNECTED) {
Serial.print("Attempting to connect to WPA SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network:
status = WiFi.begin(ssid, pass);
// wait 10 seconds for connection:
delay(10000);
}
// you're connected now, so print out the data:
Serial.println("You're connected to the network");
printCurrentNet();
printWifiData();
server.begin();
}
void web_server() {
WiFiClient client = server.available();
if (client) { // if you get a client,
Serial.println("new client"); // print a message out the serial port
String currentLine = ""; // make a String to hold incoming data from the client
while (client.connected()) { // loop while the client's connected
if (client.available()) { // if there's bytes to read from the client,
char c = client.read(); // read a byte, then
Serial.write(c); // print it out to the serial monitor
if (c == '\n') { // if the byte is a newline character
// if the current line is blank, you got two newline characters in a row.
// that's the end of the client HTTP request, so send a response:
if (currentLine.length() == 0) {
// HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
// and a content-type so the client knows what's coming, then a blank line:
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println();
// the content of the HTTP response follows the header:
client.print("<p style=\"font-size:7vw;\">Click <a href=\"/H\">here</a> turn the LED on<br></p>");
client.print("<p style=\"font-size:7vw;\">Click <a href=\"/L\">here</a> turn the LED off<br></p>");
// The HTTP response ends with another blank line:
client.println();
// break out of the while loop:
break;
} else { // if you got a newline, then clear currentLine:
currentLine = "";
}
} else if (c != '\r') { // if you got anything else but a carriage return character,
currentLine += c; // add it to the end of the currentLine
}
}
}
// close the connection:
client.stop();
Serial.println("client disconnected");
}
}

11
src/arduino_network.h Normal file
View File

@@ -0,0 +1,11 @@
#ifndef PONG_NETWORK_H
#define PONG_NETWORK_H
#include "Arduino.h"
void setup_network();
void printMacAddress(byte mac[]);
void printWifiData();
void printCurrentNet();
void web_server();
#endif

19
src/config.h Normal file
View File

@@ -0,0 +1,19 @@
#if __has_include("secrets.h")
#include "secrets.h"
#else
#define SECRET_SSID "Fallback_SSID"
#define SECRET_PASS "Fallback_PASS"
#warning "⚠️ secrets.h not found"
#endif
#define P1_BTN_UP 12
#define P1_BTN_BOTTOM 11
#define P2_BTN_UP 10
#define P2_BTN_BOTTOM 9
#define MATRIX_WIDTH 12
#define MATRIX_HEIGHT 8
#define BALL_RESET_X (MATRIX_WIDTH / 2)
#define BALL_RESET_Y (MATRIX_HEIGHT / 2)
#define BAR_LENGTH 3
#define INITIAL_BALL_DELAY 200

88
src/pong_ball.cpp Normal file
View File

@@ -0,0 +1,88 @@
#include <Arduino.h>
#include "config.h"
#include "pong_player.h"
// used to increase speed when game is too easy
int hits= 0;
int p1_score= 0;
int p2_score= 0;
// initially ball has no movements
// once game/round starts, balls gets random x and y movements
int ball_move_x= 0;
int ball_move_y= 0;
void point_scored(int &ball_x, int &ball_y, int &ball_delay) {
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;
ball_delay= INITIAL_BALL_DELAY;
}
void move_ball(int &ball_x, int &ball_y, int &ball_delay, int p1_start, int p2_start, int &need_refresh) {
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;
else ball_move_x= -1;
if (random(2) == 0) ball_move_y= 1;
else ball_move_y= -1;
}
else if (ball_x == 0) {
// if p1 collision: reverse x, go left
if (!ball_player_collision(p1_start, ball_y)) {
// else p2 score, reset board
p2_score += 1;
Serial.println("Player 2 Scores");
point_scored(ball_x, ball_y, ball_delay);
}
else {
hits += 1;
ball_move_x= ball_move_x * -1;
}
}
else if (ball_x == MATRIX_WIDTH-1) {
if (!ball_player_collision(p2_start, ball_y)) {
// else p1 score, reset board
p1_score += 1;
Serial.println("Player 1 Scores");
point_scored(ball_x, ball_y, ball_delay);
}
else {
hits += 1;
ball_move_x= ball_move_x * -1;
}
}
if (ball_y == 0 || ball_y == MATRIX_HEIGHT-1) {
// reverse y, go down
ball_move_y= ball_move_y * -1;
}
if (hits >= 6 && ball_delay >= 80) {
// increase ball speed
hits = 0;
ball_delay -= 20;
}
ball_x+= ball_move_x;
ball_y+= ball_move_y;
}

7
src/pong_ball.h Normal file
View File

@@ -0,0 +1,7 @@
#ifndef PONG_BALL_H
#define PONG_BALL_H
void point_scored();
void move_ball(int &ball_x, int &ball_y, int &loop_delay, int p1_start, int p2_start, int &need_refresh);
#endif

33
src/pong_player.cpp Normal file
View File

@@ -0,0 +1,33 @@
#include <Arduino.h>
#include "config.h"
int ball_player_collision(int player, int ball_y) {
for (int p= player; p < player + BAR_LENGTH; p++) {
if (ball_y == p) {
return 1;
}
}
return 0;
}
int pong_move_p1(int &p1_start, int &need_refresh) {
if (digitalRead(P1_BTN_UP) == LOW && p1_start > 0) {
p1_start -= 1;
need_refresh= 1;
}
else if (digitalRead(P1_BTN_BOTTOM) == LOW && p1_start < 5) {
p1_start += 1;
need_refresh= 1;
}
}
int pong_move_p2(int &p2_start, int &need_refresh) {
if (digitalRead(P2_BTN_UP) == LOW && p2_start > 0) {
p2_start -= 1;
need_refresh= 1;
}
else if (digitalRead(P2_BTN_BOTTOM) == LOW && p2_start < 5) {
p2_start += 1;
need_refresh= 1;
}
}

8
src/pong_player.h Normal file
View File

@@ -0,0 +1,8 @@
#ifndef PONG_PLAYER_H
#define PONG_PLAYER_H
int ball_player_collision(int player, int ball_y);
int pong_move_p1(int &p1_start, int &need_refresh);
int pong_move_p2(int &p2_start, int &need_refresh);
#endif

22
src/pong_render.cpp Normal file
View File

@@ -0,0 +1,22 @@
#include <Arduino.h>
#include "config.h"
void render_matrix(byte frame[MATRIX_HEIGHT][MATRIX_WIDTH], int p1_start, int p2_start, int ball_x, int ball_y) {
// clear
for (int x=0; x < MATRIX_WIDTH; x++) {
for (int y=0; y < MATRIX_HEIGHT; y++) {
frame[y][x]= 0;
}
}
// players coords
for (int i= p1_start; i < p1_start+BAR_LENGTH; i++) {
frame[i][0]= 1;
}
for (int i= p2_start; i < p2_start+BAR_LENGTH; i++) {
frame[i][MATRIX_WIDTH-1]= 1;
}
// ball coords
frame[ball_y][ball_x]= 1;
}

9
src/pong_render.h Normal file
View File

@@ -0,0 +1,9 @@
#ifndef PONG_RENDER_H
#define PONG_RENDER_H
#include <Arduino.h>
#include "Arduino_LED_Matrix.h"
void render_matrix(byte frame[MATRIX_HEIGHT][MATRIX_WIDTH], int p1_start, int p2_start, int ball_x, int ball_y);
#endif