from machine import Pin, ADC, PWM
import time
import neopixel
# Define number of NeoPixel LEDs
NUM_PIXELS = 16
# Define pins for NeoPixel, potentiometer, relay, and buzzer
neo_pin = Pin(27)
pot_pin = ADC(28)
relay_pin = Pin(22, Pin.OUT)
buzzer_pin = Pin(21, Pin.OUT)
# Create NeoPixel object
np = neopixel.NeoPixel(neo_pin, NUM_PIXELS)
# Create PWM for buzzer
buzzer_pwm = PWM(buzzer_pin)
def map_value(x, in_min, in_max, out_min, out_max):
return (x - in_min) * (out_max - out_min) // (in_max - in_min) + out_min
def play_buzzer(frequency, duration):
buzzer_pwm.freq(frequency)
buzzer_pwm.duty_u16(32768) # Set duty cycle to 50%
time.sleep(duration)
buzzer_pwm.duty_u16(0) # Turn off buzzer
# Define a list of colors (RGB tuples) for each LED
# Define a list of colors (RGB tuples) for each LED
colors = [
(255, 0, 0), # Red
(255, 69, 0), # Red-Orange (Orange Red)
(255, 215, 0), # Gold
(0, 255, 0), # Lime
(50, 205, 50), # Lime Green
(0, 255, 255), # Cyan
(0, 191, 255), # Deep Sky Blue
(0, 0, 255), # Blue
(138, 43, 226), # Blue Violet
(255, 20, 147), # Deep Pink
(255, 105, 180),# Hot Pink
(128, 0, 128), # Purple
(255, 99, 71), # Tomato
(255, 69, 0), # Red Orange
(255, 192, 203),# Pink
(255, 255, 255) # White
]
while True:
# Read value from potentiometer
pot_value = pot_pin.read_u16()
# Print potentiometer value to Serial Monitor
# Map potentiometer value to the number of LEDs to light up
num_leds = map_value(pot_value, 0, 65535, 0, NUM_PIXELS)
# Set NeoPixel LEDs
for i in range(NUM_PIXELS):
if i < num_leds:
# Use colors based on the index; wrap around if there are more LEDs than colors
np[i] = colors[i % len(colors)]
else:
np[i] = (0, 0, 0) # Turn off remaining LEDs
np.write()
# Check if all LEDs are on
if num_leds == NUM_PIXELS:
play_buzzer(1000, 0.5) # Sound buzzer at 1000 Hz for 0.5 seconds
# Check if potentiometer value is above mid-point
if pot_value > 32768:
relay_pin.on() # Turn on relay if above midpoint
else:
relay_pin.off() # Turn off relay if below midpoint
time.sleep(0.1)