Search this site
Embedded Files
哈里斯數碼學院
  • 最新消息
  • 運算思維
    • Code.org
    • micro:bit
      • 1-1 硬體簡介
      • 1-2 變數體驗
      • 2-1 音樂模組
      • 3-1外接Led
      • 3-2 RGB Led燈控制
      • 3-3 紅外線感測器
      • 3-5 伺服馬達
      • 3-4 超聲波感測
    • MicroPython
    • Python
      • 介面環境
      • 基本運算
      • 海龜程式
    • 電腦流程圖
    • Arduino
  • 系統平台
    • 電腦工作原理
  • 資料處理分析
    • 試算表應用
  • 數位應用
    • 人工智慧
    • 影音製作
    • 簡報設計
      • 範例大綱
    • 協作平台實作
    • 資訊專題
  • 資訊社會
    • 資訊安全與倫理
  • 教學小工具
    • 政令宣導
  • 關於哈里斯
    • 推薦網站
哈里斯數碼學院
  • 最新消息
  • 運算思維
    • Code.org
    • micro:bit
      • 1-1 硬體簡介
      • 1-2 變數體驗
      • 2-1 音樂模組
      • 3-1外接Led
      • 3-2 RGB Led燈控制
      • 3-3 紅外線感測器
      • 3-5 伺服馬達
      • 3-4 超聲波感測
    • MicroPython
    • Python
      • 介面環境
      • 基本運算
      • 海龜程式
    • 電腦流程圖
    • Arduino
  • 系統平台
    • 電腦工作原理
  • 資料處理分析
    • 試算表應用
  • 數位應用
    • 人工智慧
    • 影音製作
    • 簡報設計
      • 範例大綱
    • 協作平台實作
    • 資訊專題
  • 資訊社會
    • 資訊安全與倫理
  • 教學小工具
    • 政令宣導
  • 關於哈里斯
    • 推薦網站
  • More
    • 最新消息
    • 運算思維
      • Code.org
      • micro:bit
        • 1-1 硬體簡介
        • 1-2 變數體驗
        • 2-1 音樂模組
        • 3-1外接Led
        • 3-2 RGB Led燈控制
        • 3-3 紅外線感測器
        • 3-5 伺服馬達
        • 3-4 超聲波感測
      • MicroPython
      • Python
        • 介面環境
        • 基本運算
        • 海龜程式
      • 電腦流程圖
      • Arduino
    • 系統平台
      • 電腦工作原理
    • 資料處理分析
      • 試算表應用
    • 數位應用
      • 人工智慧
      • 影音製作
      • 簡報設計
        • 範例大綱
      • 協作平台實作
      • 資訊專題
    • 資訊社會
      • 資訊安全與倫理
    • 教學小工具
      • 政令宣導
    • 關於哈里斯
      • 推薦網站

Micro:bit Python Editor  

1-1 Show vs Scroll

    Show  your name first

inc. delay=100 & loop

e.g.("harrison", delay=100, loop=True)

=====

Show the Images

  display.show(Image.HEART)

=======


1-2  image

loop ( 迴圈 )

loop參數預設值是 False(假),表示僅執行一次而已;loop = True (真),即可使 scroll 函式反覆執行該動作,而各參數之間須加逗號以示區隔,且注意逗號和指令中間要留一個空白以示區隔,且指令大小寫需依規定,例如 TRUE 或 true 都是不可以的。

show裡面string的參數若是文字,前後要加單引號或雙引號,數字不用加引號。


2. LED  control 

    Light brightness control

實作三:

【內置LED燈如何顯示】

from microbit import *

for x in range(5):

    for y in range(5):

        display.set_pixel(x, y, 9)

        sleep(200)


實作1:

將左列程式改另一種顯示模式

實作2:

如果我們不想要填滿整個LED矩陣,而只想讓LED在畫面上移動?

display.clear()

實作3:

將左列程式改成無限迴圈

(while True:)的顯示



3. Music 播放音樂

import music

music.play("C4:4")

music.play("E4:4")

=========

import music 

A = ["C4:4", "E4:4"] 

music.play(A)


想一想輸出結果

from microbit import *

while True:

      sleep(500)

      if button_a.is_pressed():

         display.set_pixel(2, 2, 9)

         sleep(1000)

      else:

         display.set_pixel(2, 2, 0)


想一想輸出結果

from microbit import *

from random import randint

while True:

     val = 9

     x = randint(0, 4)

     y = randint(0,4)

     display.set_pixel(x, y, val)

     while (val > 0):

         display.set_pixel(x, y, val)

         sleep(500)

         val = val-1

         display.set_pixel(x, y, 0)



播放歌曲

若要播放音效,需先匯人 music 元件庫,「music.BIRTHDAY」 是内建的音效,其名稱均為大寫。

  • DADADADUM.

  • ENTERTAINER.

  • PRELUDE.

  • ODE.

  • NYAN.

  • RINGTONE.

  • FUNK.

  • BLUES.

下列程式的輸出結果⋯⋯


from microbit import *

import music

while True:

    music.pitch(accelerometer.get_y(), 10)


 If else 指令


4. button (按鈕輸入)

下列程式的輸出結果⋯⋯

from microbit import *

 

while True:

    if button_a.was_pressed():

        answer = random.randrange(3)

        display.show(answer)

is_pressed()在於:除非您在那個精確時刻按下按鈕,否則您將無法檢測該按鈕是否被按下。


下列程式的輸出結果⋯⋯


from microbit import *

import random

 

while True:

    if button_a.was_pressed():

        display.show(str(random.randint(1, 6)))


5. count button (計算按鍵次數)

get 代表「得到」presses 代表「按下」意思, get presses 得到的是數值。

若不想捲動顯示結果可將 scroll 改為show,若想反覆執行可以使用while True:

。


6. and  (檢查是否同時按下兩鍵)

如果同時按下兩個按鈕則顯示AB,並跳出迴圈。


Random

如果shake,就亂數產生點數。


from microbit import *

import random 

while True:

    if button_a.was_pressed():

        display.scroll(random.randint(1, 30))

        sleep(1000)

左列程式的執行結果為何?


from microbit import *

import random 


begin = 5

while True:

    if button_a.was_pressed():

        begin += 1

        if begin > 9: 

            begin = 9

    elif button_b.was_pressed():

        begin -= 1

        if begin < 0:

            begin = 0

    display.show(begin)


請將左列調音器程式改方向 (按A鍵變小聲,按B鍵變大聲)?


温度感測

看懂程式碼

加速感測器1

加速感測器2

光線偵測光線偵測

觸摸感測

磁力感測


TIP. Loop


指北針


import speech

speech.say("Hello, My name is Harrison")

文字轉語音

Sound meter

from microbit import *

 

# function to map any range of numbers to another range

def map(value, fromMin, fromMax, toMin, toMax):

    fromRange = fromMax - fromMin

    toRange = toMax - toMin

    valueScaled = float(value - fromMin) / float(fromRange)

    return toMin + (valueScaled * toRange)

 

# set of images for simple bar chart

graph5 = Image("99999:"

               "99999:"

               "99999:"

               "99999:"

               "99999")

 

graph4 = Image("00000:"

               "99999:"

               "99999:"

               "99999:"

               "99999")

 

graph3 = Image("00000:"

               "00000:"

               "99999:"

               "99999:"

               "99999")

 

graph2 = Image("00000:"

               "00000:"

               "00000:"

               "99999:"

               "99999")

 

graph1 = Image("00000:"

               "00000:"

               "00000:"

               "00000:"

               "99999")

               

graph0 = Image("00000:"

               "00000:"

               "00000:"

               "00000:"

               "00000")

allGraphs = [graph0, graph1, graph2, graph3, graph4, graph5]

 

# ignore first sound level reading

soundLevel = microphone.sound_level()

sleep(200)

while True:

    # map sound levels from range 0-255 to range 0-5 for choosing graph image

    soundLevel = int(map(microphone.sound_level(), 0, 255, 0, 5))

    display.show(allGraphs[soundLevel])

 


WS 2812 循環燈


from microbit import *

import neopixel


# 設置WS2812 LED燈條

np = neopixel.NeoPixel(pin0, 24)


# 紅、綠、藍三種顏色

colors = [ (255, 0, 0), (0, 255, 0), (0, 0, 255) ]


# 循環顯示每種顏色

while True:

    for color in colors:

        for i in range(len(np)):

            np[i] = color

        np.show()

        sleep(500)


WS 2812 呼吸燈

from microbit import *

import neopixel

# 設置WS2812 LED燈條

np = neopixel.NeoPixel(pin0, 24)

# 最大亮度

MAX_BRIGHTNESS = 50

 

# 呼吸燈效果

while True:

    for i in range(MAX_BRIGHTNESS):

        for j in range(len(np)):

            np[j] = (i, i, 0)

        np.show()

        sleep(20)

    for i in range(MAX_BRIGHTNESS, 0, -1):

        for j in range(len(np)):

            np[j] = (i, i, 0)

        np.show()

        sleep(20)


WS 2812 流水燈

from microbit import *

import neopixel


# 設置WS2812 LED燈條

np = neopixel.NeoPixel(pin0, 24)


# 紅、橙、黃、綠、青、藍、紫七種顏色

colors = [(255, 0, 0), (255, 127, 0), (255, 255, 0),

          (0, 255, 0), (0, 255, 255), (0, 0, 255), (127, 0, 255)]


# 彩虹效果

while True:

    for i in range(len(colors)):

        for j in range(len(np)):

            np[j] = colors[(i+j) % len(colors)]

        np.show()

        sleep(100)

想一想輸出結果



TBA


HC- SR04 感測



from microbit import *

import utime

 

# Trigger 和 Echo 連接的引腳

TRIG = pin0

ECHO = pin1

 

def distance_measurement():

    TRIG.write_digital(0)

    utime.sleep_us(2)

    TRIG.write_digital(1)

    utime.sleep_us(10)

    TRIG.write_digital(0)

 

    while ECHO.read_digital() == 0:

        pass

    start = utime.ticks_us()

 

    while ECHO.read_digital() == 1:

        pass

    stop = utime.ticks_us()

 

    distance = (stop - start) / 58

    return distance

 

while True:

    dist = distance_measurement()

    if dist < 20:

        display.show(Image.SAD)

        sleep(500)

    else:

        display.clear()

    sleep(100)


© 2021-2023 Harrison, Lin
Report abuse
Page details
Page updated
Report abuse