---
# System prepended metadata

title: Untitled
tags: [大安高工, Code]

---

###### tags: `大安高工` `Code`
import pigpio
import time

pwm_pin = 18
pi = pigpio.pi()

melody = [
330, 330, 330, 262, 330, 392, 196, 262, 196, 165, 220, 247, 233, 220, 196, 330, 392, 
440, 349, 392, 330, 262, 294, 247, 262, 196, 165, 220, 247, 233, 220, 196, 330, 392,
440, 349, 392, 330, 262, 294, 247, 392, 370, 330, 311, 330, 208, 220, 262, 220, 262,
294, 392, 370, 330, 311, 330, 523, 523, 523, 392, 370, 330, 311, 330, 208, 220, 262,
220, 262, 294, 311, 294, 262, 262, 262, 262, 262, 294, 330, 262, 220, 196, 262, 262,
262, 262, 294, 330, 262, 262, 262, 262, 294, 330, 262, 220, 196]
 
noteDurations = [
8,4,4,8,4,2,2,
3,3,3,4,4,8,4,8,8,8,4,8,4,3,8,8,3,
3,3,3,4,4,8,4,8,8,8,4,8,4,3,8,8,2,
8,8,8,4,4,8,8,4,8,8,3,8,8,8,4,4,4,8,2,
8,8,8,4,4,8,8,4,8,8,3,3,3,1,
8,4,4,8,4,8,4,8,2,8,4,4,8,4,1,
8,4,4,8,4,8,4,8,2
]

try:
    for i in range(0,len(melody),1): 
        noteDuration = 800/noteDurations[i]
        pi.hardware_PWM(pwm_pin, melody[i]*2, 500000)
        print(i)
        time.sleep(int(noteDuration * 1.30)/1000)
        print(i)
except KeyboardInterrupt:
    print('關閉程式')
finally:
    pi.set_mode(pwm_pin,pigpio.INPUT)


