Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bouncing ball example #170

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions docs/examples.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,38 @@ Ensure your device works with this simple test.
:caption: examples/neopixel_simpletest.py
:linenos:

RPI simple test
---------------

Test using the Raspberry Pi.

.. literalinclude:: ../examples/neopixel_rpi_simpletest.py
:caption: examples/neopixel_rpi_simpletest.py
:linenos:

Pixel
-----

Example of setting the color of a single pixel.

.. literalinclude:: ../examples/neopixel_pixel.py
:caption: examples/neopixel_pixel.py
:linenos:

Rainbow
-------

Example of cycling through the colors of the rainbow.

.. literalinclude:: ../examples/neopixel_rainbowio_simpletest.py
:caption: examples/neopixel_rainbowio_simpletest.py
:linenos:

Bouncing ball
-------------

Example of a bouncing ball animation.

.. literalinclude:: ../examples/neopixel_bouncing_ball.py
:caption: examples/neopixel_bouncing_ball.py
:linenos:
65 changes: 65 additions & 0 deletions examples/neopixel_bouncing_ball.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# SPDX-FileCopyrightText: 2025 Jose D. Montoya
# SPDX-License-Identifier: MIT

# This example simulates a ball bouncing on a NeoPixel strip affected by gravity.
# Most NeoPixels = neopixel.GRB or neopixel.GRBW
# The 8mm Diffused NeoPixel (PID 1734) = neopixel.RGB
import time
from math import ceil
import board
import neopixel


# Configure the setup
PIXEL_PIN = board.A3 # pin that the NeoPixel is connected to
ORDER = neopixel.RGB # pixel color channel order
COLOR = (255, 50, 150) # color to blink
CLEAR = (0, 0, 0) # clear (or second color)
DELAY = 0.1 # blink rate in seconds

# Simulation Values.
gravity = 0.5
velocity = 2
energy_loss = 0.6

# Create the NeoPixel object
num_pixels = 60
pixel_seg = neopixel.NeoPixel(PIXEL_PIN, num_pixels, pixel_order=ORDER)

# Animation start values
travel = 0
going_up = False
floor_count = 0
top = 0

# Loop forever and simulate
while True:
# Blink the color
pixel_seg[travel] = COLOR
time.sleep(0.05)
pixel_seg[travel] = CLEAR
time.sleep(DELAY)
velocity += gravity

# Check if the ball is at the top to change direction
if velocity >= 0 and going_up:
velocity = ceil(-velocity)
going_up = False
top = travel

# Check if the ball is at the bottom to break the loop
if top == num_pixels - 1:
floor_count = floor_count + 1
if floor_count == 3:
break

travel = int(travel + velocity)

# Check if the ball is at the floor to change direction
if travel >= num_pixels:
travel = num_pixels - 1
velocity = ceil(-velocity * energy_loss)
going_up = True

# Clear the strip
pixel_seg.fill(0)