From 4a2e880da7301518d3273600f1e7a1851658816f Mon Sep 17 00:00:00 2001 From: jposada2020 Date: Wed, 5 Feb 2025 23:19:08 -0500 Subject: [PATCH 1/3] bouncing ball example --- docs/examples.rst | 32 +++++++++++++++ examples/neopixel_bouncing_ball.py | 64 ++++++++++++++++++++++++++++++ 2 files changed, 96 insertions(+) create mode 100644 examples/neopixel_bouncing_ball.py diff --git a/docs/examples.rst b/docs/examples.rst index 4648a06..729d94c 100644 --- a/docs/examples.rst +++ b/docs/examples.rst @@ -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_rainbow_simpletest.py + :caption: examples/neopixel_rainbow_simpletest.py + :linenos: + +Bouncing ball +------------- + +Example of a bouncing ball animation. + +.. literalinclude:: ../examples/neopixel_bouncing_ball.py + :caption: examples/neopixel_bouncing_ball.py + :linenos: diff --git a/examples/neopixel_bouncing_ball.py b/examples/neopixel_bouncing_ball.py new file mode 100644 index 0000000..048cb24 --- /dev/null +++ b/examples/neopixel_bouncing_ball.py @@ -0,0 +1,64 @@ +# 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 +import board +import neopixel +from math import ceil + +# 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) From b6af454e45e1e1e6d0910ec1571a771a33fe93bf Mon Sep 17 00:00:00 2001 From: jposada2020 Date: Wed, 5 Feb 2025 23:19:49 -0500 Subject: [PATCH 2/3] example import --- examples/neopixel_bouncing_ball.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/examples/neopixel_bouncing_ball.py b/examples/neopixel_bouncing_ball.py index 048cb24..2f9d9dd 100644 --- a/examples/neopixel_bouncing_ball.py +++ b/examples/neopixel_bouncing_ball.py @@ -5,9 +5,10 @@ # 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 -from math import ceil + # Configure the setup PIXEL_PIN = board.A3 # pin that the NeoPixel is connected to From 7df6c58683012751c922a864d177646342090c8f Mon Sep 17 00:00:00 2001 From: jposada2020 Date: Wed, 5 Feb 2025 23:25:51 -0500 Subject: [PATCH 3/3] example filename correction --- docs/examples.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/examples.rst b/docs/examples.rst index 729d94c..9e4224f 100644 --- a/docs/examples.rst +++ b/docs/examples.rst @@ -30,8 +30,8 @@ Rainbow Example of cycling through the colors of the rainbow. -.. literalinclude:: ../examples/neopixel_rainbow_simpletest.py - :caption: examples/neopixel_rainbow_simpletest.py +.. literalinclude:: ../examples/neopixel_rainbowio_simpletest.py + :caption: examples/neopixel_rainbowio_simpletest.py :linenos: Bouncing ball