Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create a more advanced version of the light sensor -> circle of LEDs program #749

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
"""Maps light level to multiple circles of colors.

The light intensity level is divided into four bands.
Dim light creates a growing circle of blue lights,
followed by circles of green, yellow, and then red lights
for the highest intensity."""

import time
import board
import neopixel
from analogio import AnalogIn

NUM_LEDS = 10

pixels = neopixel.NeoPixel(board.NEOPIXEL, NUM_LEDS, auto_write=0, brightness=.3)
pixels.fill((0, 0, 0))
pixels.show()

sensor = AnalogIn(board.LIGHT)
hues = (
( 0, 0, 255), # Blue
( 0, 255, 0), # Green
(255, 255, 0), # Yellow
(255, 0, 0), # Red
)

while True:
normalized_brightness = sensor.value / 65535
hues_position = normalized_brightness * len(hues)
hue_idx = int(hues_position)
hue_fill_fraction = hues_position - hue_idx

pixels.fill((0, 0, 0))

for led_idx in range(int(hue_fill_fraction * NUM_LEDS) + 1):
pixels[9 - led_idx] = hues[hue_idx]

pixels.show()

time.sleep(0.01)