Hi All I have attempted to merge 2 pieces of code using 2 buttons.
Button 1 is for Brightness.
Button 2 is for the patterns.
In this basic first design (just so it’s not too complicated) the animation modes(button 2) gives simple different colours.
Button 1 will only control red which is how the brightness code originally started out. In doing this now (because I have merged 2 pieces of code, the leds will turn red at the specified brightness of button 1(when pushed) and the shell will show the percentage brightness.
The leds will then return to whichever (current_mode) colour has been selected using button 2
What I am trying to achieve is for the whole strip or ring to still be color/pattern controlled with button 2 and for button 1 to change the brightness for whichever colour /pattern is showing at the time.
code below
any assistance greatly appreciated
cheers
Nick
import neopixel
from machine import Pin
import time
# NeoPixel setup
PIXEL_PIN = 0 # Example pin, adjust as needed
NUM_PIXELS = 24 # Number of NeoPixels
np = neopixel.NeoPixel(Pin(PIXEL_PIN), NUM_PIXELS)
#pixels = neopixel.NeoPixel(Pin(NEOPIXEL_PIN), NUM_PIXELS)
# Button setup
BUTTON1_PIN = 14 # to change brightness
BUTTON2_PIN = 15 # to change patterns
button1 = Pin(BUTTON1_PIN, Pin.IN, Pin.PULL_UP)
button2 = Pin(BUTTON2_PIN, Pin.IN, Pin.PULL_UP)
brightness_levels = [0.05, 0.1, 0.3, 0.6, 1.0] # Example brightness levels
current_brightness_index = 0
# Define colors (RGB tuples)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
PINK = (250, 0 ,250)
ORANGE = (255, 50, 0)
OFF = (0, 0, 0)
colors = [RED, GREEN, BLUE, OFF]
current_color_index = 0
def set_all_pixels_color(r, g, b, brightness):
for i in range(NUM_PIXELS):
np[i] =(int(r * brightness), int(g * brightness), int(b * brightness))
np.write()
# Initial state
set_all_pixels_color(255, 0, 0, brightness_levels[current_brightness_index])
# Set initial color
np.fill(colors[current_color_index])
np.write()
current_mode = 0
num_modes = 6 # Adjust based on your number of animation functions
last_button_state = True # True for unpressed (pull-up)
last_debounce_time = 0
DEBOUNCE_DELAY = 50 # milliseconds
def set_all_pixels_color(r, g, b, brightness): # this is meant to enable brightness control
for i in range(NUM_PIXELS):
np[i] = (int(r * brightness), int(g * brightness), int(b * brightness))
np.write()
def animation_mode_0(): #set_all_pixels_color
# Example: Solid Red
for i in range(NUM_PIXELS):
np[i] = (255, 0, 0)
np.write()
def animation_mode_1(): #set_all_pixels_color
# Example: Solid Blue
for i in range(NUM_PIXELS):
np[i] = (0, 0, 255)
np.write()
def animation_mode_2(): # Example: Rainbow cycle (simplified, needs more logic for smooth animation)
for i in range(NUM_PIXELS):
np[i] = (0, 255, 0) # Green
np.write()
def animation_mode_3():
for i in range(NUM_PIXELS):
np[i] = (250, 0, 250) # Pink
np.write()
def animation_mode_4(): #set_all_pixels_color
# Example: Rainbow cycle (simplified, needs more logic for smooth animation)
for i in range(NUM_PIXELS):
np[i] = (250, 55, 0) # Orange
np.write()
def animation_mode_5(): #set_all_pixels_color
# Example: Rainbow cycle (simplified, needs more logic for smooth animation)
for i in range(NUM_PIXELS):
np[i] = (0, 0, 0) # Off
np.write()
animations = [animation_mode_0, animation_mode_1, animation_mode_2, animation_mode_3, animation_mode_4,animation_mode_5 ]
while True:
# #brightness
# if button1.value() == 0: # Button pressed
# time.sleep_ms(50) # Debounce
# if button1.value() == 0: # Confirm press after debounce
# current_brightness_index = (current_brightness_index + 1) % len(brightness_levels)
# set_all_pixels_color(255, 0, 0, brightness_levels[current_brightness_index])
# print(f"Brightness set to: {brightness_levels[current_brightness_index] * 100}%")
# while button1.value() == 0: # Wait for button release
# time.sleep_ms(10)
# time.sleep_ms(10) # Small delay for main loop
#brightness
if button1.value() == 0: # Button pressed
time.sleep_ms(50) # Debounce
if button1.value() == 0: # Confirm press after debounce
current_brightness_index = (current_brightness_index + 1) % len(brightness_levels)
set_all_pixels_color(255, 0, 0, brightness_levels[current_brightness_index])
print(f"Brightness set to: {brightness_levels[current_brightness_index] * 100}%")
while button1.value() == 0: # Wait for button release
time.sleep_ms(10)
time.sleep_ms(10) # Small delay for main loop
# Button 2 colour change polling and debouncing
current_button2_state = button2.value()
if current_button2_state == False and last_button2_state == True: # Button pressed
if (time.ticks_ms() - last_debounce_time) > DEBOUNCE_DELAY:
current_mode = (current_mode + 1) % num_modes
last_debounce_time = time.ticks_ms()
last_button2_state = current_button2_state
# Execute current animation mode
animations[current_mode]()
# Short delay to prevent busy-waiting too intensely, adjust as needed
time.sleep_ms(10)
#time.sleep_ms(10) # Small delay to prevent busy-waiting
# # pattern change
# if button2.value() == 0: # Button is pressed (pulled low)
# time.sleep_ms(50) # Debounce delay
# if button2.value() == 0: # Confirm button press after debounce
# current_color_index = (current_color_index + 1) % len(colors)
# np.fill(colors[current_color_index])
# np.write()
# while button2.value() == 0: # Wait for button release
# time.sleep_ms(10)
# time.sleep_ms(10) # Small delay to prevent busy-waiting