from time import sleep, ticks_ms, ticks_diff
from machine import ADC, Pin
from neopixel import NeoPixel
# Hardware parameters ESP32, NeoPixel, Button
PIN_NR_21 = 21
PIN_NR_25 = 25
PIN_NR_33 = 33 # ADC-capable
LED_AANTAL = 16
DENDER_VALUE = 0.3
pin = Pin(PIN_NR_21, Pin.OUT) # set GPIO0 to output to drive NeoPixels
np = NeoPixel(pin, LED_AANTAL) # create NeoPixel driver on GPIO0
button = Pin(PIN_NR_25, Pin.IN, Pin.PULL_DOWN) # start-stop
adc_input = ADC(Pin(PIN_NR_33)) #Define analog input pin
time_stamp = [ticks_ms()] * 1 # list for multiple delay counters if needed
# NON-BlOCKING Function
def nonblocking_delay_passed(delay_ms=1000, delay_index=0):
global time_stamp
if ticks_diff(ticks_ms(), time_stamp[delay_index]) > delay_ms:
time_stamp[delay_index] = ticks_ms()
return True
return False
# START-STOP BUTTON Function
def push_start_stop(push_pin, push_prev, push_now, push_status):
push_now = push_pin.value()
if push_now != push_prev:
push_prev = push_now
if push_now:
push_status = not push_status
print(f"Stopped: {push_status}")
sleep(DENDER_VALUE)
return push_status
# Potentiometer for adapting speed between 0.2 - 1 sec.
def control_strip_speed(adc):
pot_value = adc.read() # Read the analog value (0-4095)
output_adc = map_range(pot_value, 0, 4095, 1000, 200)
print("Analog Value:", f"{pot_value:4}", "\tFlash time:", f"{output_adc:.2f}", "ms")
return output_adc
# Function to map a number from one range to another (Copilot)
def map_range(num, in_min, in_max, out_min, out_max):
return (num - in_min) * (out_max - out_min) / (in_max - in_min) + out_min
# MAIN PROGRAM
STEP = 1 # step size for led loop
flash_time = 0 # ms
press_now = False
press_prev = False
stopped = False # defines how the ledstrip will start: stopped or not
while True:
i = 0
while i < LED_AANTAL:
flash_time = control_strip_speed(adc_input) # get pot meter value
stopped = push_start_stop(button, press_prev, press_now, stopped) # check button status
np[i] = (255, 0, 0) # red, full intensity
np.write()
if not stopped:
if nonblocking_delay_passed(flash_time, 0):
print(f"Delay {flash_time} ms. i = {i}")
np[i] = (0, 0, 0) # reset pixel
np.write()
i += STEP