Compare commits
8 Commits
fa47480aef
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f710fb2a0e | ||
|
|
748cf6a678 | ||
|
|
b1b421c367 | ||
|
|
abc381f397 | ||
|
|
5ebf74e044 | ||
|
|
486ee4df97 | ||
|
|
b9bc138cad | ||
|
|
e7f0c3cc1a |
63
README.md
63
README.md
@@ -1,3 +1,62 @@
|
|||||||
# 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.
|
||||||
|
|
||||||
|
# 📹 Preview
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
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 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. |
|
||||||
|
|
||||||
|
> **Note:** If your board is on a different port, edit the `Makefile` or override it: `make upload PORT=/dev/ttyUSB0`.
|
||||||
|
|||||||
113
arduino_pong.ino
113
arduino_pong.ino
@@ -42,7 +42,13 @@ int bar_length= 3;
|
|||||||
int p1_score= 0;
|
int p1_score= 0;
|
||||||
int p2_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();
|
long exec_t2= millis();
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
@@ -59,6 +65,8 @@ void setup() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void render_matrix() {
|
void render_matrix() {
|
||||||
|
if (!need_refresh) return;
|
||||||
|
need_refresh= 0;
|
||||||
// clear
|
// clear
|
||||||
for (int x=0; x < 12; x++) {
|
for (int x=0; x < 12; x++) {
|
||||||
for (int y=0; y < 8; y++) {
|
for (int y=0; y < 8; y++) {
|
||||||
@@ -84,30 +92,59 @@ void render_matrix() {
|
|||||||
void pong_move_p1() {
|
void pong_move_p1() {
|
||||||
if (digitalRead(P1_BTN_UP) == LOW && p1_start > 0) {
|
if (digitalRead(P1_BTN_UP) == LOW && p1_start > 0) {
|
||||||
p1_start -= 1;
|
p1_start -= 1;
|
||||||
Serial.print("P1 up: ");
|
need_refresh= 1;
|
||||||
Serial.println(p1_start);
|
|
||||||
}
|
}
|
||||||
else if (digitalRead(P1_BTN_BOTTOM) == LOW && p1_start < 5) {
|
else if (digitalRead(P1_BTN_BOTTOM) == LOW && p1_start < 5) {
|
||||||
p1_start += 1;
|
p1_start += 1;
|
||||||
Serial.print("P1 down: ");
|
need_refresh= 1;
|
||||||
Serial.println(p1_start);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void pong_move_p2() {
|
void pong_move_p2() {
|
||||||
if (digitalRead(P2_BTN_UP) == LOW && p2_start > 0) {
|
if (digitalRead(P2_BTN_UP) == LOW && p2_start > 0) {
|
||||||
p2_start -= 1;
|
p2_start -= 1;
|
||||||
Serial.print("P2 up: ");
|
need_refresh= 1;
|
||||||
Serial.println(p2_start);
|
|
||||||
}
|
}
|
||||||
else if (digitalRead(P2_BTN_BOTTOM) == LOW && p2_start < 5) {
|
else if (digitalRead(P2_BTN_BOTTOM) == LOW && p2_start < 5) {
|
||||||
p2_start += 1;
|
p2_start += 1;
|
||||||
Serial.print("P2 down: ");
|
need_refresh= 1;
|
||||||
Serial.println(p2_start);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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() {
|
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) {
|
if (ball_move_x == 0 || ball_move_y == 0) {
|
||||||
// extract random number between 0 or 1 to select the directions
|
// extract random number between 0 or 1 to select the directions
|
||||||
if (random(2) == 0) ball_move_x= 1;
|
if (random(2) == 0) ball_move_x= 1;
|
||||||
@@ -118,55 +155,41 @@ void move_ball() {
|
|||||||
|
|
||||||
else if (ball_x == 0) {
|
else if (ball_x == 0) {
|
||||||
// if p1 collision: reverse x, go left
|
// if p1 collision: reverse x, go left
|
||||||
int hit= 0;
|
if (!ball_player_collision(p1_start)) {
|
||||||
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) {
|
|
||||||
// else p2 score, reset board
|
// else p2 score, reset board
|
||||||
ball_x= ball_reset_x;
|
|
||||||
ball_y= ball_reset_y;
|
|
||||||
p2_score += 1;
|
p2_score += 1;
|
||||||
|
Serial.println("Player 2 Point");
|
||||||
Serial.print("Player 2 score: ");
|
point_scored();
|
||||||
Serial.println(p2_score);
|
}
|
||||||
Serial.print("Player 1 score: ");
|
else {
|
||||||
Serial.println(p1_score);
|
hits += 1;
|
||||||
|
ball_move_x= ball_move_x * -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (ball_x == 11) {
|
else if (ball_x == 11) {
|
||||||
int hit= 0;
|
if (!ball_player_collision(p2_start)) {
|
||||||
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 p1 score, reset board
|
// else p1 score, reset board
|
||||||
ball_x= ball_reset_x;
|
|
||||||
ball_y= ball_reset_y;
|
|
||||||
p1_score += 1;
|
p1_score += 1;
|
||||||
Serial.print("Player 2: ");
|
Serial.println("Player 1 Point");
|
||||||
Serial.println(p2_score);
|
point_scored();
|
||||||
Serial.print("Player 1: ");
|
}
|
||||||
Serial.println(p1_score);
|
else {
|
||||||
|
hits += 1;
|
||||||
|
ball_move_x= ball_move_x * -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
else if (ball_y == 0 || ball_y == 7) {
|
if (ball_y == 0 || ball_y == 7) {
|
||||||
// reverse y, go down
|
// reverse y, go down
|
||||||
ball_move_y= ball_move_y * -1;
|
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_x+= ball_move_x;
|
||||||
ball_y+= ball_move_y;
|
ball_y+= ball_move_y;
|
||||||
}
|
}
|
||||||
@@ -180,5 +203,5 @@ void loop() {
|
|||||||
move_ball();
|
move_ball();
|
||||||
exec_t2= exec_t1;
|
exec_t2= exec_t1;
|
||||||
}
|
}
|
||||||
delay(10);
|
delay(50);
|
||||||
}
|
}
|
||||||
|
|||||||
BIN
preview.png
Normal file
BIN
preview.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 847 KiB |
Reference in New Issue
Block a user