### Lab8-1 (Lab11.py) ### from machine import Pin, PWM import time beeper = PWM(Pin(2, Pin.OUT)) #|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: # 休止符不發音 beeper.duty(0) else: beeper.duty(512) # 設定為一半音量 beeper.freq(note) # 依照音符設定頻率 time.sleep(0.2) # 讓聲音持續 02秒 beeper.duty(0) # 停止發聲 time.sleep(0.1) # 持續無聲 01 秒 ### Lab8-2 ### >>> ages = {"Mary":13, "John":14} >>> ages["Mary"] >>> ages["John"] ### Lab8-3 (Lab12.py) ### from machine import Pin, PWM import time beeper = PWM(Pin(2, Pin.OUT)) # 用 2 號腳位控制蜂鳴器 button1 = Pin(16, Pin.IN) # 用 D0 讀 OUT1 button2 = Pin(14, Pin.IN) # 用 D5 讀 OUT2 button3 = Pin(12, Pin.IN) # 用 D6 讀 OUT3 button4 = Pin(13, Pin.IN) # 用 D7 讀 OUT4 button5 = Pin(15, Pin.IN) # 用 D8 讀 OUT5 button6 = Pin(5, Pin.IN) # 用 D1 讀 OUT6 button7 = Pin(4, Pin.IN) # 用 D2 讀 OUT7 tones = { # 儲存音名與頻率的字典 'c': 262, # Do 'd': 294, # Re 'e': 330, # Mi 'f': 349, # Fa 'g': 392, # So 'a': 440, # La 'b': 494, # Si } while True: # 持續讀取觸控按鈕訊號 if button1.value() == 1: # 按了 1 號按鈕 beeper.duty(512) # 設定一半音量 beeper.freq(tones['c']) # 設定 Do 的頻率 elif button2.value() == 1: # 按了 2 號按鈕 beeper.duty(512) # 設定一半音量 beeper.freq(tones['d']) # 設定 Re 的頻率 elif button3.value() == 1: # 按了 3 號按鈕 beeper.duty(512) # 設定一半音量 beeper.freq(tones['e']) # 設定 Mi 的頻率 elif button4.value() == 1: # 按了 4 號按鈕 beeper.duty(512) # 設定一半音量 beeper.freq(tones['f']) # 設定 Fa 的頻率 elif button5.value() == 1: # 按了 5 號按鈕 beeper.duty(512) # 設定一半音量 beeper.freq(tones['g']) # 設定 So 的頻率 elif button6.value() == 1: # 按了 6 號按鈕 beeper.duty(512) # 設定一半音量 beeper.freq(tones['a']) # 設定 La 的頻率 elif button7.value() == 1: # 按了 7 號按鈕 beeper.duty(512) # 設定一半音量 beeper.freq(tones['b']) # 設定 Si 的頻率 else: # 沒有按鈕 beeper.duty(0) # 設定不發聲 time.sleep(0.05) ### Lab8-4 ### from machine import Pin, PWM import time beeper = PWM(Pin(2, Pin.OUT)) # 用 2 號腳位控制蜂鳴器 buttons = [ Pin(16, Pin.IN), # 用 D0 讀 OUT1 Pin(14, Pin.IN), # 用 D5 讀 OUT2 Pin(12, Pin.IN), # 用 D6 讀 OUT3 Pin(13, Pin.IN), # 用 D7 讀 OUT4 Pin(15, Pin.IN), # 用 D8 讀 OUT5 Pin(5, Pin.IN), # 用 D1 讀 OUT6 Pin(4, Pin.IN), # 用 D2 讀 OUT7 ] tones = [ # 儲存音名頻率的串列 262, # Do 294, # Re 330, # Mi 349, # Fa 392, # So 440, # La 494, # Si ] while True: # 持續讀取觸控按鈕訊號 for i in range(len(buttons)): # 一一取出物件檢查按鈕 if buttons[i].value() == 1: # 如果有按鈕 beeper.duty(512) # 設定一半音量 beeper.freq(tones[i]) # 設定對應音符的音量 break # 不需再檢查其他按鈕 else: # 如果所有按鈕都沒按下 beeper.duty(0) # 設定不發聲 time.sleep(0.05)