### ex2-1 ### 下載與安裝 Thonny https://thonny.org ### ex2-2 ### # 在 Shell 區執行 (測試) 單行敘述 >>> print("Hello World") # 在程式編輯區寫多行敘述 import time from machine import Pin led = Pin(15, Pin.OUT) led.value(1) time.sleep(3) led.value(0) ### ex2-3 ### >>> "abc".upper() # 使用字串物件 "abc" 的 upper() 方法,將字串轉成大寫 >>> "abc".find('b') # find() 方法尋找 'b' 的位置 (從 0 開始) >>> "abc".replace('b', 'z') # replace() 方法將所有的 'b' 取代成 'z' ### ex2-4 ### >>> 111 + 111 # 整數物件相加 >>> "111" + "111" # 字串物件串接 ### ex2-5 ### >>> 111 + "111" # 不同型別的資料相加,發生錯誤 ### ex2-6 ### >>> str(111) + "111" # str() 可轉換物件為字串型別 >>> 111 + int("111") # int() 可轉換物件為整數型別 ### ex2-7 ### >>> 10 + 5.5 # 相加 >>> 10 - 5.5 # 相減 >>> 10 * 5.5 # 相乘 >>> 10 / 5.5 # 除法取商 >>> 10 // 5.5 # 除法取整數商 >>> 10 % 5.5 # 除法取餘數 >>> 4 ** 0.5, 8 ** (1/3) # 指數運算 ### ex2-8 ### >>> x = 1 >>> x = x + 1 >>> x ### ex2-9 ### >>> n1 = 123456789 # 將整數物件 123456789 指派給變數 n1 >>> n2 = 987654321 # 將整數物件 987654321 指派給變數 n2 >>> n1 + n2 # 實際上是 123456789 + 987654321 ### ex2-10 ### pi = 3.1 radius = 2.2 # 使用公式計算圓面積 circle_area = pi * radius * radius print(circle_area) ### ex2-11 ### >>> print("abc") # 顯示字串物件 >>> print("abc".upper()) # 顯示字串物件.方法的執行結果 >>> print(111 + 111) # 顯示整數物件運算的結果 ### ex2-12 ### >>> import time # 匯入時間相關的 time 模組 >>> time.sleep(3) # 執行 time 模組的 sleep() 函式,暫停 3 秒 >>> from time import sleep # 從 time 模組裡匯入 sleep() 函式 >>> sleep(5) # 執行 sleep() 函式,暫停 5 秒 ### ex2-13 ### # 暫停 5 秒後,印出 Hello World! from time import sleep sleep(3) print("Hello World!") ### ex2-14 ### # 下載與安裝 D1 mini 的驅動程式 http://www.wch.cn/downloads/CH341SER_EXE.html ### ex2-15 ### >>> from machine import Pin # 從 machine 模組匯入 Pin 物件 >>> led = Pin(2, Pin.OUT) # 建立 2 號腳位的 Pin 物件,設定為輸出腳位,並命名為 led >>> led.value(0) # 低電位,點亮 LED >>> led.value(1) # 高電位,關閉 LED # 暫停 3 秒 from machine import Pin import time led = Pin(2, Pin.OUT) led.value(1) time.sleep(3) led.value(0) ### ex2-16 (Lab01.py) ### from machine import Pin # 從 machine 模組匯入 Pin 物件 import time # 匯入時間相關的 time 模組 led = Pin(2, Pin.OUT) # 建立 2 號腳位的 Pin 物件,設定為輸出腳位,並命名為 led while True: # 一直重複執行 led.value(0) # 低電位,點亮 LED time.sleep(0.5) # 暫停 0.5 秒 led.value(1) # 高電位,關閉 LED time.sleep(0.5) # 暫停 0.5 秒 ### ex2-17 (Lab02.py) ### from machine import Pin from neopixel import NeoPixel # 從 neopixel 模組匯入控制條燈的 NeoPixel 物件 led_strip = NeoPixel(Pin(4), 15) # 建立 NeoPixel 物件,設定控制為 4 號腳位,燈珠數量為 15,命名為 led_strip led_strip[0] = (0, 0, 255) # 設定第 1 顆燈珠數值 led_strip.write() # 依設定顯示 ### ex2-18 (暴力法控制 RGB LED) ### from machine import Pin from neopixel import NeoPixel import time led_strip = NeoPixel(Pin(4), 15) while True: led_strip[0] = (0, 48, 64) led_strip.write() time.sleep_ms(100) led_strip[0] = (0, 0, 0) led_strip[1] = (0, 48, 64) led_strip.write() time.sleep_ms(100) led_strip[1] = (0, 0, 0) led_strip[2] = (0, 48, 64) led_strip.write() time.sleep_ms(100) led_strip[2] = (0, 0, 0) led_strip[3] = (0, 48, 64) led_strip.write() time.sleep_ms(100) led_strip[3] = (0, 0, 0) led_strip[4] = (0, 48, 64) led_strip.write() time.sleep_ms(100) led_strip[4] = (0, 0, 0) led_strip[5] = (0, 48, 64) led_strip.write() time.sleep_ms(100) led_strip[5] = (0, 0, 0) led_strip[6] = (0, 48, 64) led_strip.write() time.sleep_ms(100) led_strip[6] = (0, 0, 0) led_strip[7] = (0, 48, 64) led_strip.write() time.sleep_ms(100) led_strip[7] = (0, 0, 0) led_strip[8] = (0, 48, 64) led_strip.write() time.sleep_ms(100) led_strip[8] = (0, 0, 0) led_strip[9] = (0, 48, 64) led_strip.write() time.sleep_ms(100) led_strip[9] = (0, 0, 0) led_strip[10] = (0, 48, 64) led_strip.write() time.sleep_ms(100) led_strip[10] = (0, 0, 0) led_strip[11] = (0, 48, 64) led_strip.write() time.sleep_ms(100) led_strip[11] = (0, 0, 0) led_strip[12] = (0, 48, 64) led_strip.write() time.sleep_ms(100) led_strip[12] = (0, 0, 0) led_strip[13] = (0, 48, 64) led_strip.write() time.sleep_ms(100) led_strip[13] = (0, 0, 0) led_strip[14] = (0, 48, 64) led_strip.write() time.sleep_ms(100) led_strip[14] = (0, 0, 0) ### ex2-20 ### >>> for i in range(10): # 產生 0 到 10,但不包含 10 的數列 print(i) >>> for i in range(1, 11): # 產生 1 到 11,但不包含 11 的數列 print(i) >>> for i in range(1, 10, 2): # 產生 1 到 9 的遞增奇數列 print(i) >>> for i in range(9, 0, -2): # 產生 9 到 1 的遞減奇數列 print(i) ### ex2-21 (Lab03.py) ### from machine import Pin # 從 machine 模組匯入 Pin 物件 from neopixel import NeoPixel # 從 neopixel 模組匯入控制條燈的 NeoPixel 物件 import time # 匯入時間相關的 time 模組 led_strip = NeoPixel(Pin(4), 15) # 建立 NeoPixel 物件,設定控制為 4 號腳位,燈珠數量為 15,命名為 led_strip while True: # 一直不斷執行 for i in range(led_strip.n): # for 走訪整數串列,範圍為燈珠數量 led_strip[i] = (0, 48, 64) # 依序設定每一顆燈珠 led_strip.write() # 依設定顯示 time.sleep_ms(100) # 暫停 100 毫秒 led_strip[i] = (0, 0, 0) # 再將當顆燈珠熄滅