原文:Play a Melody using the tone() function | Arduino Documentation
Play a melody with a Piezo speaker.
LAST REVISION:2023/12/05 22:33
This example shows how to use the
tone()
command to generate notes. It plays a little melody you may have heard before.
Hardware Required
- Arduino board
- piezo buzzer or a speaker
- hook-up wires
Circuit
Schematic
Code
The code below uses an extra file, pitches.h. This file contains all the pitch values for typical notes. For example, NOTE_C4 is middle C. NOTE_FS4 is F sharp, and so forth. This note table was originally written by Brett Hagman, on whose work the tone() command was based. You may find it useful whenever you want to make musical notes.
The main sketch is as follows:
1/*
2
3 Melody
4
5 Plays a melody
6
7 circuit:
8
9 - 8 ohm speaker on digital pin 8
10
11 created 21 Jan 2010
12
13 modified 30 Aug 2011
14
15 by Tom Igoe
16
17 This example code is in the public domain.
18
19 https://www.arduino.cc/en/Tutorial/Tone
20
21*/
22
23#include"pitches.h"
24
25// notes in the melody:
26int[]={
27
28,,,,,0,, NOTE_C4
29};
30
31// note durations: 4 = quarter note, 8 = eighth note, etc.:
32int[]={
33
344,8,8,4,4,4,4,4
35};
36
37voidsetup(){
38
39// iterate over the notes of the melody:
40
41for(int=0;<8;++){
42
43// to calculate the note duration, take one second divided by the note type.
44
45//e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
46
47int=1000/[thisNote];
48
49tone(8,[thisNote],);
50
51// to distinguish the notes, set a minimum time between them.
52
53// the note's duration + 30% seems to work well:
54
55int=*1.30;
56
57delay(pauseBetweenNotes);
58
59// stop the tone playing:
60
61noTone(8);
62
63}
64}
65
66voidloop(){
67
68// no need to repeat the melody.
69}
To make the pitches.h file, either click on the button just below the serial monitor icon and choose "New Tab", or use Ctrl+Shift+N.
Then paste in the following code:
1/*************************************************
2
3 * Public Constants
4
5 *************************************************/
6
7#defineNOTE_B031
8#defineNOTE_C133
9#defineNOTE_CS135
10#defineNOTE_D137
11#defineNOTE_DS139
12#defineNOTE_E141
13#defineNOTE_F144
14#defineNOTE_FS146
15#defineNOTE_G149
16#defineNOTE_GS152
17#defineNOTE_A155
18#defineNOTE_AS158
19#defineNOTE_B162
20#defineNOTE_C265
21#defineNOTE_CS269
22#defineNOTE_D273
23#defineNOTE_DS278
24#defineNOTE_E282
25#defineNOTE_F287
26#defineNOTE_FS293
27#defineNOTE_G298
28#defineNOTE_GS2104
29#defineNOTE_A2110
30#defineNOTE_AS2117
31#defineNOTE_B2123
32#defineNOTE_C3131
33#defineNOTE_CS3139
34#defineNOTE_D3147
35#defineNOTE_DS3156
36#defineNOTE_E3165
37#defineNOTE_F3175
38#defineNOTE_FS3185
39#defineNOTE_G3196
40#defineNOTE_GS3208
41#defineNOTE_A3220
42#defineNOTE_AS3233
43#defineNOTE_B3247
44#defineNOTE_C4262
45#defineNOTE_CS4277
46#defineNOTE_D4294
47#defineNOTE_DS4311
48#defineNOTE_E4330
49#defineNOTE_F4349
50#defineNOTE_FS4370
51#defineNOTE_G4392
52#defineNOTE_GS4415
53#defineNOTE_A4440
54#defineNOTE_AS4466
55#defineNOTE_B4494
56#defineNOTE_C5523
57#defineNOTE_CS5554
58#defineNOTE_D5587
59#defineNOTE_DS5622
60#defineNOTE_E5659
61#defineNOTE_F5698
62#defineNOTE_FS5740
63#defineNOTE_G5784
64#defineNOTE_GS5831
65#defineNOTE_A5880
66#defineNOTE_AS5932
67#defineNOTE_B5988
68#defineNOTE_C61047
69#defineNOTE_CS61109
70#defineNOTE_D61175
71#defineNOTE_DS61245
72#defineNOTE_E61319
73#defineNOTE_F61397
74#defineNOTE_FS61480
75#defineNOTE_G61568
76#defineNOTE_GS61661
77#defineNOTE_A61760
78#defineNOTE_AS61865
79#defineNOTE_B61976
80#defineNOTE_C72093
81#defineNOTE_CS72217
82#defineNOTE_D72349
83#defineNOTE_DS72489
84#defineNOTE_E72637
85#defineNOTE_F72794
86#defineNOTE_FS72960
87#defineNOTE_G73136
88#defineNOTE_GS73322
89#defineNOTE_A73520
90#defineNOTE_AS73729
91#defineNOTE_B73951
92#defineNOTE_C84186
93#defineNOTE_CS84435
94#defineNOTE_D84699
95#defineNOTE_DS84978
and save it as pitches.h
Learn more
You can find more basic tutorials in the built-in examples section.
You can also explore the language reference, a detailed collection of the Arduino programming language.
Last revision 2015/08/11 by SM