Joy-it RB-P-XPLR-SET handleiding
Handleiding
Je bekijkt pagina 33 van 37

33
from machine import Pin, ADC
import neopixel
import utime
# Neopixel setup
NUM_LEDS = 4
PIXEL_PIN = 1
np = neopixel.NeoPixel(Pin(PIXEL_PIN), NUM_LEDS)
# Photodiode setup on ADC pin GP28 (A2)
fotodiode = ADC(2)
# Conversion function for brightness values of the photodiode into a suitable
brightness for the LEDs
def brightness_from_light(sensor_value):
# Minimum and maximum sensor value
min_sensor_value = 400
max_sensor_value = 10000
# Invert the sensor value within the actual range
normalized_value = max_sensor_value - sensor_value + min_sensor_value
# Scale the inverted value to a brightness range (0.05 to 0.5)
# Adjust the scaling: Divide by (max_sensor_value - min_sensor_value)
return max(0.05, min(0.5, normalized_value / (max_sensor_value - min_sensor_
value) * 0.45 + 0.05))
def update_leds(brightness):
color = (255, 255, 255) # White
dimmed_color = tuple([int(c * brightness) for c in color])
for i in range(NUM_LEDS):
np[i] = dimmed_color
np.write()
while True:
# Read the sensor value from the photodiode
light_value = fotodiode.read_u16()
print(light_value)
# Calculate the brightness based on the sensor value
brightness = brightness_from_light(light_value)
# Update the LEDs with the new brightness
update_leds(brightness)
# Waiting time to reduce the load on the CPU and for smoother brightness
transitions
utime.sleep(0.5)
SUMMARY: We use our photodiode to measure the ambient brightness and adjust the brightness of four LEDs. The intensity of the
LEDs changes according to the light detected by the photodiode, with darker environments leading to brighter LEDs and vice versa.
It is best to use a flashlight to achieve the best possible result.
Initialization of the LEDs and the
photodiode
Measurement of the photodiode
and controlling the brightness
Bekijk gratis de handleiding van Joy-it RB-P-XPLR-SET, stel vragen en lees de antwoorden op veelvoorkomende problemen, of gebruik onze assistent om sneller informatie in de handleiding te vinden of uitleg te krijgen over specifieke functies.
Productinformatie
Merk | Joy-it |
Model | RB-P-XPLR-SET |
Categorie | Niet gecategoriseerd |
Taal | Nederlands |
Grootte | 21690 MB |