from machine import Pin
import time


datapin = Pin(15, Pin.OUT)
clockpin = Pin(19, Pin.OUT)
latchpin = Pin(18, Pin.OUT)

data = 0

def shift_write(desired_pin, desired_state):
    global data
    if desired_state:
        data |= (1 << desired_pin)
    else:
        data &= ~(1 << desired_pin)

    shift_out(datapin, clockpin, data)

    latchpin.on()
    latchpin.off()

def shift_out(datapin, clockpin, value):
    for i in range(8):
        bit = (value >> (7 - i)) & 0x01
        datapin.value(bit)
        clockpin.on()
        clockpin.off()

def one_after_another():
    delay_time = 0.1

    for index in range(8):
        shift_write(index, 1)
        time.sleep(delay_time)


    for index in range(7, -1, -1):
        shift_write(index, 0)
        time.sleep(delay_time)

def one_on_at_a_time():
    delay_time = 0.1

    for index in range(8):
        shift_write(index, 1)
        time.sleep(delay_time)
        shift_write(index, 0) 

def ping_pong():
    delay_time = 0.1 


    for index in range(8):
        shift_write(index, 1)
        time.sleep(delay_time)
        shift_write(index, 0) 


    for index in range(7, -1, -1):
        shift_write(index, 1)
        time.sleep(delay_time)
        shift_write(index, 0)

def random_led():
    import urandom
    delay_time = 0.1

    index = urandom.getrandbits(3)

    shift_write(index, 1)
    time.sleep(delay_time)
    shift_write(index, 0) 

def binary_count():
    global data
    delay_time = 1


    shift_out(datapin, clockpin, data)

    latchpin.on()
    latchpin.off()
    data = (data + 1) % 256

    time.sleep(delay_time)

while True:
      #one_after_another()
      #one_on_at_a_time()
      #ping_pong()
      # random_led()
       binary_count()
BOOTSELLED1239USBRaspberryPiPico©2020RP2-8020/21P64M15.00TTT
74HC595