To add a cool effect to the NeoPixel color change, we can implement a gradual transition or "fade" effect between the current color and the new color when there's a change in UV intensity or temperature. This will make the LED transition smoother and more visually appealing.
Here's an updated version of the functions with a fade effect implemented:
import time
import math
def fade_to_color(start_color, end_color, steps=50, delay=0.01):
"""
Smoothly transition from start_color to end_color over a number of steps.
"""
for step in range(steps + 1):
r = int(start_color[0] + (end_color[0] - start_color[0]) * (step / steps))
g = int(start_color[1] + (end_color[1] - start_color[1]) * (step / steps))
b = int(start_color[2] + (end_color[2] - start_color[2]) * (step / steps))
color = (r, g, b)
for i in range(len(np)): # Assuming np is a list of LEDs
np[i] = color
np.write()
time.sleep(delay)
def set_neopixel_color_uv(uv_intensity):
"""
Set NeoPixel colors for UV indicators based on UV intensity,
with a smooth transition effect.
"""
if uv_intensity is None:
new_color = (0, 0, 0) # Default to off if there's no reading
elif uv_intensity >= 0.1:
new_color = (255, 0, 0) # Red for high UV intensity
elif uv_intensity >= 0.05:
new_color = (255, 255, 0) # Yellow for medium UV intensity
elif uv_intensity >= 0.01:
new_color = (255, 255, 255) # White for low UV intensity
else:
new_color = (0, 0, 0)
# Get the current color of the LEDs in the range
current_color = np[UV_LED_RANGE.start] if UV_LED_RANGE else (0, 0, 0)
fade_to_color(current_color, new_color)
def set_neopixel_color_temp(temperature):
"""
Set NeoPixel colors for Temperature indicators based on temperature,
with a smooth transition effect.
"""
if temperature is None:
new_color = (0, 0, 0)
elif 0 <= temperature <= 19:
new_color = (0, 0, 255)
elif 20 <= temperature <= 29:
new_color = (0, 255, 0)
elif temperature >= 30:
new_color = (255, 0, 0)
else:
new_color = (0, 0, 255)
# Get the current color of the LEDs in the range
current_color = np[TEMP_LED_RANGE.start] if TEMP_LED_RANGE else (0, 0, 0)
fade_to_color(current_color, new_color)
# Assuming np has been defined somewhere and is a list-like object of NeoPixels
Explanation
-
Fade Effect: A fade_to_color function is added. It gradually transitions from a starting color to a new color over a specified number of steps and with a delay between each step.
-
Updating Functions: Both set_neopixel_color_uv and set_neopixel_color_temp now utilize fade_to_color to change colors smoothly.
-
Current Color Retrieval: Before starting the fade effect, we determine the current color of the NeoPixels in the specific range. This assumes UV_LED_RANGE and TEMP_LED_RANGE are ranges or indices that point to segments of the NeoPixel array.
The fading effect will make transitions smoother and more visually pleasing, especially if the changes to intensity or temperature are frequent or gradual.