Files
WaterTank/Maker_Pico_Box/main.py
2026-06-16 13:02:06 +02:00

173 lines
5.0 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import machine
import time
import neopixel
# ============================================================
# HEARTBEAT CLASS — smooth fade with MIN + MAX brightness
# ============================================================
class HeartbeatFadeNeoPixel:
def __init__(self, neopixel_obj, pixel_index=0, color=(255, 0, 0),
min_brightness=5, max_brightness=100,
step=2, interval_ms=20):
self.np = neopixel_obj
self.index = pixel_index
self.color = color
self.min_brightness = min_brightness
self.max_brightness = max_brightness
self.step = step
self.interval = interval_ms
self.brightness = min_brightness
self.direction = 1 # 1 = fade up, -1 = fade down
self.last_time = time.ticks_ms()
def apply_brightness(self, c, level):
r, g, b = c
factor = level / 100
return (int(r * factor), int(g * factor), int(b * factor))
def update(self):
now = time.ticks_ms()
if time.ticks_diff(now, self.last_time) >= self.interval:
self.last_time = now
# Update brightness
self.brightness += self.direction * self.step
# Bounce at limits
if self.brightness >= self.max_brightness:
self.brightness = self.max_brightness
self.direction = -1
if self.brightness <= self.min_brightness:
self.brightness = self.min_brightness
self.direction = 1
# Apply brightness
self.np[self.index] = self.apply_brightness(self.color, self.brightness)
self.np.write()
# ============================================================
# NEOPIXEL SETUP
# ============================================================
STRIP_PIN = 17
STRIP_LEDS = 3
strip = neopixel.NeoPixel(machine.Pin(STRIP_PIN), STRIP_LEDS)
SINGLE_PIN = 28
single = neopixel.NeoPixel(machine.Pin(SINGLE_PIN), 1)
# ============================================================
# BUTTONS
# ============================================================
button_pins = [20, 21, 22]
buttons = [machine.Pin(pin, machine.Pin.IN, machine.Pin.PULL_UP) for pin in button_pins]
# ============================================================
# COLOR PALETTE
# ============================================================
colors = [
(0, 0, 0), # Off
(255, 0, 0), # Red
(0, 255, 0), # Green
(0, 0, 255), # Blue
# (255, 255, 0), # Yellow
# (0, 255, 255), # Cyan
# (255, 0, 255), # Magenta
# (255, 128, 0), # Orange
# (128, 0, 255), # Purple
# (255, 20, 147), # Pink
# (128, 255, 0), # Lime
(255, 255, 255), # White
]
# ============================================================
# STRIP STATE
# ============================================================
color_index = [0, 0, 0]
brightness = [50, 50, 50]
last_state = [1, 1, 1]
press_time = [0, 0, 0]
# ============================================================
# HEARTBEAT CONFIG (NOW HAS MIN + MAX)
# ============================================================
FLASH_COLOR_INDEX = 1 # Palette color
FLASH_MIN = 1 # << lowest brightness (0100)
FLASH_MAX = 10 # << highest brightness
FLASH_STEP = 0.2 # fade speed
FLASH_INTERVAL_MS = 20 # smoothness
heartbeat = HeartbeatFadeNeoPixel(
neopixel_obj=single,
pixel_index=0,
color=colors[FLASH_COLOR_INDEX],
min_brightness=FLASH_MIN,
max_brightness=FLASH_MAX,
step=FLASH_STEP,
interval_ms=FLASH_INTERVAL_MS
)
# ============================================================
# HELPERS
# ============================================================
def apply_brightness(color, level):
r, g, b = color
factor = level / 100
return (int(r * factor), int(g * factor), int(b * factor))
def update_strip():
for i in range(STRIP_LEDS):
strip[i] = apply_brightness(colors[color_index[i]], brightness[i])
strip.write()
# ============================================================
# INITIAL
# ============================================================
update_strip()
# ============================================================
# MAIN LOOP
# ============================================================
while True:
now = time.ticks_ms()
# BUTTON CONTROLLED PIXELS (02)
for i in range(3):
state = buttons[i].value()
if last_state[i] == 1 and state == 0:
press_time[i] = now
if last_state[i] == 0 and state == 1:
duration = time.ticks_diff(now, press_time[i])
if duration > 500:
brightness[i] += 10
if brightness[i] > 100:
brightness[i] = 10
else:
color_index[i] = (color_index[i] + 1) % len(colors)
update_strip()
time.sleep_ms(200)
last_state[i] = state
# HEARTBEAT ON GP28
heartbeat.update()
time.sleep_ms(10)