# Sound-reactive light show: a free micro:bit lesson from Maze Education
# https://maze.education/microbit/lessons/sound-light-show
# MicroPython for the micro:bit Python Editor: https://python.microbit.org

# Sound-reactive light show: lights sparkle with the sound level, and a clap flashes a diamond (micro:bit V2).
from microbit import *
import random

microphone.set_threshold(SoundEvent.LOUD, 150)
while True:
    if microphone.was_event(SoundEvent.LOUD):
        display.show(Image.DIAMOND)
        sleep(300)
        display.show(Image.DIAMOND_SMALL)
        sleep(300)
        display.clear()
    level = microphone.sound_level()
    display.set_pixel(random.randint(0, 4), random.randint(0, 4), level * 9 // 255)
    display.set_pixel(random.randint(0, 4), random.randint(0, 4), 0)
    sleep(20)
