from machine import Pin, PWM
from utime import sleep
import machine
from neopixel import NeoPixel
from random import randrange
print("started!")
import time
nl = 16
timediff= 0
leds = NeoPixel(Pin(12, Pin.OUT),nl)
button = Pin(21,Pin.IN, Pin.PULL_UP)
barve = [[255,0,0], [0,255,0], [0,0,255]]
times = 0
x=0
pressed = False
colors = {
"r":0,
"g":0,
"b":0
}
# 1st LED must be green
leds[0] = barve[1]
while True:
print(int((time.time_ns()/1000000) - times))
did = False
if not button.value():
if not pressed:
# makes the first button press more accurate
if x == 0:
times = time.time_ns()/1000000
leds.write()
x += 1
pressed = True
continue
# it light only one led per press
pressed = True
# time in nanoseconds devided by 1 mil --> 1 sec = 1000
timediff = (time.time_ns()/1000000) - times
if timediff < 1000:
leds[x] = [0,255,0]
colors["g"] +=1
did=True
elif timediff >= 1000 and timediff < 2000:
leds[x] = [0,0,255]
colors["b"] +=1
did=True
elif timediff >= 2000:
leds[x] = [255,0,0]
colors["r"] +=1
did=True
if did:
x+=1
times = time.time_ns()/1000000
leds.write()
# the inner if loop will execute again only after we stop holding the button
else:
pressed = False
#initiates ordering sequence
if x == nl:
break
print(colors)
for i in range(nl):
if colors["r"] >= 0:
colors["r"] -=1
leds[i] = [255,0,0]
elif colors["b"] >= 0:
colors["b"] -=1
leds[i] = [0,0,255]
elif colors["g"] >= 0:
colors["g"] -=1
leds[i] = [0,255,0]
print("done :)")
leds.write()