from machine import Pin
import utime
from neopixel import Neopixel
# Constants for Pins
TRIGGER_PIN = 0
ECHO_PIN = 1
# Constants for LED Gauge
MIN_DISTANCE = 50
MAX_DISTANCE = 200
MIN_LEDS = 0
MAX_LEDS = 16
# Constants for LED Colors
GREEN_COLOR = (0, 255, 0)
RED_COLOR = (255, 0, 0)
OFF_COLOR = (0, 0, 0) # Black, for turning off LEDs
# Brightness factor (between 0.0 and 1.0)
BRIGHTNESS = 0.5 # Adjust this value to set the desired brightness
# Constants for Flashing
FLASH_INTERVALS = {0: 0.2, 1: 0.3, 2: 0.4, 3: 0.5}
# Initialize Ultrasonic Sensor
trigger = Pin(TRIGGER_PIN, Pin.OUT)
echo = Pin(ECHO_PIN, Pin.IN)
def get_distance():
trigger.low()
utime.sleep_us(2)
trigger.high()
utime.sleep_us(5)
trigger.low()
signaloff = utime.ticks_us() # Initialize with default values
while echo.value() == 0:
signaloff = utime.ticks_us()
signalon = utime.ticks_us() # Initialize with default values
while echo.value() == 1:
signalon = utime.ticks_us()
timepassed = signalon - signaloff
distance = (timepassed * 0.0343) / 2
return distance
# Initialize Neopixel
pixels = Neopixel(16, 0, 6, "GRB")
# Toggle variable for the direction of LED calculation
reverse_leds = True
while True:
raw_distance = get_distance()
# Ensure distance is within the specified range
distance = max(MIN_DISTANCE, min(MAX_DISTANCE, raw_distance))
if reverse_leds:
# Calculate the number of LEDs to turn green inversely proportional to distance
leds_to_turn_green = MAX_LEDS - int((distance - MIN_DISTANCE) / (MAX_DISTANCE - MIN_DISTANCE) * (MAX_LEDS - MIN_LEDS))
else:
# Calculate the number of LEDs to turn green proportional to distance
leds_to_turn_green = int((distance - MIN_DISTANCE) / (MAX_DISTANCE - MIN_DISTANCE) * (MAX_LEDS - MIN_LEDS)) + MIN_LEDS
# Determine the flashing interval based on the number of green LEDs
flash_interval = FLASH_INTERVALS.get(leds_to_turn_green, 0.2)
# LED Gauge Display
if leds_to_turn_green <= 3:
for i in range(16):
if reverse_leds:
led_index = (16 - i - 1) # Anticlockwise direction
else:
led_index = i # Clockwise direction
if led_index < leds_to_turn_green:
pixels.set_pixel(led_index, tuple(int(c * BRIGHTNESS) for c in GREEN_COLOR))
else:
pixels.set_pixel(led_index, tuple(int(c * BRIGHTNESS) for c in RED_COLOR))
pixels.show()
utime.sleep(flash_interval)
for i in range(16):
if reverse_leds:
led_index = (16 - i - 1) # Anticlockwise direction
else:
led_index = i # Clockwise direction
if led_index < leds_to_turn_green:
pixels.set_pixel(led_index, tuple(int(c * BRIGHTNESS) for c in GREEN_COLOR))
else:
pixels.set_pixel(led_index, tuple(int(c * BRIGHTNESS) for c in OFF_COLOR))
pixels.show()
utime.sleep(flash_interval)
else:
# All LEDs are green, and distance is outside the range
if leds_to_turn_green == 16 and (raw_distance < MIN_DISTANCE or raw_distance > MAX_DISTANCE):
#print("overfill")
for i in range(16):
pixels.set_pixel(i, tuple(int(c * BRIGHTNESS) for c in GREEN_COLOR))
pixels.show()
utime.sleep(0.2)
for i in range(16):
pixels.set_pixel(i, tuple(int(c * BRIGHTNESS) for c in OFF_COLOR))
pixels.show()
utime.sleep(0.2)
else:
# Delay for the normal display
for i in range(16):
if reverse_leds:
led_index = (16 - i - 1) # Anticlockwise direction
else:
led_index = i # Clockwise direction
if led_index < leds_to_turn_green:
pixels.set_pixel(led_index, tuple(int(c * BRIGHTNESS) for c in GREEN_COLOR))
else:
pixels.set_pixel(led_index, tuple(int(c * BRIGHTNESS) for c in RED_COLOR))
pixels.show()
utime.sleep(0.1) # Adjust this value to change the duration of the normal display