from machine import Pin
from neopixel import NeoPixel
import time, _thread, random

# Set up LED pins
seg1 = Pin(0, Pin.OUT)
seg2 = Pin(1, Pin.OUT)
seg3 = Pin(2, Pin.OUT)
seg4 = Pin(3, Pin.OUT)
seg5 = Pin(4, Pin.OUT)
seg6 = Pin(6, Pin.OUT)

# Create a list of our LEDs
segments = [seg1, seg2, seg3, seg4, seg5, seg6]

# Set up buttons
greenbtn = Pin(14, Pin.IN, Pin.PULL_DOWN)
redbtn = Pin(15, Pin.IN, Pin.PULL_DOWN)

# Define the strip pin number (2) and number of LEDs (12)
ring = NeoPixel(Pin(16), 12)

# Initialize ring
ring.fill((0,0,0))
ring.write()
time.sleep(1)

#r = random.randint(0,100)
#g = random.randint(0,100)
#b = random.randint(0,100)

#while True:
def ledScan():
    # For loop to turn each LED on then off in order of the list
    for led in segments:

        led.value(1)
        time.sleep(0.08)
        led.value(0)
    # For loop in reverse, running backwards through the list
    for led in reversed (segments):

        led.value(1)
        time.sleep(0.08)
        led.value(0)

def colorRing():
    # Create random RGB values
    r = random.randint(0,100)
    g = random.randint(0,100)
    b = random.randint(0,100)

    for i in range(12):

        # Light each LED a random colour
        ring[i] = (r,g,b)
        ring.write()

        # Show the LED for this long
        time.sleep(0.05)

        #Clear the ring at the end of each loop
        #ring.fill((0,0,0))
        ring.write()
while True:
    #ledScan()
    if greenbtn.value()==1:
        _thread.start_new_thread(colorRing(),())
    if redbtn.value()==1:
        ring.fill((0,0,0))
        ring.write()
    ledScan()