-
Notifications
You must be signed in to change notification settings - Fork 101
/
Copy pathneopixel_bouncing_ball.py
65 lines (53 loc) · 1.61 KB
/
neopixel_bouncing_ball.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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)