0% found this document useful (0 votes)
11 views5 pages

Games 1

Games1

Uploaded by

Samah Abu Seedo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views5 pages

Games 1

Games1

Uploaded by

Samah Abu Seedo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Project 03: 演奏音乐

实验简介
小车扩展板上有个喇叭功放元件,它常用来播放音乐,作为一些音乐播放设备的外接扩
音设备。

在本实验中,我们利用喇叭功放元件来播放一首音乐。

元件知识
喇叭功放元件:
* *喇叭功放元件(原理相当于无源蜂鸣器),其内部不带震荡电路,
控制时需要在元件正极输入不同频率的方波,负极接地,从而控制喇叭功放元件响起不同频
率的声音。

实验代码:
1 /*
2 Project 03 Buzzer
3 Buzzer plays music
4 */
5 #define NOTE_B0 31
6 #define NOTE_C1 33
7 #define NOTE_CS1 35
8 #define NOTE_D1 37
9 #define NOTE_DS1 39
10 #define NOTE_E1 41
11 #define NOTE_F1 44
12 #define NOTE_FS1 46
13 #define NOTE_G1 49
14 #define NOTE_GS1 52
15 #define NOTE_A1 55
16 #define NOTE_AS1 58
17 #define NOTE_B1 62
18 #define NOTE_C2 65
19 #define NOTE_CS2 69
20 #define NOTE_D2 73
21 #define NOTE_DS2 78
22 #define NOTE_E2 82
23 #define NOTE_F2 87
24 #define NOTE_FS2 93
25 #define NOTE_G2 98
26 #define NOTE_GS2 104
27 #define NOTE_A2 110
28 #define NOTE_AS2 117
29 #define NOTE_B2 123
30 #define NOTE_C3 131
31 #define NOTE_CS3 139
32 #define NOTE_D3 147
33 #define NOTE_DS3 156
34 #define NOTE_E3 165
35 #define NOTE_F3 175
36 #define NOTE_FS3 185
37 #define NOTE_G3 196
38 #define NOTE_GS3 208
39 #define NOTE_A3 220
40 #define NOTE_AS3 233
41 #define NOTE_B3 247
42 #define NOTE_C4 262
43 #define NOTE_CS4 277
44 #define NOTE_D4 294
45 #define NOTE_DS4 311
46 #define NOTE_E4 330
47 #define NOTE_F4 349
48 #define NOTE_FS4 370
49 #define NOTE_G4 392
50 #define NOTE_GS4 415
51 #define NOTE_A4 440
52 #define NOTE_AS4 466
53 #define NOTE_B4 494
54 #define NOTE_C5 523
55 #define NOTE_CS5 554
56 #define NOTE_D5 587
57 #define NOTE_DS5 622
58 #define NOTE_E5 659
59 #define NOTE_F5 698
60 #define NOTE_FS5 740
61 #define NOTE_G5 784
62 #define NOTE_GS5 831
63 #define NOTE_A5 880
64 #define NOTE_AS5 932
65 #define NOTE_B5 988
66 #define NOTE_C6 1047
67 #define NOTE_CS6 1109
68 #define NOTE_D6 1175
69 #define NOTE_DS6 1245
70 #define NOTE_E6 1319
71 #define NOTE_F6 1397
72 #define NOTE_FS6 1480
73 #define NOTE_G6 1568
74 #define NOTE_GS6 1661
75 #define NOTE_A6 1760
76 #define NOTE_AS6 1865
77 #define NOTE_B6 1976
78 #define NOTE_C7 2093
79 #define NOTE_CS7 2217
80 #define NOTE_D7 2349
81 #define NOTE_DS7 2489
82 #define NOTE_E7 2637
83 #define NOTE_F7 2794
84 #define NOTE_FS7 2960
85 #define NOTE_G7 3136
86 #define NOTE_GS7 3322
87 #define NOTE_A7 3520
88 #define NOTE_AS7 3729
89 #define NOTE_B7 3951
90 #define NOTE_C8 4186
91 #define NOTE_CS8 4435
92 #define NOTE_D8 4699
93 #define NOTE_DS8 4978
94 #define REST 0
95 int tempo=114; // change this to make the song slower or faster
96 int buzzer = 3;// initializes digital I/O PIN to control the
buzzer
97 // notes of the moledy followed by the duration
98 // a 4 means a quarter note, 8 an eighteenth , 16 sixteenth, so
on
99 // !!negative numbers are used to represent dotted notes
100 // so -4 means a dotted quarter note, that is, a quarter plus
an eighteenth
101 int melody[] = {
102 NOTE_E4,4, NOTE_E4,4, NOTE_F4,4, NOTE_G4,4,//1
103 NOTE_G4,4, NOTE_F4,4, NOTE_E4,4, NOTE_D4,4,
104 NOTE_C4,4, NOTE_C4,4, NOTE_D4,4, NOTE_E4,4,
105 NOTE_E4,-4, NOTE_D4,8, NOTE_D4,2,
106 NOTE_E4,4, NOTE_E4,4, NOTE_F4,4, NOTE_G4,4,//4
107 NOTE_G4,4, NOTE_F4,4, NOTE_E4,4, NOTE_D4,4,
108 NOTE_C4,4, NOTE_C4,4, NOTE_D4,4, NOTE_E4,4,
109 NOTE_D4,-4, NOTE_C4,8, NOTE_C4,2,
110 NOTE_D4,4, NOTE_D4,4, NOTE_E4,4, NOTE_C4,4,//8
111 NOTE_D4,4, NOTE_E4,8, NOTE_F4,8, NOTE_E4,4, NOTE_C4,4,
112 NOTE_D4,4, NOTE_E4,8, NOTE_F4,8, NOTE_E4,4, NOTE_D4,4,
113 NOTE_C4,4, NOTE_D4,4, NOTE_G3,2,
114 NOTE_E4,4, NOTE_E4,4, NOTE_F4,4, NOTE_G4,4,//12
115 NOTE_G4,4, NOTE_F4,4, NOTE_E4,4, NOTE_D4,4,
116 NOTE_C4,4, NOTE_C4,4, NOTE_D4,4, NOTE_E4,4,
117 NOTE_D4,-4, NOTE_C4,8, NOTE_C4,2
118 };
119 // sizeof gives the number of bytes, each int value is composed
of two bytes (16 bits)
120 // there are two values per note (pitch and duration), so for
each note there are four bytes
121 int notes=sizeof(melody)/sizeof(melody[0])/2;
122 // this calculates the duration of a whole note in ms
(60s/tempo)*4 beats
123 int wholenote = (60000 * 4) / tempo;
124 int divider = 0, noteDuration = 0;
125 void setup() {
126 // iterate over the notes of the melody
127 // remember, the array is twice the number of notes (notes +
durations)
128 for (int thisNote = 0; thisNote < notes * 2; thisNote =
thisNote + 2) {
129 // calculates the duration of each note
130 divider = melody[thisNote + 1];
131 if (divider > 0) {
132 noteDuration = (wholenote) / divider; // regular note, just
proceed
133 } else if (divider < 0) {
134 // dotted notes are represented with negative durations!!
135 noteDuration = (wholenote) / abs(divider);
136 noteDuration *= 1.5; // increases the duration in half
for dotted notes
137 }
138 // we only play the note for 90% of the duration, leaving
10% as a pause
139 tone(buzzer, melody[thisNote], noteDuration*0.9);
140 // Wait for the specief duration before playing the next note
141 delay(noteDuration);
142 noTone(buzzer); // stop the waveform generation before
the next note
143 }
144 }
145 void loop() {
146 // if you want to repeat the song forever,
147 // just paste the setup code here instead.
148 }

实验现象
将实验代码上传至Arduino Nano主板上,利用USB线上电后,小车PCB板上的喇叭功放
元件就开始播放一首音乐。

You might also like