from machine import Pin
import time
# Set output pins
SD = Pin(15, Pin.OUT)
SH_CP = Pin(14, Pin.OUT)
ST_CP = Pin(13, Pin.OUT)
green_led = Pin(12, Pin.OUT)
red_led = Pin(11, Pin.OUT)
# Set input pins
button_high = Pin(10, Pin.IN, Pin.PULL_DOWN) # Right
button_low = Pin(9, Pin.IN, Pin.PULL_DOWN) # Left
Q0 = Pin(28, Pin.IN)
Q1 = Pin(27, Pin.IN)
Q2 = Pin(26, Pin.IN)
Q3 = Pin(22, Pin.IN)
Q4 = Pin(21, Pin.IN)
Q5 = Pin(20, Pin.IN)
Q6 = Pin(19, Pin.IN)
Q7 = Pin(18, Pin.IN)
bit_count = 0 # Count button presses
def shift_and_latch():
global bit_count
# Shift data values
time.sleep_ms(5)
SH_CP.low()
time.sleep_ms(5)
SH_CP.high()
time.sleep_ms(5)
# Latch data
ST_CP.low()
time.sleep_ms(5)
ST_CP.high()
bit_count += 1
# RIGHT button interrupt handler
def button_high_isr(pin):
# Set register
SD.value(1)
shift_and_latch()
print("1", end='')
# LEFT button interrupt handler
def button_low_isr(pin):
# Set register
SD.value(0)
shift_and_latch()
print("0", end='')
# Attach interrupts to buttons
button_high.irq(trigger=Pin.IRQ_RISING, handler=button_high_isr)
button_low.irq(trigger=Pin.IRQ_RISING, handler=button_low_isr)
# Function to check the pattern
def check_pattern():
# Check values of shifted registers
if (Q0.value() == 0 and Q1.value() == 0 and Q2.value() == 0 and Q3.value() == 0 and Q4.value() == 1 and Q5.value() == 1 and Q6.value() == 1 and Q7.value() == 1):
print("\nSUCCESS!")
green_led.value(1)
red_led.value(0)
else:
print("\nFAIL!")
green_led.value(0)
red_led.value(1)
# Initialize LEDs state
green_led.value(0)
red_led.value(0)
# Main loop
while True:
if bit_count == 8:
check_pattern()
time.sleep(1)
break