def setneopixelcoloruv(uvintensity, blink=False, blinkinterval=0.5, blinkduration=5):…
- User
def set_neopixel_color_uv(uv_intensity, blink=False, blink_interval=0.5, blink_duration=5): """ Set NeoPixel colors for UV indicators based on UV intensity. Optionally add a blinking effect for a certain duration.
:param uv_intensity: The UV intensity reading. :param blink: Boolean indicating whether to blink the LEDs. :param blink_interval: Time interval for blinking in seconds. :param blink_duration: Total duration to blink in seconds. """ if uv_intensity is None: color = (0, 0, 0) # Off 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) # Off if blink: end_time = time.time() + blink_duration while time.time() < end_time: # Turn on LEDs for i in UV_LED_RANGE: np[i] = color np.write() time.sleep(blink_interval) # Turn off LEDs for i in UV_LED_RANGE: np[i] = (0, 0, 0) np.write() time.sleep(blink_interval) else: # Set LEDs normally for i in UV_LED_RANGE: np[i] = color np.write()def set_neopixel_color_temp(temperature, blink=False, blink_interval=0.5, blink_duration=5): """ Set NeoPixel colors for Temperature indicators based on temperature. Optionally add a blinking effect for a certain duration.
:param temperature: The temperature reading. :param blink: Boolean indicating whether to blink the LEDs. :param blink_interval: Time interval for blinking in seconds. :param blink_duration: Total duration to blink in seconds. """ if temperature is None: color = (0, 0, 0) # Off elif 0 <= temperature <= 19: color = (0, 0, 255) # Blue for low temperature elif 20 <= temperature <= 29: color = (0, 255, 0) # Green for medium temperature elif temperature >= 30: color = (255, 0, 0) # Red for high temperature else: color = (0, 0, 255) # Default to blue if blink: end_time = time.time() + blink_duration while time.time() < end_time: # Turn on LEDs for i in TEMP_LED_RANGE: np[i] = color np.write() time.sleep(blink_interval) # Turn off LEDs for i in TEMP_LED_RANGE: np[i] = (0, 0, 0) np.write() time.sleep(blink_interval) else: # Set LEDs normally for i in TEMP_LED_RANGE: np[i] = color np.write()adding any effect you think it's cool
- Assistant
To add an interesting effect beyond simple blinking, we can incorporate a gradient effect on the NeoPixels. This effect will gradually transition the colors on the NeoPixels to the target color, creating a smooth, visually appealing transition. Here's how you could update both functions to apply this gradient effect before setting the final color:
import time def apply_gradient(np, led_range, start_color, end_color, steps, delay): """Gradually change the color of LEDs from start_color to end_color.""" for step in range(steps): intermediate_color = ( int(start_color[0] + (end_color[0] - start_color[0]) * step / steps), int(start_color[1] + (end_color[1] - start_color[1]) * step / steps), int(start_color[2] + (end_color[2] - start_color[2]) * step / steps) ) for i in led_range: np[i] = intermediate_color np.write() time.sleep(delay) def set_neopixel_color_uv(uv_intensity, blink=False, blink_interval=0.5, blink_duration=5): """ Set NeoPixel colors for UV indicators based on UV intensity. Optionally add a blinking effect for a certain duration. """ if uv_intensity is None: target_color = (0, 0, 0) # Off elif uv_intensity >= 0.1: target_color = (255, 0, 0) # Red for high UV intensity elif uv_intensity >= 0.05: target_color = (255, 255, 0) # Yellow for medium UV intensity elif uv_intensity >= 0.01: target_color = (255, 255, 255) # White for low UV intensity else: target_color = (0, 0, 0) # Off apply_gradient(np, UV_LED_RANGE, (0, 0, 0), target_color, steps=20, delay=0.05) if blink: end_time = time.time() + blink_duration while time.time() < end_time: # Turn off LEDs to create blinking effect for i in UV_LED_RANGE: np[i] = (0, 0, 0) np.write() time.sleep(blink_interval) # Turn on LEDs back to the target color for i in UV_LED_RANGE: np[i] = target_color np.write() time.sleep(blink_interval) def set_neopixel_color_temp(temperature, blink=False, blink_interval=0.5, blink_duration=5): """ Set NeoPixel colors for Temperature indicators based on temperature. Optionally add a blinking effect for a certain duration. """ if temperature is None: target_color = (0, 0, 0) # Off elif 0 <= temperature <= 19: target_color = (0, 0, 255) # Blue for low temperature elif 20 <= temperature <= 29: target_color = (0, 255, 0) # Green for medium temperature elif temperature >= 30: target_color = (255, 0, 0) # Red for high temperature else: target_color = (0, 0, 255) # Default to blue apply_gradient(np, TEMP_LED_RANGE, (0, 0, 0), target_color, steps=20, delay=0.05) if blink: end_time = time.time() + blink_duration while time.time() < end_time: # Turn off LEDs to create blinking effect for i in TEMP_LED_RANGE: np[i] = (0, 0, 0) np.write() time.sleep(blink_interval) # Turn on LEDs back to the target color for i in TEMP_LED_RANGE: np[i] = target_color np.write() time.sleep(blink_interval)Explanation:
-
Gradient Effect: The
apply_gradientfunction calculates intermediate colors between the initial and target color over several steps, updating the LEDs to create a smooth transition. -
Integration: Before setting the final color,
apply_gradientis called with initial colors set to(0, 0, 0)(off) so the LEDs smoothly light up to the target color. -
Parameters: The
stepsanddelayparameters inapply_gradientcontrol the smoothness and speed of the transition. Adjust them according to your preference.
This effect adds a touch of elegance when the LEDs change color, enhancing the visual feedback beyond static colors and simple blinking.
-