Skip to content

活動

Get up!

初學者 | MakeCode, Python | LED 螢幕, 光感應器, 聲音 | 感應器, 數字和位置值, 迭代, 選擇

步驟1:製作

它是什麼?

Turn your micro:bit into a light-sensitive alarm to help you get up in the morning. You'll learn how to use infinite loops, and selection with comparison logic.

這兩支影片為您示範了製作內容和編碼方法:

運作方式

  • The program uses the micro:bit’s LED display as a light sensor.
  • Selection ('if... else') and comparison logic (> 70) are used so that if the light level is above a certain amount, a sun icon appears and it makes a noise; else, it clears the screen.
  • If the light level is greater than (>) 70, a sun appears on the LED display, telling you it’s time to get up.
  • The program uses an infinite loop to keep sensing the light level.
  • You can adjust the threshold at which the sun appears. Use larger numbers to make it less sensitive to light, so the alarm only goes off when it's brighter - or smaller numbers to make it more sensitive to light.

你需要的東西

  • micro:bit (or use the simulator in the online editors)
  • MakeCode 或, Python 編輯器
  • battery pack (optional but recommended)

步驟2:編碼

1from microbit import *
2import music
3
4while True:
5    if display.read_light_level() > 70:
6        display.show(Image(
7        "90909:"
8        "09990:"
9        "99999:"
10        "09990:"
11        "90909"))
12        music.play(music.DADADADUM)    
13    else:
14        display.clear()
15        

步驟3:進階

  • Change the alarm sound.
  • Replace the sun icon with motivational messages that can help you to start your day.