新手笔记 - 让Arduino也唱小苹果

2014-10-231.6万
AI 快速预览详细 收起
本文介绍了如何使用Arduino播放《小苹果》的旋律。首先,通过包含pitches.h文件定义了音调频率。然后,通过定义melody数组和noteDurations数组分别表示音调和节奏。在setup函数中,通过循环遍历这些数组,使用tone函数和delay函数实现音乐播放。这个项目适合零基础的新手,无需音乐理论知识即可完成。
今天看的示例是toneMelody
  1. /*
  2.   Melody
  3. Plays a melody
  4. circuit:
  5. * 8-ohm speaker on digital pin 8
  6. created 21 Jan 2010
  7. modified 30 Aug 2011
  8. by Tom Igoe
  9. This example code is in the public domain.
  10. http://Arduino.cc/en/Tutorial/Tone
  11. */
  12. #include "pitches.h"
  13. // notes in the melody:
  14. int melody[] = {
  15.   NOTE_C4, NOTE_G3,NOTE_G3, NOTE_A3, NOTE_G3,0, NOTE_B3, NOTE_C4};
  16. // note durations: 4 = quarter note, 8 = eighth note, etc.:
  17. int noteDurations[] = {
  18.   4, 8, 8, 4,4,4,4,4 };
  19. void setup() {
  20.   // iterate over the notes of the melody:
  21.   for (int thisNote = 0; thisNote < 8; thisNote++) {
  22.     // to calculate the note duration, take one second
  23.     // divided by the note type.
  24.     //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
  25.     int noteDuration = 1000/noteDurations[thisNote];
  26.     tone(8, melody[thisNote],noteDuration);
  27.     // to distinguish the notes, set a minimum time between them.
  28.     // the note's duration + 30% seems to work well:
  29.     int pauseBetweenNotes = noteDuration * 1.30;
  30.     delay(pauseBetweenNotes);
  31.     // stop the tone playing:
  32.     noTone(8);
  33.   }
  34. }
  35. void loop() {
  36.   // no need to repeat the melody.
  37. }
复制代码
首先看到的是#include "pitches.h" 并且这个文件会同时打开,
看一看先, 里面有#define NOTE_C5  523, 结合tone函数定义,  发现这里面其实是定义的音调的发声频率
而523正好是C大调的Do , 那么对于 1=C 的 Do,Re,Mi,Fa,Sol,La,Si, 分别对应 NOTE_C5, NOTE_D5,NOTE_E5,NOTE_F5,NOTE_G5,NOTE_A5,NOTE_B5
再往下 , 发现noteDuration是发声时长 , int noteDurations[] = { 4, 8, 8, 4,4,4,4,4 }; 第一个音调发是1000/4 ms, 这几个可以看做是 4分音符, 8分音符, 8分音符, 4分音符...
也就是说
int melody[] 定义了音调
int noteDurations[] 定义了节奏
楼主基本可以看懂简谱, 下面省去楼主扒谱的一个多小时,


  1. #include "pitches.h"
  2. // pin8 接限流电阻再接蜂鸣器
  3. // reset 按一下唱一遍
  4. int melody[] = {
  5.   NOTE_E6,NOTE_C6,NOTE_D6,NOTE_A5,
  6.   NOTE_E6,NOTE_D6,NOTE_C6,NOTE_D6,NOTE_A5,
  7.   NOTE_E6,NOTE_C6,NOTE_D6,NOTE_D6,
  8.   NOTE_G6,NOTE_E6,NOTE_B5,NOTE_C6,NOTE_C6,NOTE_B5,
  9.   NOTE_A5,NOTE_B5,NOTE_C6,NOTE_D6,NOTE_G5,
  10.   NOTE_A6,NOTE_G6,NOTE_E6,NOTE_E6,NOTE_D6,
  11.   NOTE_C6,NOTE_D6,NOTE_E6,NOTE_D6,NOTE_E6,NOTE_D6,NOTE_E6,NOTE_G6,
  12.   NOTE_G6,NOTE_G6,NOTE_G6,NOTE_G6,NOTE_G6,NOTE_G6,
  13.   NOTE_E6,NOTE_C6,NOTE_D6,NOTE_A5,
  14.   NOTE_E6,NOTE_D6,NOTE_C6,NOTE_D6,NOTE_A5,
  15.   NOTE_E6,NOTE_C6,NOTE_D6,NOTE_D6,NOTE_D6,
  16.   NOTE_G6,NOTE_E6,NOTE_B5,NOTE_C6,NOTE_C6,NOTE_B5,
  17.   NOTE_A5,NOTE_B5,NOTE_C6,NOTE_D6,NOTE_G5,
  18.   NOTE_A6,NOTE_G6,NOTE_E6,NOTE_E6,NOTE_D6,
  19.   NOTE_C6,NOTE_D6,NOTE_E6,NOTE_D6,NOTE_G5,
  20.   NOTE_A5,NOTE_A5,NOTE_C6,NOTE_A5 };
  21. int noteDurations[] = {
  22.   4,4,4,4,
  23.   8,8,8,8,2,
  24.   4,4,4,4,
  25.   8,8,4,4,8,8,
  26.   4,8,8,4,4,
  27.   8,8,4,3,8,
  28.   4,8,8,8,8,8,16,16,
  29.   4,8,8,8,8,4,
  30.   4,4,4,4,
  31.   8,8,8,8,2,
  32.   4,4,4,8,8,
  33.   8,8,4,4,8,8,
  34.   4,8,8,4,4,
  35.   8,8,4,3,8,
  36.   4,8,8,4,4,
  37.   4,8,8,2 };
  38. void setup() {
  39. for (int thisNote = 0; thisNote < 82; thisNote++) {
  40.     int noteDuration = 1000/noteDurations[thisNote];
  41.     tone(8, melody[thisNote], noteDuration);
  42.     int pauseBetweenNotes = noteDuration * 1.30;
  43.     delay(pauseBetweenNotes);
  44.     noTone(8);
  45.   }
  46. }
  47. void loop() {
  48. }
复制代码




创作许可协议

本项目采用 None(不开放任何权利,保留所有权利) 进行许可。

评论(0)
- 没有更多了 -