|
| 1 | +# SPDX-FileCopyrightText: 2021 Kattni Rembor for Adafruit Industries |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: MIT |
| 4 | + |
| 5 | +""" |
| 6 | +Circuit Playground Bluefruit NeoPixel Sound Meter |
| 7 | +
|
| 8 | +Talk or make noise close to your Circuit Playground Bluefruit to see the NeoPixels light up. |
| 9 | +""" |
| 10 | +from adafruit_circuitplayground import cp |
| 11 | + |
| 12 | +# Choose a color. Defaults to red. This is an RGB value, where (r, g, b) represents red, green, |
| 13 | +# and blue. Each value has a range of 0-255, where 0 is off and 255 is max intensity. You can |
| 14 | +# update these values to change the colors. For example, (0, 255, 0) would be max green. You can |
| 15 | +# combine numbers within the range to make other colors such as (255, 0, 180) being pink. |
| 16 | +# Try it out! |
| 17 | +color_value = (255, 0, 0) |
| 18 | + |
| 19 | +# This is the sound level needed to light up all 10 NeoPixels. If all the LEDs are lighting up too |
| 20 | +# easily, increase this value to make it more difficult to reach the max. If you are only able to |
| 21 | +# light up a few LEDs, decrease this value to make it easier to reach the max. Full possible sound |
| 22 | +# range is 0 - 65535. |
| 23 | +sound_max = 1500 |
| 24 | + |
| 25 | +cp.pixels.auto_write = False |
| 26 | +cp.pixels.brightness = 0.3 |
| 27 | + |
| 28 | + |
| 29 | +def scale_range(value): |
| 30 | + """Scale a value from 0-sound_max (chosen sound range) to 0-9 (NeoPixel range). |
| 31 | + Allows remapping sound value to pixel position. |
| 32 | + Full sound range is 0 - 65535. sound_max should be chosen based on testing.""" |
| 33 | + return round(value / sound_max * 9) |
| 34 | + |
| 35 | + |
| 36 | +while True: |
| 37 | + peak = scale_range(cp.sound_level) |
| 38 | + |
| 39 | + for pixel in range(10): |
| 40 | + if pixel <= peak: |
| 41 | + cp.pixels[pixel] = color_value |
| 42 | + else: |
| 43 | + cp.pixels[pixel] = (0, 0, 0) # Off |
| 44 | + cp.pixels.show() |
0 commit comments