Skip to content

활동

Cookie tin alarm

초급 | MakeCode, Python | LED, 빛 센서 | 선택 실행, 센서, 수 & 위치 값

1단계: 만들어 보세요.

프로젝트 소개

Has anyone been in your cookie tin? You can program your BBC micro:bit to act as an alarm to let you know! You will learn about selection, comparison logic, and the micro:bit's light sensor.

다음 동영상을 통해 만들고 프로그래밍하는 방법을 살펴보세요.:

설명

  • The program turns the micro:bit’s LED display into a light sensor.
  • Put your micro:bit into the tin you wish to protect. When the lid is opened, the light sensor detects a change in light level and triggers the alarm.
  • The program uses an infinite loop to keep sensing the light level.
  • Selection ('if... else') and comparison logic (> 30) are used so that if the light level is above a certain amount, an angry face appears on the display; else, it clears the screen.

준비물

  • micro:bit (또는 MakeCode 시뮬레이터)
  • MakeCode 편집기
  • battery pack

2단계: 프로그래밍 해보세요.

1from microbit import *
2
3while True:
4    if display.read_light_level() > 30:
5        display.show(Image.ANGRY)
6    else:
7        display.clear()
8

3단계: 더 좋게 만들어 보세요.

  • Adjust the program, removing the "else" part of the comparison, so that the angry face remains on the micro:bit’s display whatever the light level so that you can tell someone has opened your tin.
  • Add an audible alarm using blocks from the music section of the MakeCode editor.
  • Count how many times your tin has been opened by introducing a variable into the program.
  • If you have two micro:bits, use the radio function to send you a signal as soon as your tin has been opened. Take a look at the light alarm project to find out how to do this.