from machine import Pin
import time
# LED pins (LSB = GP2, MSB = GP9)
led_pins = [2, 3, 4, 5, 6, 7, 8, 9]
leds = [Pin(p, Pin.OUT) for p in led_pins]
# Buttons
toggle_btn = Pin(10, Pin.IN, Pin.PULL_DOWN)
rotate_btn = Pin(11, Pin.IN, Pin.PULL_DOWN)
# Initial value (0–255)
value = 0
# Previous states for edge detection
last_toggle = 0
last_rotate = 0
# Function to display binary on LEDs
def update_leds(val):
for i in range(8):
bit = (val >> i) & 1 # extract each bit
leds[i].value(bit)
while True:
t = toggle_btn.value()
r = rotate_btn.value()
#Toggle LSB (rightmost bit)
if t == 1 and last_toggle == 0:
value ^= 1 # flip LSB
print("Value:", value)
time.sleep(0.2) # debounce
#Circular right shift
if r == 1 and last_rotate == 0:
lsb = value & 1
value = (value >> 1) | (lsb << 7)
print("Value:", value)
time.sleep(0.2) # debounce
last_toggle = t
last_rotate = r
update_leds(value)
time.sleep(0.05)