from neopixel import Neopixel
import utime
# --- Basic Setup ---
num_cols = 8
leds_per_col = 4
numpix = num_cols * leds_per_col
strip = Neopixel(numpix, 0, 28, "RGB")
white = (255, 255, 255)
black = (0, 0, 0)
# --- NEW: Parameters to control the column fading effect ---
# The number of steps for the fade between one column and the next.
num_steps_per_col = 15
# The maximum brightness for the center column (0-255).
max_brightness = 200
# The delay between each of the 10 steps. A very short delay
# will make the transition appear as one smooth motion.
step_delay = 0.05 # 10 steps * 0.025s = 0.25s per column transition
# --- Helper function to easily set the brightness of a whole column ---
def set_column_brightness(col_index, brightness):
"""Sets all LEDs in a given column to a specific brightness."""
# Make sure brightness is within the valid 0-255 range
brightness = max(0, min(255, int(brightness)))
# Calculate the first pixel of the column
start_pixel = col_index * leds_per_col
end_pixel = start_pixel + leds_per_col - 1
strip.set_pixel_line(start_pixel, end_pixel, white, brightness)
# --- The Main Loop ---
current_center_col = 0
while True:
# --- Define the four columns involved in this transition ---
# The column that is currently fading from 100% down to 50%
col_fading_down = current_center_col
# The column that is fading from 50% up to 100%
col_fading_up = (current_center_col + 1) % num_cols
# The column that is fading from 0% up to 50%
col_fading_in = (current_center_col + 2) % num_cols
# The column that is fading from 50% down to 0%
col_fading_out = (current_center_col - 1 + num_cols) % num_cols
# --- Inner loop for the smooth transition ---
for step in range(num_steps_per_col + 1): # Loop from 0 to 10
# Calculate the brightness for each column based on the current step
# This creates the linear fade you described.
# Fades from 100% -> 50%
brightness_current = max_brightness * (1.0 - 0.5 * (step / num_steps_per_col))
# Fades from 50% -> 100%
brightness_next = max_brightness * (0.5 + 0.5 * (step / num_steps_per_col))
# Fades from 0% -> 50%
brightness_fading_in = max_brightness * (0.5 * (step / num_steps_per_col))
# Fades from 50% -> 0%
brightness_fading_out = max_brightness * (0.5 - 0.5 * (step / num_steps_per_col))
# --- Update the LED strip ---
strip.clear() # Clear the previous state
set_column_brightness(col_fading_down, brightness_current)
set_column_brightness(col_fading_up, brightness_next)
set_column_brightness(col_fading_in, brightness_fading_in)
set_column_brightness(col_fading_out, brightness_fading_out)
strip.show()
utime.sleep(step_delay)
# Move to the next column for the next major transition
current_center_col = (current_center_col + 1) % num_cols