from machine import Pin
import utime
# === CONFIG ===
led_pins = [Pin(2, Pin.OUT),
Pin(4, Pin.OUT),
Pin(5, Pin.OUT),
Pin(7, Pin.OUT),
Pin(9, Pin.OUT),
Pin(10, Pin.OUT),
Pin(12, Pin.OUT),
Pin(14, Pin.OUT),] #[Pin(i, Pin.OUT) for i in range(0, 8)]
button = Pin(15, Pin.IN, Pin.PULL_UP)
# Modes: 0=running light, 1=running light stay-on, 2=alternating, 3=BCD counter
mode = 0
direction = 1 # 1=normal, -1=reverse
last_press_time = 0
press_count = 0
hold_time = 1000 # ms
double_click_gap = 400 # ms
counter = 0
def clear_leds():
for led in led_pins:
led.value(0)
def running_light(step):
idx = step % 8
clear_leds()
led_pins[idx].value(1)
def running_light_stayon(step):
clear_leds()
pos = step % 8
if direction == 1: # left to right
for i in range(pos + 1):
led_pins[i].value(1)
else: # right to left
for i in range(pos, 8):
led_pins[i].value(1)
def alternating(step):
clear_leds()
if direction == 1:
group_size = 4
else:
group_size = 2
phase = (step // group_size) % 2
for i in range(8):
if (i // group_size) % 2 == phase:
led_pins[i].value(1)
def bcd_counter(step):
val = step %10
for i in range(4):
led_pins[i].value((val >> i) & 1)
led_pins[i+4].value((val >> i) & 1)
print(val)
def update_mode(step):
if mode == 0:
running_light(step)
elif mode == 1:
running_light_stayon(step)
elif mode == 2:
alternating(step)
elif mode == 3:
bcd_counter(step)
step = 0
while True:
# --- Button handling ---
if not button.value(): # button pressed
start_hold = utime.ticks_ms()
is_hold = False
# Wait while button is still pressed
while not button.value():
# Check hold condition
if utime.ticks_diff(utime.ticks_ms(), start_hold) > hold_time:
clear_leds() # turn off LEDs for hold
is_hold = True
while not button.value(): # wait release
pass
step = 0
break
# Count press only if it wasn't a hold
if not is_hold:
press_count += 1
last_press_time = utime.ticks_ms()
# --- Check for single or double press ---
if press_count > 0 and utime.ticks_diff(utime.ticks_ms(), last_press_time) > double_click_gap:
if press_count == 1:
direction *= -1 # reverse direction
elif press_count == 2:
mode = (mode + 1) % 4 # change mode
press_count = 0
# --- Update step counter ---
if mode == 3: # BCD counter mode
if direction == 1:
step = (step + 1) % 100
else:
step = (step - 1) % 100
else:
step += direction
# --- Update LEDs ---
update_mode(step)
utime.sleep(0.2)
Fabre, Carl Vincent
Intal, Elijah
Roxas, Aiam
Rubio, Emerson
Sir sa pagdouble isakto nyo po 400ms pagitan ng click para meregister