Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 8082

MicroPython • Controlling Neopixels with at least 2 buttons and possibly ssd1306 and Pico2

$
0
0
Hi All
I have a new project!!!!!
I am trying to sort out how to control neopixels with multiple patterns using push buttons and ssd1306 with Pico2 and micropython.
However it is not running as as smoothly as I thought it would
With the code as is , which I will publish below I was expecting that the code that is between
#zzzzzzzzzzzzzzzzzzz and
#zzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
would enable me to select pattern 1,2,3 etc with button 1 and then use button 2 to activate the pattern. I t doesn't appear to be working in this fashion.
It appears to me that the code is only using the ""current mode "" part of the code from line76 onwards , despite this button 2 can still make some changes

If I uncomment the code between lines 105 and 138 and comment out line 102 I get errors which I cannot fix
It seems like it would be easier to have one button and no OLED......this wasn't in the plan
Is anyone able to assist

cheers
Nick

Code:

from machine import Pin, I2Cimport neopixelimport  machineimport ssd1306import time# NeoPixel setupNEOPIXEL_PIN = 0NUM_PIXELS = 7np = neopixel.NeoPixel(Pin(NEOPIXEL_PIN), NUM_PIXELS)# OLED setupOLED_SDA = 20OLED_SCL = 21i2c = I2C(0, sda=Pin(OLED_SDA), scl=Pin(OLED_SCL))oled = ssd1306.SSD1306_I2C(128, 64, i2c)# Button setupBUTTON1_PIN = 15BUTTON2_PIN = 12button1 = Pin(BUTTON1_PIN, Pin.IN, Pin.PULL_UP)button2 = Pin(BUTTON2_PIN, Pin.IN, Pin.PULL_UP)current_mode = 0def update_display():    oled.fill(0) # Clear display    oled.text("NeoPixel Control", 0, 0)    oled.text(f"Mode: {current_mode}", 0, 16)    oled.show()#zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzwhile True:    #PATTERN 1    MODE 0    if button1.value() == 0: # Button 1 pressed        current_mode = (current_mode + 1) % 6 # Cycle through modes        update_display()        time.sleep(0.1) # Debounce        if button2.value() == 0: # Button 2 pressed        # Implement specific action for button 2, e.g., change color        np[0] = (255, 0, 0) # Set first pixel to red        np.write()    time.sleep(0.1) # Debounce#xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx    #PATTERN 2      MODE 2    if button1.value() == 2: # Button 1 pressed        current_mode = (current_mode + 1) % 3 # Cycle through modes        update_display()    time.sleep(0.1) # Debounce        if button2.value() == 0: # Button 2 pressed        # Implement specific action for button 2, e.g., change color        np[0] = (255, 250, 0) # Set first pixel to red        np.write()    time.sleep(0.1) # Debounce           #xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx    # PATTERN 3     MODE 3    if button1.value() == 3: # Button 1 pressed        current_mode = (current_mode + 1) % 3 # Cycle through modes        update_display()    time.sleep(0.1) # Debounce        if button2.value() == 0: # Button 2 pressed        # Implement specific action for button 2, e.g., change color        np[1] = (0, 250, 0) # Set second pixel to yellow        np[4] = (0, 250, 0) # Set second pixel to yellow        np.write()    time.sleep(0.1) # Debounce   #zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz#xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx# NeoPixel logic based on current_mode    if current_mode == 0:        # Static color        np.fill((0, 0, 255)) # Blue        np[0] = (250, 25, 0)  #orange        np[3] = (250, 25, 0)  #orange        np[6] = (250, 25, 0)  #orange        np.write()        time.sleep(0.2) # Debounce        #np.fill((0, 0, 0)) # off   this causes blinking        pass    elif current_mode == 1:        # Rainbow animation        # (Implement rainbow animation logic here)        np[0] = (255, 0, 250) # Set first pixel to red        np.write()        time.sleep(0.2) # Debounce        pass    elif current_mode == 2:        np.fill((0, 250, 0)) # Green        # Blinking        # (Implement blinking logic here)        pass        np.write()        time.sleep(0.1)    elif current_mode == 3:        np.fill((0, 250, 200)) # light blue        # Blinking        # (Implement blinking logic here)        #cccccccccccccccccccccccccccccccccccccccccccccccccccc#         # Define colors (RGB tuples)#         RED = (255, 0, 0)#         GREEN = (0, 255, 0)#         BLUE = (0, 0, 255)#         PINK = (250, 0, 250)#         OFF = (0, 0, 0)# # def blink_strip(color, on_delay, off_delay):#     """#     Turns all pixels on with a given color, waits, then turns them off and waits again.#     """#     for i in range(NUM_PIXELS):#         np[i] = color#         np.write()#         time.sleep(on_delay)# #     for i in range(NUM_PIXELS):#         np[i] = OFF#         np.write()#         time.sleep(off_delay)# # while True:#     # Blink red#     blink_strip(RED, ON_DELAY, OFF_DELAY)#     # Blink green#     blink_strip(GREEN, ON_DELAY, OFF_DELAY)#     # Blink blue#     blink_strip(BLUE, ON_DELAY, OFF_DELAY)#     # Blink blue#     blink_strip(PINK, ON_DELAY, OFF_DELAY) #                             #ccccccccccccccccccccccccccccccccccccccccccccccccccc        pass        np.write()        time.sleep(0.1)    elif current_mode == 4:        np.fill((100, 250, 0)) # Yellow green        # Blinking        # (Implement blinking logic here)        pass        np.write()        time.sleep(0.1)            elif current_mode == 5:        np.fill((250, 25, 0)) # Orange    # Blinking    # (Implement blinking logic here)    pass    np.write()    time.sleep(0.1)

Statistics: Posted by Nick100 — Thu Aug 28, 2025 2:39 am



Viewing all articles
Browse latest Browse all 8082

Trending Articles