Slide 41
Slide 41 text
ୈ 4 ষ micro:bit ͷ͓͠Ζ׆༻ߟࠪ 4.2 ଂ Beep ԻεϐʔΧʔͰϝϩσΟϓϨΠϠʔ
• write_analog ͰԻྔΛ্͛Լ͛ͯ͠ٳූ
Ϧετ 4.1: ͖Β͖Β playe@Pythonr
1: from microbit import *
2: while True:
3: if button_a.is_pressed():
4: pin = pin_speaker
5: print("pin_speaker")
6: speed = 1000/4
7: power = 25
8: rate = 440*4
9: stp = 0
10: c,d,e,f,g,a,b,v = 0,2,4,5,7,9,11,99 // 99 ٳූ
11: musicstep = [c,c,g,g,a,a,g,v,f,f,e,e,d,d,c,v]
12: while len(musicstep)>stp:
13: note = int(2**((12-musicstep[stp]) / 12) * rate)
14: if musicstep[stp] is not v:
15: pin.write_analog(power)
16: pin.set_analog_period_microseconds(note)
17: print("({}){}".format(stp,note))
18: sleep(int(speed*0.9))
19: pin.write_analog(0)
20: sleep(int(speed*0.1))
21: stp+=1
22: if button_b.is_pressed(): break
23: print("stop")
24: pin.write_analog(0)
25:
[YouTube] https://youtu.be/qzfX0eibEKQ
34