Microbit Python 課程介紹 – MicroPython Input/Output

2017-06-23 07:54

Microbit Python 課程介紹

Micro:bit MicroPython Input/Output

Microbit Shop

在BBC micro:bit 的底下,有直條的金屬條文,看起來像牙齒,我們一般稱之為Golden Finger (金手指) 。他們是 input/output pins (or I/O pins for short)。

有些pins 比較大,這就方便可以用鱷魚夾去夾住它!  這裡有標示0, 1, 2, 3V 和 GND (computers always start counting from zero,接地). 如果,你是使用金手指插槽,就可以用線去接較小的Pins !

在BBC micro:bit ,我們是用物件 object  叫 pinN ,這裡的 N  是代表pin 的編號 ,舉例,pin 0 (zero),我們便使用物件 pin0.

簡單吧!

這些物件會因為pin 的能力而有不同的方法來使用。

Ticklish Python

最簡單例子是去確定這些 pins 是不是有被接觸到。 我們就來替你的裝置來搔個癢讓它變個笑臉:

 

from microbit import *

 

while True:

    if pin0.is_touched():

        display.show(Image.HAPPY)

    else:

        display.show(Image.SAD)

一隻手接觸 GND pin,另一手去輕輕接觸the 0 (zero) pin,你就會看到螢幕變成笑臉。

這是很基本的輸入量測(basic input measurement),你可以開始動手去接觸及其他的pins 。

Bleeps and Bloops

最簡單的輸出裝置就是蜂鳴器,我們可以使用它玩一些聲音輸出有關的程式。.

我們用 BBC micro:bit 的 pin 0 and GND 去跟蜂鳴器對接( 就像下圖).

 

我們將pin 0 接到蜂鳴器的正極,GND 接到負極!

以下的程式便可以產生出聲音出來:

from microbit import *

 

pin0.write_digital(1)

你大慨覺得有趣只有幾秒鐘,就想要讓這聲音停止。讓我們來改進一下這程式:

from microbit import *

 

while True:

    pin0.write_digital(1)

    sleep(20)

    pin0.write_digital(0)

    sleep(480)

以下的英文就是在描述這段程式的工作。不熟英文的朋友就練習一下! 蠻直覺的!

Can you work out how this script works? Remember that 1 is “on” and 0 is “off” in the digital world.

The device is put into an infinite loop and immediately switches pin 0 on. This causes the buzzer to emit a beep. While the buzzer is beeping, the device sleeps for twenty milliseconds and then switches pin 0 off. This gives the effect of a short bleep. Finally, the device sleeps for 480 milliseconds before looping back and starting all over again. This means you’ll get two bleeps per second (one every 500 milliseconds).

We’ve made a very simple metronome! (可以用這段程式改個節拍器出來)

Microbit 台灣 商店購買

Microbit 中文 課程 : Python , Javascript, 物聯網

              中國

—————

返回