# NeoPixels Rainbow on MicroPython
# Wokwi Example https://wokwi.com/arduino/projects/305569065545499202
from machine import Pin
from neopixel import NeoPixel
import time
# from adafruit_led_animation.animation.sparkle import Sparkle
# from adafruit_led_animation.animation.sparklepulse import SparklePulse
# from adafruit_led_animation.animation.chase import Chase
# from adafruit_led_animation.sequence import AnimationSequence
# from adafruit_led_animation.color import AMBER, JADE, RED, WHITE
# rainbow = [
# (126 , 1 , 0),(114 , 13 , 0),(102 , 25 , 0),(90 , 37 , 0),(78 , 49 , 0),(66 , 61 , 0),(54 , 73 , 0),(42 , 85 , 0),
# (30 , 97 , 0),(18 , 109 , 0),(6 , 121 , 0),(0 , 122 , 5),(0 , 110 , 17),(0 , 98 , 29),(0 , 86 , 41),(0 , 74 , 53),
# (0 , 62 , 65),(0 , 50 , 77),(0 , 38 , 89),(0 , 26 , 101),(0 , 14 , 113),(0 , 2 , 125),(9 , 0 , 118),(21 , 0 , 106),
# (33 , 0 , 94),(45 , 0 , 82),(57 , 0 , 70),(69 , 0 , 58),(81 , 0 , 46),(93 , 0 , 34),(105 , 0 , 22),(117 , 0 , 10)]
num_pixels = 16
pixels = NeoPixel(Pin(15), num_pixels)
reset_button = Pin(4, Pin.IN, Pin.PULL_UP)
total_wait_time = 30 # * 60 # 30 minutes
session_time = 0
init_run_time = total_wait_time/(num_pixels+1)
init_run = True
# divide total_wait_time by num_pixels to get the wait time for each pixel
wait_time_per_pixel = total_wait_time / num_pixels
# compute the start and stop time for each pixel wait time and save it into a list of tuples
session_segments = [(wait_time_per_pixel * i, wait_time_per_pixel * (i + 1), i) for i in range(num_pixels)]
def fill_wait_bar(session_time):
for i in range(num_pixels):
if (i+1)*wait_time_per_pixel <= session_time:
pixels[i] = (255, 0, 0)
elif session_time < (i+1)*wait_time_per_pixel <= session_time + wait_time_per_pixel:
# pixels[i] = (255, 127,0)
pixels[i] = (255, 70, 0)
else:
pixels[i] = (0, 255, 0)
pixels.write()
while True:
# otherwise increment session_time by 1
if reset_button.value() != False:
session_time += 1
# check if reset button is pressed and set session_time to 0
else:
session_time = 0
init_run = True
pixels.fill((0,255,0))
pixels.write()
time.sleep( round(total_wait_time/100*5,0) )
if init_run:
print(f'init_run: {session_time}')
pixels.fill((0,255,0))
# pixels.show()
if session_time >= init_run_time:
session_time = 0
init_run = False
continue
time.sleep(1)
continue
if session_time >= total_wait_time:
# sparkle = Sparkle(pixels, speed=0.05, color=AMBER, num_sparkles=10)
# sparkle_pulse = SparklePulse(pixels, speed=0.05, period=3, color=RED)
# chase = Chase(pixels, speed=0.2, size=6, spacing=8, color=(30, 0, 0))
# animations = AnimationSequence(
# chase,
# # sparkle_pulse,
# advance_interval=5,
# auto_clear=True,
# )
while reset_button.value() != False:
# animations.animate()
print('button not pressed')
fill_wait_bar(session_time)
print(session_time)
time.sleep(1)