### ex4-1 ### from machine import Pin, PWM import time buzzer = PWM(Pin(5, Pin.OUT)) buzzer.freq(392) buzzer.duty(512) time.sleep(2) buzzer.duty(0) buzzer.freq(0) ### ex4-2 (Lab06.py) ### from machine import Pin, PWM import time buzzer = PWM(Pin(5)) buzzer.duty(512) buzzer.freq(392) time.sleep(.5) buzzer.freq(330) time.sleep(.5) buzzer.freq(330) time.sleep(1.) buzzer.freq(349) time.sleep(.5) buzzer.freq(294) time.sleep(.5) buzzer.freq(294) time.sleep(1.) buzzer.freq(261) time.sleep(.5) buzzer.freq(294) time.sleep(.5) buzzer.freq(330) time.sleep(.5) buzzer.freq(349) time.sleep(.5) buzzer.freq(392) time.sleep(.5) buzzer.freq(392) time.sleep(.5) buzzer.freq(392) time.sleep(.5) buzzer.duty(0) # 關閉蜂鳴器 ### ex4-3 (加入休止符的 Lab06.py) ### from machine import Pin, PWM import time buzzer = PWM(Pin(5)) buzzer.duty(512) buzzer.freq(392) time.sleep(.5) buzzer.freq(330) time.sleep(.5) buzzer.freq(330) time.sleep(.5) buzzer.duty(0) time.sleep(.5) buzzer.duty(512) buzzer.freq(349) time.sleep(.5) buzzer.freq(294) time.sleep(.5) buzzer.freq(294) time.sleep(.5) buzzer.duty(0) time.sleep(.5) buzzer.duty(512) buzzer.freq(261) time.sleep(.5) buzzer.freq(294) time.sleep(.5) buzzer.freq(330) time.sleep(.5) buzzer.freq(349) time.sleep(.5) buzzer.freq(392) time.sleep(.5) buzzer.freq(392) time.sleep(.5) buzzer.freq(392) time.sleep(.5) buzzer.duty(0) # 關閉蜂鳴器 ### ex4-4 (使用 for 迴圈與串列容器改寫 Lab06.py) ### from machine import Pin, PWM import time buzzer = PWM(Pin(5)) #|5 3 3 -|4 2 2 -|1 2 3 4 5 5 5|,0 表示休止符 notes = [ 392, 330, 330, 0, 349, 294, 294, 0, 262, 294, 330, 349, 392, 392, 392, 0] for note in notes: # 一一取出音符 if note == 0: # 休止符不發音 buzzer.duty(0) else: buzzer.duty(512) # 設定為一半音量 buzzer.freq(note) # 依照音符設定頻率 time.sleep(0.2) # 讓聲音持續 02秒 buzzer.duty(0) # 停止發聲 time.sleep(0.1) # 持續無聲 01 秒 ### ex4-5 (使用 for 迴圈與 PWN 設計呼吸燈) ### from machine import Pin, PWM import time led = PWM(Pin(2)) while True: for i in range(0, 1024, 10): # range() 會產生 0 到 1024 但不包含 1024,間隔為 10 的數列 led.duty(i) # 設定 PWM 輸出值控制 LED 亮度 time.sleep(0.01) ### ex4-6 ### from machine import Pin, PWM from rtttl import RTTTL import time buzzer = PWM(Pin(5)) buzzer.duty(0) def play_tone(freq, msec): if freq > 0: buzzer.freq(freq) buzzer.duty(512) time.sleep(msec * 0.001) buzzer.duty(0) time.sleep(0.05) print('play music') tune = RTTTL("winner:d=4, o=5, b=140:16e6, 16e6, 32p, 8e6, 16c6, 8e6, 8g6, 8p, 8g, 8p, 8c6, 16p, 8g, 16p, 8e, 16p, 8a, 8b, 2e6") for freq, msec in tune.notes(): play_tone(freq, msec) buzzer.duty(0) ### ex4-7 ### # Teachable Machine 官網 https://teachablemachine.withgoogle.com/train ### ex4-8 (Lab07.py) ### import network from machine import Pin, PWM from time import sleep_ms from umqtt.robust import MQTTClient buzzer = PWM(Pin(5)) buzzer.freq(400) # 設定蜂鳴器發聲頻率 buzzer_on = 0 # 建立變數作為警報狀態 sta_if = network.WLAN(network.STA_IF) sta_if.active(True) sta_if.connect("無線網路名稱", "無線網路密碼") while not sta_if.isconnected(): pass print("控制板已連線") # 建立 MQTT 客戶端物件 client = MQTTClient( client_id = "D1mini", server = "io.adafruit.com", user = "AIO 帳號", password = "AIO 金鑰", ssl = False) # 註冊收到訂閱資料時的處理函式 def get_cmd(topic, msg): global buzzer_on print(topic, msg) if msg == b"100": buzzer_on = 1 print('NO MASK!') else: buzzer_on = 0 print('CLEAR.') # 蜂鳴器發聲函式 def buzz(): buzzer.duty(512) sleep_ms(200) buzzer.duty(0) sleep_ms(200) client.connect() client.set_callback(get_cmd) client.subscribe(client.user.encode() + b"/feeds/mask"); while True: client.check_msg() if buzzer_on : # 判斷狀態是否需要發聲 buzz() ### ex4-9 ### https://flagtech.github.io/FM633A/lab07.html ### ex4-10 (Lab08.py) ### import network, time from machine import Pin, PWM from umqtt.robust import MQTTClient from rtttl import RTTTL buzzer = PWM(Pin(5)) buzzer.duty(0) def play_tone(freq, msec): if freq > 0: buzzer.freq(freq) buzzer.duty(512) time.sleep(msec * 0.001) buzzer.duty(0) time.sleep(0.05) sta_if = network.WLAN(network.STA_IF) sta_if.active(True) sta_if.connect("無線網路基地台", "無線網路密碼") while not sta_if.isconnected(): pass print("控制板已連線") client = MQTTClient( client_id = "D1mini", server = "io.adafruit.com", user = "AIO 帳號", password = "AIO 金鑰", ssl = False) # 註冊收到訂閱資料時的處理函式 def get_cmd(topic, msg): print(topic, msg) if msg == b"100": print('play music') tune = RTTTL("winner:d=4, o=5, b=140:16e6, 16e6, 32p, 8e6, 16c6, 8e6, 8g6, 8p, 8g, 8p, 8c6, 16p, 8g, 16p, 8e, 16p, 8a, 8b, 2e6") for freq, msec in tune.notes(): play_tone(freq, msec) buzzer.duty(0) client.connect() client.set_callback(get_cmd) client.subscribe(client.user.encode() + b"/feeds/cardgame"); while True: client.check_msg() ### ex4-11 ### https://flagtech.github.io/FM633A/lab08.html ### ex4-12 ### # IFTTT https://ifttt.com/ ### ex4-13 ### https://flagtech.github.io/FM633A/lab09.html