|
| 1 | +# SPDX-FileCopyrightText: Erin St Blaine and ChatGPT for Adafruit Industries |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: MIT |
| 4 | + |
| 5 | +import time |
| 6 | +import board |
| 7 | +from adafruit_circuitplayground import cp |
| 8 | +import neopixel |
| 9 | + |
| 10 | +# Constants |
| 11 | +NUM_PIXELS_STRIP = 20 |
| 12 | +NUM_PIXELS_FACE = 10 # Number of NeoPixels on the face |
| 13 | +PIXEL_PIN_STRIP = board.A1 |
| 14 | +SOUND_THRESHOLD = 50 # Adjust this threshold based on your environment |
| 15 | + |
| 16 | +# Variables |
| 17 | +DELAY_AFTER_LIGHT_UP = 2.0 |
| 18 | +COLOR = (255, 100, 0) # Warm yellow color |
| 19 | +SPEED = 0.3 # Animation speed (adjust as needed, higher number moves more slowly) |
| 20 | + |
| 21 | +# Initialize NeoPixels on face |
| 22 | +pixels_face = cp.pixels |
| 23 | +pixels_face.brightness = 0.5 |
| 24 | + |
| 25 | +# Initialize NeoPixels on strip (A1) |
| 26 | +pixels_strip = neopixel.NeoPixel(PIXEL_PIN_STRIP, NUM_PIXELS_STRIP, brightness=0.5) |
| 27 | + |
| 28 | +# Main loop |
| 29 | +while True: |
| 30 | + # Read sound level |
| 31 | + sound_level = cp.sound_level |
| 32 | + |
| 33 | + # Debugging: Print sound level to the serial monitor |
| 34 | + print("Sound Level:", sound_level) |
| 35 | + |
| 36 | + # Check if sound threshold is reached |
| 37 | + if sound_level > SOUND_THRESHOLD: |
| 38 | + # Sequentially light up NeoPixels on the face |
| 39 | + for i in range(NUM_PIXELS_FACE): |
| 40 | + if i < len(pixels_face): |
| 41 | + pixels_face[i] = COLOR |
| 42 | + time.sleep(SPEED) # Adjust speed if needed |
| 43 | + pixels_face.show() # Show all pixels at once |
| 44 | + |
| 45 | + # Sequentially light up NeoPixels on strip (A1) |
| 46 | + for i in range(NUM_PIXELS_STRIP): |
| 47 | + if i < len(pixels_strip): |
| 48 | + pixels_strip[i] = COLOR |
| 49 | + time.sleep(SPEED) # Adjust speed if needed |
| 50 | + pixels_strip.show() # Show all pixels at once |
| 51 | + |
| 52 | + # Delay for the specified duration after lighting up all pixels |
| 53 | + time.sleep(DELAY_AFTER_LIGHT_UP) |
| 54 | + |
| 55 | + # Turn off all pixels on strip |
| 56 | + pixels_strip.fill((0, 0, 0)) |
| 57 | + pixels_strip.show() |
| 58 | + |
| 59 | + # Turn off all pixels on face |
| 60 | + pixels_face.fill((0, 0, 0)) |
| 61 | + pixels_face.show() |
| 62 | + |
| 63 | + # Add a delay to avoid rapid detection |
| 64 | + time.sleep(0.1) |
0 commit comments