### ex3-1 ### >>> from servo import Servo >>> from machine import Pin >>> my_servo = Servo(Pin(22)) # 建立伺服馬達物件 >>> my_servo.write_angle(90) # 轉至 90 度 >>> my_servo.write_angle(0) # 轉至 0 度 ### ex3-2 (LAB02.py) ### from servo import Servo from machine import Pin import time my_servo = Servo(Pin(22)) my_servo.write_angle(0) time.sleep(1) my_servo.write_angle(90) time.sleep(1) ### ex3-3 ### >>> from ble_uart import BLE_UART >>> ble = BLE_UART("door_lock") ### ex3-4 ### # iPhone 使用者請參考: https://hackmd.io/@flagmaker/ry6ST-Get ### ex3-5 (LAB03.py) ### from ble_uart import BLE_UART from servo import Servo from machine import Pin my_servo = Servo(Pin(22)) ble = BLE_UART("你的藍牙 ID") while True: getValue = ble.get() getValue = getValue.lower() # 將取得的英文字母都更改為小寫 if(getValue == "open"): my_servo.write_angle(0) # 轉至 0 度 print("開啟") if(getValue == "close"): my_servo.write_angle(90) # 轉至 90 度 print("關閉") ### ex3-6 ### >>> from machine import Pin, ADC >>> adc_pin = Pin(32) >>> adc = ADC(adc_pin) # 建立 ADC 物件 >>> adc.width(ADC.WIDTH_12BIT) # 設定 ADC 解析度為 12 bits >>> adc.atten(ADC.ATTN_11DB) # 設定最高感測電壓為 3.6V >>> adc.read() # 讀取 ADC 值 ### ex3-7 (LAB04.py) ### from machine import Pin, ADC import time adc_pin=Pin(32) adc = ADC(adc_pin) # 建立 ADC 物件 adc.width(ADC.WIDTH_12BIT) # 設定 ADC 解析度。12BIT代表範圍是 0 ~ 4095 adc.atten(ADC.ATTN_11DB) # 將最大感測電壓設定成 3.6V,超過 3.6V 時會得到 ADC 最大值 4095 while True: vol = (adc.read()/4095) * 3.6 tem = (vol - 0.5) * 100 print("目前溫度:", tem) # 顯示溫度值 time.sleep(1) ### ex3-8 (LAB05.py) ### from machine import Pin, ADC import time from ble_uart import BLE_UART adc_pin=Pin(32) adc = ADC(adc_pin) adc.width(ADC.WIDTH_12BIT) adc.atten(ADC.ATTN_11DB) ble = BLE_UART("temperature") while True: vol = (adc.read()/4095) * 3.6 tem = (vol - 0.5) * 100 print("目前溫度:", tem) ble.send('temperature:'+ str(tem)) # 傳送溫度值 time.sleep(1)