From c76c58f0eddb427dcff1a132e2fe7deff3f4b3fd Mon Sep 17 00:00:00 2001 From: Dave Briccetti Date: Sun, 7 Jul 2019 13:22:01 -0700 Subject: [PATCH 1/2] Create a more advanced version of the light sensor -> circle of LEDs program. --- ...aygroundExpress_LightSensor_Four_Colors.py | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 Introducing_CircuitPlaygroundExpress/CircuitPlaygroundExpress_LightSensor_Four_Colors.py diff --git a/Introducing_CircuitPlaygroundExpress/CircuitPlaygroundExpress_LightSensor_Four_Colors.py b/Introducing_CircuitPlaygroundExpress/CircuitPlaygroundExpress_LightSensor_Four_Colors.py new file mode 100644 index 000000000..ace24b4de --- /dev/null +++ b/Introducing_CircuitPlaygroundExpress/CircuitPlaygroundExpress_LightSensor_Four_Colors.py @@ -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[led_idx] = hues[hue_idx] + + pixels.show() + + time.sleep(0.01) From ab3d91776b784fb045d99558bd17980f40208c40 Mon Sep 17 00:00:00 2001 From: Dave Briccetti Date: Sat, 20 Jul 2019 13:43:27 -0700 Subject: [PATCH 2/2] Make the ring of lights increase clockwise, which seems more natural --- .../CircuitPlaygroundExpress_LightSensor_Four_Colors.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Introducing_CircuitPlaygroundExpress/CircuitPlaygroundExpress_LightSensor_Four_Colors.py b/Introducing_CircuitPlaygroundExpress/CircuitPlaygroundExpress_LightSensor_Four_Colors.py index ace24b4de..0e60c050b 100644 --- a/Introducing_CircuitPlaygroundExpress/CircuitPlaygroundExpress_LightSensor_Four_Colors.py +++ b/Introducing_CircuitPlaygroundExpress/CircuitPlaygroundExpress_LightSensor_Four_Colors.py @@ -33,7 +33,7 @@ pixels.fill((0, 0, 0)) for led_idx in range(int(hue_fill_fraction * NUM_LEDS) + 1): - pixels[led_idx] = hues[hue_idx] + pixels[9 - led_idx] = hues[hue_idx] pixels.show()