#copycat
#pins: GP2-GP9

from machine import Pin, PWM
from time import sleep

def ByteDisplay(val = 0):
    """Converts entered value into 8 bits of binary number and lights LEDs correspondingly."""
    
    binary = "{0:08b}".format(val)  
    
    
    
    led0 = Pin(2, int(binary[7]))
    led1 = Pin(3, int(binary[6]))
    led2 = Pin(4, int(binary[5]))
    led3 = Pin(5, int(binary[4]))
    led4 = Pin(6, int(binary[3]))
    led5 = Pin(7, int(binary[2]))
    led6 = Pin(8, int(binary[1]))
    led7 = Pin(9, int(binary[0]))

    while True:
        led0.toggle()
        led1.toggle()
        led2.toggle()
        led3.toggle()
        led4.toggle()
        led5.toggle()
        led6.toggle()
        led7.toggle()

def Volta(N = 1, speed = 0.1):
    """LEDs get lighted starting from left ending at the right end, and
    in repeat itself N time at the given speed."""

    for repeat in range(N):    
        for forward in range(2,10):
            led = Pin(forward, Pin.OUT)
            led.high()
            sleep(speed)
            led.low()
        for backward in range(9,1,-1):
            led = Pin(backward, Pin.OUT)
            led.high()
            sleep(speed)
            led.low()
    
    
def FadingVolta(L = 3, speed = 0.1):
    """Similar to Volta() but with fading lights."""
    
    led = {0: PWM(Pin(2)),1: PWM(Pin(3)),2: PWM(Pin(4)),3: PWM(Pin(5)),
           4: PWM(Pin(6)),5: PWM(Pin(7)),6: PWM(Pin(8)),7: PWM(Pin(9))}

    """
    led[0].duty_u16(65000)
    sleep(speed)
    led[0].deinit()
    
    led[1].duty_u16(65000)
    led[0].duty_u16(32500)
    sleep(speed)
    led[1].deinit()
    led[0].deinit()
    
    led[2].duty_u16(65000)
    led[1].duty_u16(32500)
    led[0].duty_u16(17250)
    sleep(speed)
    led[2].deinit()
    led[1].deinit()
    led[0].deinit()
    """
    full = 65000
    decrement = int(65000/7)
    
    
    for forward in range(8):
        try:
                
            forward_buffer = forward
            forward_buffer2 = forward
            forward_buffer = forward_buffer + 1
            counter = 0
            
            while forward >=0:
                
                led[forward].duty_u16(full-decrement*counter)
                forward = forward - 1
                counter = counter + 1
            
            while forward_buffer < 8:
                
                led[forward_buffer].duty_u16(0)
                forward = forward + 1
            
            sleep(speed)
            
            while forward_buffer2 < 8:

                led[forward_buffer2].duty_u16(0)

            
        except KeyError:
            pass
        
    for backward in range(8,-1,-1):
        try:
            led[backward].duty_u16(17500)
            led[backward-1].duty_u16(32500)
            led[backward-2].duty_u16(65000)
            sleep(speed)
            led[backward].duty_u16(0)
            led[backward-1].duty_u16(0)
            led[backward-2].duty_u16(0)
        except KeyError:
            pass




 #    snake = []
 #    size = L-1
 #
 #    try:
 #        led[size]
 #        snake.append(size)
 #        led[size-1]
 #        snake.append(size-1)
 #        led[size-2]
 #        snake.append(size-2)
 #        led[size-3]
 #        snake.append(size-3)
 #        led[size-4]
 #        snake.append(size-4)
 #        led[size-5]
 #        snake.append(size-5)
 #        led[size-6]
 #        snake.append(size-6)
 #        led[size-7]
 #        snake.append(size-7)
 #        led[size-8]
 #        snake.append(size-8)
 #    except KeyError:
 #        pass
 #    snake.reverse()
    
    
FadingVolta()
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
BOOTSELLED1239USBRaspberryPiPico©2020RP2-8020/21P64M15.00TTT