### lab3-1 ### >>> import os # 匯入 os 模組 (os 模組提供了數十個與作業系統溝通的函式) >>> os.listdir() # listdir() 可查看裝置內的檔案 ### lab3-2 from hcsr04 import HCSR04 # 匯入函式庫 HCSR04 sonar = HCSR04(trigger_pin = 14, echo_pin = 12) # 建立超音波模組物件 distance = sonar.distance_cm() # 呼叫 sonar.distance_cm() 方法,讀取偵測到的障礙物距離 print(distance) # 電腦螢幕上顯示 ### lab3-3 from machine import Pin, I2C from ssd1306 import SSD1306_I2C from hcsr04 import HCSR04 import utime oled = SSD1306_I2C(128, 64, I2C(scl=Pin(5), sda=Pin(4))) # 建立 OLED 模組物件 sonar = HCSR04(trigger_pin = 14, echo_pin = 12) while True: distance = sonar.distance_cm() oled.fill(0) # 清空 OLED 畫面 oled.text("Distance:", 0, 0) oled.text(str(distance) + " cm", 0, 16) # 和 OLED 上一行字,相隔一行 oled.show() # 顯示 OLED 畫面 print("偵測距離: " + str(distance) + " 公分") # 同步在電腦螢幕上顯示 utime.sleep_ms(25) # 暫停 25ms (毫秒) ### lab3-4 from machine import Pin, PWM import utime buzzer = PWM(Pin(15, Pin.OUT), freq = 0, duty = 512) # 建立 PWM 模組物件 buzzer.freq(261) # 頻率設為 261Hz --> 中音 C (Do) utime.sleep_ms(1000) # 讓聲音持續 1000ms buzzer.deinit() # 結束,關閉 PWM (完全關閉蜂鳴器) ### lab3-5 from machine import Pin, I2C, PWM from ssd1306 import SSD1306_I2C from hcsr04 import HCSR04 import utime alarm_distance = 10 # 觸發警報距離 oled = SSD1306_I2C(128, 64, I2C(scl = Pin(5), sda = Pin(4))) # 建立 OLED 模組物件 sonar = HCSR04(trigger_pin = 14, echo_pin = 12) # 建立超音波模組物件 buzzer = PWM(Pin(15, Pin.OUT), freq = 880, duty = 0) # 建立 PWM 模組物件 while True: distance = sonar.distance_cm() oled.fill(0) oled.text("Distance:", 0, 0) oled.text(str(distance) + " cm", 0, 16) if 2 <= distance <= alarm_distance: buzzer.duty(512) oled.text("!!! ALARM !!!", 0, 40) # 在 OLED 顯示警報訊息 print("!!! 觸發警報 !!!") else: buzzer.duty(0) print("無警報") oled.show() utime.sleep_ms(25) ### lab3-6 from machine import Pin, I2C, PWM from ssd1306 import SSD1306_I2C from hcsr04 import HCSR04 import utime oled = SSD1306_I2C(128, 64, I2C(scl=Pin(5), sda=Pin(4))) sonar = HCSR04(trigger_pin = 14, echo_pin = 12) buzzer = PWM(Pin(15, Pin.OUT), freq = 784, duty = 0) sound_delay = 500 buzzer_time = utime.ticks_ms() while True: distance = sonar.distance_cm() oled.fill(0) oled.text("Distance:", 0, 0) oled.text(str(distance) + " cm", 0, 16) oled.show() print("偵測距離: " + str(distance) + " 公分") if 2 <= distance <= 50: buzzer.duty(512) utime.sleep_ms(10) buzzer.duty(0) sound_delay = int(distance) * 10 - 10 else: sound_delay = 500 utime.sleep_ms(sound_delay) ### lab3-7 from machine import Pin, I2C, PWM from ssd1306 import SSD1306_I2C from hcsr04 import HCSR04 import utime note_min = 220 # 最小頻率 (Hz) note_max = 880 # 最大頻率 (Hz) dist_min = 40 # 測距最小距離 (公厘) dist_max = dist_min + (note_max - note_min) # 測距最大距離 (公厘) oled = SSD1306_I2C(128, 64, I2C(scl = Pin(5), sda = Pin(4))) sonar = HCSR04(trigger_pin = 14, echo_pin = 12) buzzer = PWM(Pin(15, Pin.OUT), freq = 0, duty = 0) while True: distance = sonar.distance_mm() if dist_min <= distance <= dist_max: key = note_min + (distance - dist_min) buzzer.freq(key) buzzer.duty(512) else: key = 0 buzzer.duty(0) oled.fill(0) oled.text("Dist: " + str(distance) + " mm", 0, 0) oled.text("Note: " + str(key) + " Hz", 0, 16) oled.show() print("偵測距離: " + str(distance) + " 公厘, 播放頻率: " + str(key) + " 赫茲") utime.sleep_ms(10)