Simulating a Bouncy Ball on the Digital Arduino LED Matrix
by Amanojaku23 in Circuits > Arduino
9 Views, 1 Favorites, 0 Comments
Simulating a Bouncy Ball on the Digital Arduino LED Matrix
We’ll build an interactive bouncy ball simulation using the Arduino UNO R4 WiFi and an analog joystick. You’ll code a 12 x 8 LED matrix display to simulate velocity, collision, and damping. You'll be able to fling a digital ball across the screen and shift gravity down, left, up, or right by clicking.
Supplies
- Arduino UNO R4 WiFi (The LED's are built in so it can play animation)
- Analog Joystick Module
- 5x Jumper Wires (Female-to-Male)
- USB-C Cable
Wiring
Joystick to Arduino Wiring
To connect the analog joystick to the Arduino, we need to wire the positive power, ground, the two directional control pins (X and Y axes), and the push-button switch. The joystick translates physical movement and button clicks into electrical signals that the Arduino interprets to control the ball's velocity and toggle gravity.
These are the corresponding pins from the joystick to the Arduino.
GND (Brown)
Arduino GND
Return path for the electric current. Establishes a shared ground reference.
+5V (Red)
Arduino 5V
Provides a constant 5-Volt stream of electricity to power the joystick.
VRx (Yellow)
Arduino A0
Analog output for horizontal movement.
VRy (Green)
Arduino A1
Analog output for vertical movement.
SW (Blue)
Arduino Pin 2
Digital output for the push-button switch.
You can power the project with a computer via USB-C cable.
Coding
To make the animation, we need to create individual frames that tell the Arduino which LED lights to have off and which to have turned off. We build and display these frames quickly, which simulates a ball bouncing around. The program constantly tracks its position, speed, and the pull of gravity. Inside the main loop, the Arduino reads your joystick movements, pulls the ball downward to simulate gravity, and watches for the edges of the screen. When the ball hits a wall, the code flips its direction and applies a "damping" factor so the ball loses a bit of energy.
I uploaded this code using the PlatformIO extension in VS Code, but you can also use the Arduino IDE. If you are using the official IDE, you can just copy and paste the code below straight into a blank sketch and upload it directly to your Arduino.
Constants and Setup()
Loop()
Entire File
Modifying
Gravity - You can change the GRAVITY variable to something much lower like .01 to significantly reduce the ball's gravity, like it's on the moon.
Damping - You can change the BOUNCE_DAMPING variable to something higher like .9 so the bouncy ball gets much bouncier and loses less energy hitting the walls.
You could also add on to the code by allowing the analog stick to change these variables while the program runs. You could make the frames load quicker, add more balls, or make bigger balls.