Making Music (音乐制作)
In this project, you'll learn how to generate (激发) sound with the Arduino.
| Parts needed:
Software (软件) stuff you'll learn:
- tone (音调),
- noTone (没有音调),
- #define (定义),
- #include (包括)
|
1
| Making the connections (连接)
- Connect one terminal (终端) on the buzzer (蜂鸣器) to pin (控制开关) 8 on the Arduino.
- Connect the other terminal (终端) on the buzzer (蜂鸣器) to GND (电线接地端) on the Arduino.
|
|
2
| Create (创建) a new program by selecting File (创建文档) from the menu (菜单),
then select New.
|
3
| Type (键入) in this program.
The tone command (命令) generates (激发) a music note (音符) in the given frequency (顺序), in this case 440 Hz (赫兹), through the specified pin number, in this case it is pin 8. The frequence 440 Hz corresponds to the A note on the piano (钢琴).
The delay (延迟) command causes (导致) the note to play for 1 second before turning it off with the
noTone command.
The noTone command stops the sound.
|
|
4
| Upload (上传) and run the program. You should hear a note for one second (秒).
|
5
| Change the number 440 in the tone command to 523.
Upload and run the program again.
What do you notice? Experiment with other numbers. (你注意到了什么?用其他数字试一下.)
|
6
| This next program will play several notes of a song.
Create a new program (程序) by selecting File from the menu,
then select New. Type in this program.
The #define (定义) command
defines an identifier (鉴定) name and assigns (指定) a number to it. For example, the identifier name NOTE_G3 is given the value (值) 196.
Later in the code (代码), we can use the identifier name instead of the number.
The identifier name must start with a letter and cannot have any spaces.
Notice that there is no semicolon (分号) at the end of the #define line.
|
|
7
| Upload and run (运行) the program. You should hear a song.
|
| |