def set_neopixel_color_uv(uv_intensity):
"""
Set NeoPixel colors for UV indicators based on UV intensity.
"""
if uv_intensity is None:
color = (0, 0, 0) # Default to off if there's no reading
elif uv_intensity >= 0.1:
color = (255, 0, 0) # Red for high UV intensity
elif uv_intensity >= 0.05:
color = (255, 255, 0) # Yellow for medium UV intensity
elif uv_intensity >= 0.01:
color = (255, 255, 255) # White for low UV intensity
else:
color = (0, 0, 0)
code
for i in UV_LED_RANGE:
np[i] = color
np.write()
def set_neopixel_color_temp(temperature):
"""
Set NeoPixel colors for Temperature indicators based on temperature.
"""
if temperature is None:
color = (0, 0, 0)
elif 0 <= temperature <= 19:
color = (0, 0, 255)
elif 20 <= temperature <= 29:
color = (0, 255, 0)
elif temperature >= 30:
color = (255, 0, 0)
else:
color = (0, 0, 255)
code
for i in TEMP_LED_RANGE:
np[i] = color
np.write()
adding wave effect
Assistant
Adding a wave effect to the NeoPixel color setting functions can create a more dynamic and visually appealing display. A wave effect typically involves gradually changing the intensity or color of the LEDs in sequence, simulating a wave-like motion across the strip. Here’s how you can modify your set_neopixel_color_uv and set_neopixel_color_temp functions to include a wave effect:
python
import time
import math
WAVE_PERIOD = 2 # Time period for a full wave cycle
WAVE_AMPLITUDE = 0.5 # Amplitude of brightness modulation from min to max
WAVE_SPEED = 0.5 # Speed factor of the wave animation
def calculate_wave_brightness(index, total_leds, time_elapsed):
# Calculate the wave effect
offset = WAVE_SPEED * time_elapsed + index * 2 * math.pi / total_leds
wave_brightness = WAVE_AMPLITUDE * (1 + math.sin(offset))
return wave_brightness
def apply_wave_effect(color, index, total_leds, time_elapsed):
# Apply the wave effect to the color
wave_brightness = calculate_wave_brightness(index, total_leds, time_elapsed)
return tuple(int(c * wave_brightness) for c in color)
def set_neopixel_color_uv(uv_intensity):
if uv_intensity is None:
base_color = (0, 0, 0)
elif uv_intensity >= 0.1:
base_color = (255, 0, 0)
elif uv_intensity >= 0.05:
base_color = (255, 255, 0)
elif uv_intensity >= 0.01:
base_color = (255, 255, 255)
else:
base_color = (0, 0, 0)
time_start = time.time()
while True:
time_elapsed = time.time() - time_start
for i in UV_LED_RANGE:
np[i] = apply_wave_effect(base_color, i, len(UV_LED_RANGE), time_elapsed)
np.write()
time.sleep(0.05) # Adjust the sleep for smoothness and speed
def set_neopixel_color_temp(temperature):
if temperature is None:
base_color = (0, 0, 0)
elif 0 <= temperature <= 19:
base_color = (0, 0, 255)
elif 20 <= temperature <= 29:
base_color = (0, 255, 0)
elif temperature >= 30:
base_color = (255, 0, 0)
else:
base_color = (0, 0, 255)
time_start = time.time()
while True:
time_elapsed = time.time() - time_start
for i in TEMP_LED_RANGE:
np[i] = apply_wave_effect(base_color, i, len(TEMP_LED_RANGE), time_elapsed)
np.write()
time.sleep(0.05) # Adjust the sleep for smoothness and speed
Explanation:
Wave Calculation:
calculate_wave_brightness: This function calculates the brightness modulation for each LED based on the sine wave function, resulting in a smooth transition from min to max brightness.
Applying the Wave:
apply_wave_effect: This function adjusts the brightness of the base color for each LED according to the wave calculated.
Timing and Animation Loop:
In both set_neopixel_color_uv and set_neopixel_color_temp, the wave effect is animated over time using an infinite while loop.
The time.sleep(0.05) adds a delay to control the speed of the wave. Adjust this to make the animation faster or slower.
Continuous Animation:
The while loop continuously animates the wave. You may want to have an exit condition or integrate these functions into running contexts where they run for a specific duration or under certain conditions.
By integrating these changes, you'll create an engaging and smooth wavy light pattern in response to your UV and temperature measurements. Adjust WAVE_PERIOD, WAVE_AMPLITUDE, and WAVE_SPEED to fit your specific aesthetic and performance needs.