数字部分-旋律音调

这个示例将展示如何用ton()来发出音符,演奏一段简单的旋律。

硬件需求
—Arduino控制板
—8欧喇叭

原理图
clip_image002

原理图
clip_image004

代码

下面的代码会用到额外的库文件“pitches.h”,这个库外立面包含了完整的音符值。打个比方,NOTE_C4就是C中调,NOTE_FS4就是升F调等等。

#include "pitches.h"

// 旋律中的音符

int melody[] = {

  NOTE_C4, NOTE_G3, NOTE_G3, NOTE_A3, NOTE_G3, 0, NOTE_B3, NOTE_C4

};

// 音符时值: 4 = 4分音符, 8 = 8分音符, 等等:

int noteDurations[] = {

4, 8, 8, 4, 4, 4, 4, 4

};

void setup() {

// 循环旋律中的音符:

for (int thisNote = 0; thisNote < 8; thisNote++) {

// 为了计算音符时间,我们用1秒作为每个音符单位

//举例子四分音符 = 1000 / 4, 8分音符 = 1000/8, 等.

int noteDuration = 1000 / noteDurations[thisNote];

tone(8, melody[thisNote], noteDuration);

// 为了辨别音符,设置一个最小区别时间

// 音符音长 + 30%:

int pauseBetweenNotes = noteDuration * 1.30;

delay(pauseBetweenNotes);

// 停止tone播放:

noTone(8);

}

}

void loop() {

// 不需要再重复

}
0
分享到:

评论0

请先

本站资料配套硬件销售店铺:天猫店ultirobot.tmall.com 、三冠淘宝店zhongbest.taobao.com
没有账号? 忘记密码?