# Data logger: a free micro:bit lesson from Maze Education
# https://maze.education/microbit/lessons/data-logger
# MicroPython for the micro:bit Python Editor: https://python.microbit.org

# Data logger: A starts logging temperature, light and sound every ten seconds, B stops it (micro:bit V2).
from microbit import *
import log

log.set_labels("temperature", "light", "sound", timestamp=log.SECONDS)
logging = False


@run_every(s=10)
def record():
    if logging:
        log.add({
            "temperature": temperature(),
            "light": display.read_light_level(),
            "sound": microphone.sound_level()
        })


while True:
    if button_a.was_pressed():
        logging = True
        display.show(Image.YES)
    if button_b.was_pressed():
        logging = False
        display.show(Image.NO)
    sleep(100)
