import time
import color
from rotary_irq_rp2 import RotaryIRQ
from machine import Pin, PWM
base_polling_speed = 250
reset_time_s = 2 # press and hold to reset (in seconds)
SW = Pin(20, Pin.IN, Pin.PULL_UP)
r,g,b,pins = color.init_color_pins(7, 9, 8)
rotor = RotaryIRQ(pin_num_clk=18,
pin_num_dt=19,
min_val=0,
max_val=255,
reverse=False,
range_mode=RotaryIRQ.RANGE_WRAP)
val_old = rotor.value()
current_color = 0
while True:
try:
#print(rotor.value())
if val_old is None or rotor.value() != val_old:
# increment the color in the direction the rotary was turned
current_color += (rotor.value()-val_old)
val_new = rotor.value()
# Figure out the color from the new rotor value
colors = color.wheel(val_new)
# print(colors)
color.set_color(colors[0], colors[1], colors[2])
# Reset color if button is pressed
if SW.value() == 0:
reset_clock += base_polling_speed
# Until reset threshold, count off the reset clock updates
if reset_clock < reset_time_s*1000:
print(f"--------- Button is Pressed --- reset clock: {reset_clock}")
# after crossing the reset threshold (but just once)
if reset_clock >= reset_time_s*1000 and reset_clock < reset_time_s*1000+base_polling_speed:
print(">>>>>>>>>>>>>>>>>>>>>>>>>>>>> Resetting")
# Reset rotor position and set LED to white
rotor.set(value=0)
color.set_color(255, 255, 255)
# after resetting, print another message after 10 cycles
if reset_clock % (base_polling_speed*10) == 0 and reset_clock > reset_time_s*1000+base_polling_speed:
print("Already Reset")
else:
# Set reset clock in nanoseconds
reset_clock = 0
except KeyboardInterrupt:
break
time.sleep_ms(base_polling_speed)