Skip to content

活動

小夜燈

初學者 | MakeCode, Python | LED 螢幕, 光感應器 | 感應器, 燈光, 系統 & 控制, 輸入輸出, 迭代, 選擇

步驟1:製作

它是什麼?

A nightlight that lights up your BBC micro:bit’s LED display in the dark.

介紹

編程指引

This project uses the micro:bit’s LEDs as a light sensor input to make a light that switches on automatically when it gets dark.

The micro:bit’s light sensor measures light in a range from 0 (very dark) to 255 (very bright).

運作方式

  • An infinite loop in the code keeps the micro:bit checking light levels.
  • It uses logic to decide whether to turn the LEDs on or off. A conditional instruction (if… then… else) makes the decision to turn the LED lights on or off.
  • If the light level falls below 100, then it lights up the LEDs on the micro:bit’s display. Else (otherwise), it clears the screen to turn the LED lights off.
  • Test it out by covering the display or shining a light on it, and see if the LEDs light up when it’s dark.
  • You may need to change the 100 number depending on the light levels around you. Larger numbers will make the light come on more easily. Smaller numbers will make the light only come on when it’s very dark.

你需要的東西

  • micro:bit (或,MakeCode simulator)
  • MakeCode 或, Python 編輯器
  • 電池組(選配)
  • 光源和覆蓋micro:bit的東西

步驟2:編碼

1from microbit import *
2
3while True:
4    if display.read_light_level() < 100:
5        display.show(Image(
6        "99999:"
7        "99999:"
8        "99999:"
9        "99999:"
10        "99999"))
11    else:
12        display.clear()
13    sleep(2000)
14

步驟3:進階

  • 更改圖像以在天黑時顯示月亮或星星。
  • 153/5000
    將micro:bit附加到您的包包或衣服上,當您步行或騎自行車時將其作為額外的安全燈–您能否讓它閃爍而變得脫穎而出呢?
  • Try this MakeCode project that makes the LED display get lighter and darker depending on the amount of light falling on the micro:bit. 您還能在其他地方看到以這種方式對光做出反應的事物嗎?