# Is our classroom too loud?: a free micro:bit lesson from Maze Education
# https://maze.education/microbit/lessons/classroom-sound-meter
# MicroPython for the micro:bit Python Editor: https://python.microbit.org

# Is our classroom too loud? A sound level bar, and a sad face above 128 (micro:bit V2).
from microbit import *

while True:
    level = microphone.sound_level()
    if level > 128:
        display.show(Image.SAD)
        sleep(600)
    else:
        rows = level * 5 // 128
        for y in range(5):
            for x in range(5):
                if y >= 5 - rows:
                    display.set_pixel(x, y, 9)
                else:
                    display.set_pixel(x, y, 0)
    sleep(100)
