File tree Expand file tree Collapse file tree 1 file changed +60
-9
lines changed
ports/esp32/boards/KidBright32/modules Expand file tree Collapse file tree 1 file changed +60
-9
lines changed Original file line number Diff line number Diff line change 3
3
from machine import Pin , PWM
4
4
from time import sleep
5
5
6
- buzzer = PWM (Pin (13 ), freq = 2000 , duty = 0 )
6
+ note_map = {
7
+ "C4" : 261 ,
8
+ "C#4" : 277 ,
9
+ "D4" : 293 ,
10
+ "Eb4" : 311 ,
11
+ "E4" : 329 ,
12
+ "F4" : 349 ,
13
+ "F#4" : 369 ,
14
+ "G4" : 391 ,
15
+ "G#4" : 415 ,
16
+ "A4" : 440 ,
17
+ "Bb4" : 466 ,
18
+ "B4" : 493 ,
19
+ "C5" : 523 ,
20
+ "C#5" : 554 ,
21
+ "D5" : 587 ,
22
+ "Eb5" : 622 ,
23
+ "E5" : 659 ,
24
+ "F5" : 698 ,
25
+ "F#5" : 740 ,
26
+ "G5" : 784 ,
27
+ "G#5" : 831 ,
28
+ "A5" : 880 ,
29
+ "Bb5" : 932 ,
30
+ "B5" : 988 ,
31
+ "C6" : 1046 ,
32
+ "C#6" : 1109 ,
33
+ "D6" : 1175 ,
34
+ "Eb6" : 1244 ,
35
+ "E6" : 1318 ,
36
+ "F6" : 1396 ,
37
+ "F#6" : 1480 ,
38
+ "G6" : 1568 ,
39
+ "G#6" : 1661 ,
40
+ "A6" : 1760 ,
41
+ "Bb6" : 1865 ,
42
+ "B6" : 1976 ,
43
+ "C7" : 2093 ,
44
+ "SIL" : 0
45
+ }
7
46
8
- def tone (freq = 2093 , duration = 0.5 , duty = 50 ):
9
- buzzer .freq (freq )
10
- buzzer .duty (duty )
47
+ __buzzer = PWM (Pin (13 ), freq = 2000 , duty = 0 )
48
+ volume = 50
49
+ bpm = 120
50
+
51
+ def tone (freq = 2093 , duration = 0.5 ):
52
+ __buzzer .freq (int (freq ))
53
+ __buzzer .duty (int (volume / 100 * 512 ))
11
54
sleep (duration )
12
- buzzer .duty (0 )
55
+ __buzzer .duty (0 )
13
56
14
- def on (freq = 2000 , duty = 50 ):
15
- buzzer .freq (freq )
16
- buzzer .duty (duty )
57
+ def on (freq = 2000 ):
58
+ __buzzer .freq (int ( freq ) )
59
+ __buzzer .duty (int ( volume / 100 * 512 ) )
17
60
18
61
def off ():
19
- buzzer .duty (0 )
62
+ __buzzer .duty (0 )
63
+
64
+ def note (notes , duration = 4 ):
65
+ quarter_delay = (60 * 1000 ) / bpm
66
+ delay = quarter_delay * duration
67
+ delay = delay / 1000 # mS -> S
68
+ for note in notes .split (" " ):
69
+ tone (note_map [note ], delay )
70
+
You can’t perform that action at this time.
0 commit comments