from machine import Pin
import time, random

green=Pin(17,Pin.OUT)
yellow=Pin(14,Pin.OUT)
red=Pin(15,Pin.OUT)

leds=[green,yellow,red]
global count
count=0

manPin=Pin(18,Pin.IN,Pin.PULL_UP)
button=Pin(16,Pin.IN,Pin.PULL_UP) 

green.on()
yellow.off()
red.off()

def button_handler():
    if button.value()==0:
        global count
        leds[count].off()
        print(count)
        count=count+1
        if count==3:
            count=0
        leds[count].on()
        return count
    
def rand_handler():
    global count
    leds[count].off()
    print(count)
    count=count+1
    if count==3:
        count=0
    leds[count].on()
    return count
    
while True:
    if manPin.value()==1:
        button_handler()
        time.sleep(0.5)
    else:
        rand_handler()
        time.sleep(random.randint(3,15))
    