Skip to content

Commit 403fd01

Browse files
authored
Merge pull request #170 from jposada202020/bouncing_ball_example
Bouncing ball example
2 parents 1a95235 + 7df6c58 commit 403fd01

2 files changed

Lines changed: 97 additions & 0 deletions

File tree

docs/examples.rst

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,38 @@ Ensure your device works with this simple test.
77
:caption: examples/neopixel_simpletest.py
88
:linenos:
99

10+
RPI simple test
11+
---------------
12+
13+
Test using the Raspberry Pi.
14+
1015
.. literalinclude:: ../examples/neopixel_rpi_simpletest.py
1116
:caption: examples/neopixel_rpi_simpletest.py
1217
:linenos:
18+
19+
Pixel
20+
-----
21+
22+
Example of setting the color of a single pixel.
23+
24+
.. literalinclude:: ../examples/neopixel_pixel.py
25+
:caption: examples/neopixel_pixel.py
26+
:linenos:
27+
28+
Rainbow
29+
-------
30+
31+
Example of cycling through the colors of the rainbow.
32+
33+
.. literalinclude:: ../examples/neopixel_rainbowio_simpletest.py
34+
:caption: examples/neopixel_rainbowio_simpletest.py
35+
:linenos:
36+
37+
Bouncing ball
38+
-------------
39+
40+
Example of a bouncing ball animation.
41+
42+
.. literalinclude:: ../examples/neopixel_bouncing_ball.py
43+
:caption: examples/neopixel_bouncing_ball.py
44+
:linenos:

examples/neopixel_bouncing_ball.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# SPDX-FileCopyrightText: 2025 Jose D. Montoya
2+
# SPDX-License-Identifier: MIT
3+
4+
# This example simulates a ball bouncing on a NeoPixel strip affected by gravity.
5+
# Most NeoPixels = neopixel.GRB or neopixel.GRBW
6+
# The 8mm Diffused NeoPixel (PID 1734) = neopixel.RGB
7+
import time
8+
from math import ceil
9+
import board
10+
import neopixel
11+
12+
13+
# Configure the setup
14+
PIXEL_PIN = board.A3 # pin that the NeoPixel is connected to
15+
ORDER = neopixel.RGB # pixel color channel order
16+
COLOR = (255, 50, 150) # color to blink
17+
CLEAR = (0, 0, 0) # clear (or second color)
18+
DELAY = 0.1 # blink rate in seconds
19+
20+
# Simulation Values.
21+
gravity = 0.5
22+
velocity = 2
23+
energy_loss = 0.6
24+
25+
# Create the NeoPixel object
26+
num_pixels = 60
27+
pixel_seg = neopixel.NeoPixel(PIXEL_PIN, num_pixels, pixel_order=ORDER)
28+
29+
# Animation start values
30+
travel = 0
31+
going_up = False
32+
floor_count = 0
33+
top = 0
34+
35+
# Loop forever and simulate
36+
while True:
37+
# Blink the color
38+
pixel_seg[travel] = COLOR
39+
time.sleep(0.05)
40+
pixel_seg[travel] = CLEAR
41+
time.sleep(DELAY)
42+
velocity += gravity
43+
44+
# Check if the ball is at the top to change direction
45+
if velocity >= 0 and going_up:
46+
velocity = ceil(-velocity)
47+
going_up = False
48+
top = travel
49+
50+
# Check if the ball is at the bottom to break the loop
51+
if top == num_pixels - 1:
52+
floor_count = floor_count + 1
53+
if floor_count == 3:
54+
break
55+
56+
travel = int(travel + velocity)
57+
58+
# Check if the ball is at the floor to change direction
59+
if travel >= num_pixels:
60+
travel = num_pixels - 1
61+
velocity = ceil(-velocity * energy_loss)
62+
going_up = True
63+
64+
# Clear the strip
65+
pixel_seg.fill(0)

0 commit comments

Comments
 (0)