Arduino实验二:音乐编程
本文最后更新于 655 天前,其中的信息可能已经有所发展或是发生改变。

实验目的:初步了解Arduino程序语言和Arduino程序的执行过程,掌握蜂鸣器、LED灯的使用。

实验器材

  • 蜂鸣器一个
  • 红色LED灯一个
  • Arduino主板
  • IO传感器拓展板
  • USB线
  • 绿红黑线若干
  • 实验现象:打开串口监视器,输入歌曲名称,歌曲就会播放,并伴随LED灯闪烁。

实验原理

  • 蜂鸣器:蜂鸣器是一种电子发声元器件,本次实验中通过改变蜂鸣器发声的频率,使之发出音乐。
  • LED灯:LED灯是一种发光器件,我们可以通过改变LED灯发光的时间使之与蜂鸣器发声节拍配合。

实验备注:需要一点很基础很基础的乐理,不然会有点头疼的

频率参考见下图:

实验过程:

器材连线:

  • 断电
  • 蜂鸣器连接数字口13
  • LED灯连接数字口5
  • 红外传感器接数字口10
  • USB连电脑

参考连线图

代码如下:

#include <IRremote.h>
#define IR_RECEIVE_PIN 10                       //红外接受定义在数字口10
    
    int suiyuetone[]=                               //碎月每拍对应的频率
    {
      393,495,556,661,
      700,661,700,786,661,556,
      495,393,661,700,556,556,661,
      700,661,700,786,990,1112
    };
    
    float suiyuebeat[]=                             //碎月的节奏
    {
      1,1,1,1,
      2,1,1,2,1,1,
      1,1,1,1,2,1,1,
      2,1,1,2,1,1
    };
  

    // little star 简谱
    int lstune[]={
      262,262,393,393,
      441,441,393,
      350,350,330,330,
      294,294,262,
      393,393,350,350,
      330,330,294,
      393,393,350,350,
      330,330,294,
      262,262,393,393,
      441,441,393,
      350,350,330,330,
      294,294,262
    };
    //little star 节拍
    float lsbeat[]={
      1,1,1,1,
      1,1,1+1,
      1,1,1,1,
      1,1,1+1,
      1,1,1,1,
      1,1,1+1,
      1,1,1,1,
      1,1,1+1,
      1,1,1,1,
      1,1,1+1,
      1,1,1,1,
      1,1,1+1
    };
    
    int buzzerpin =13 ;  //蜂鸣器引脚
    int ledpin =5 ;     //led灯接口定义 
    int currentDelay = 500; // 初始节拍时间为500 ms
    bool doubleSpeed = false;
    
    void setup() {
      pinMode(buzzerpin,OUTPUT );      //设置蜂鸣器引脚为输出
      pinMode(ledpin,OUTPUT );         //设置led引脚为输出
      Serial.begin(9600);              //设置波特率为9600
      IrReceiver.begin(9600);          //初始化红外接收模块
    }
    
    void start(int len, int buzzpin, int lepin, int *tune, float *beat, int currentDelay) 
    {
      if(currentDelay == 500 ) 
        Serial.println("现在以正常速度播放音乐");
        else if(currentDelay == 250 )
        Serial.println("现在以两倍速度播放音乐");
      for (int x = 0; x < len; x++) 
      {
        tone(buzzpin, tune[x]);                 //蜂鸣器开动
        digitalWrite(lepin,HIGH);               //设置led亮
        delay( currentDelay* beat[x]);          //根据节拍来停止
        digitalWrite(lepin,LOW);                //设置led灭
        noTone(buzzpin);                        //蜂鸣器停止
        delay(100);
      }
      
      IrReceiver.begin(IR_RECEIVE_PIN, ENABLE_LED_FEEDBACK); //接受红外信号
    }
    
    void loop() {
      String select;                //创立一个字符串来存储写入的选项
      
      //反复读取在串口输入的东西,避免使用者分开输入
      while (Serial.available() > 0) {          
        select += char(Serial.read());
        delay(2);                               //每隔2ms从串口监视器读取一次用户输入的信息
      }
       //红外接收模块
       if(IrReceiver.decode()) {
         Serial.println(IrReceiver.decodedIRData.decodedRawData,HEX);   //用十六进制打印接受到的信息
         if (IrReceiver.decodedIRData.decodedRawData ==0xFF00BF00 ) {          //判断红外信号
           doubleSpeed = !doubleSpeed;
           currentDelay = (doubleSpeed) ? 250 : 500;                          //切换节拍时间
           Serial.println(currentDelay);
         }
         IrReceiver.resume();                                                   //接收下一个信号
     }
    
      if (select != "") {                                                       //如果输入不为空,则进行歌曲的判断
        int len;                                        //记录歌曲长度 ,即频率数组总大小除以频率数组的单个元素大小
        if (select == "little star") {
          Serial.println("Next, we will play the little star");
          len = sizeof(lstune) / sizeof(lstune[0]);
          start(len,buzzerpin,ledpin,lstune,lsbeat,500); //start函数参数
        } 
          else if(select=="suiyue")
            {
              Serial.println("Next, we will play the suiyue");
              len = sizeof(suiyuetone) / sizeof(suiyuetone[0]);
              start(len,buzzerpin,ledpin,suiyuetone,suiyuebeat,currentDelay*0.7); 
            }
        else {
          Serial.println("Sorry, there is no such song in the music library.");
        }
      }
    
    }
    

结果展示:

评论

  1. Avatar photo
    博主
    2 年前
    2023-11-14 16:11:03

    有点蚌埠,这个调子,我的评价是我自己也听不出来,但谱是没错的(

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇