在线客服系统程序河北seo网络推广
Arduino示例代码讲解:Melody 旋律
- Melody 旋律
- 代码功能
- 代码逐行解释
- 1. 注释部分
- 2. 包含头文件
- 3. 变量定义
- 4. `setup()` 函数
- 5. `loop()` 函数
- 硬件连接
- **扬声器连接**:
- **Arduino板**:
- 运行结果
- 修改建议
- 关于`pitches.h`文件
- 视频讲解
Melody 旋律
这段代码是一个Arduino示例程序,名为“Melody”,用于通过一个扬声器播放一段简单的旋律。代码使用了tone()
函数来生成音调,并通过一个数组来定义旋律中的音符和音符的持续时间。
/*MelodyPlays a melodycircuit:* 8-ohm speaker on digital pin 8created 21 Jan 2010modified 30 Aug 2011by Tom IgoeThis example code is in the public domain.http://arduino.cc/en/Tutorial/Tone*/
#include "pitches.h"// notes in the melody:
int melody[] = {NOTE_C4, NOTE_G3, NOTE_G3, NOTE_A3, NOTE_G3, 0, NOTE_B3, NOTE_C4
};// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = {4, 8, 8, 4, 4, 4, 4, 4
};void setup() {// iterate over the notes of the melody:for (int thisNote = 0; thisNote < 8; thisNote++) {// to calculate the note duration, take one second// divi