实验:两个按键控制播放两首音乐,一个按键停止播放

超级程序猿飞 2023-04-14 17:50:54

以后我就直接讲代码

文字就不怎么写了

实验结果:

一个按键按下,播放《生日快乐》

一个按键按下,播放《小星星》

具体代码如下:

//CH-    FFA25D   播放《生日快乐》
//CH+    FFE21D   播放《小星星》
//CH     FF629D   停止
 
#include<IRremote.h>
 
int PIN_SPEAKER = 8;
int RB=2;
int GB=13;
int BB=12;
int ok=3;

int length = 25;
char notes1[] = "ggagCbggagDCggGECbaffECDC";
int beats1[] = {1,1,2,2,2,4, 1,1,2,2,2,4, 1,1,2,2,2,2,2, 1,1,2,2,2,4,1};
 
char notes2[] = "ccggaagffeeddc "; // a space represents a rest 
int beats2[] = { 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 4 };  
 
int tempo = 300;
 
 
  
 
void playTone1(int tone, int duration) {
  for (long i = 0; i < duration * 1000L; i += tone * 2) {
    digitalWrite(PIN_SPEAKER, HIGH);
    delayMicroseconds(tone);
    digitalWrite(PIN_SPEAKER, LOW);
    delayMicroseconds(tone);
  }
}
 
void playNote1(char note, int duration) {
  char names[] = {'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C', 'D', 'E', 'F', 'G'};
  int tones[] = {1915, 1700, 1519, 1432, 1275, 1136, 1014, 956, 853, 759, 716, 637, 568};
 
  for (int i = 0; i < 12; i++) {
    if (names[i] == note) {
      Serial.print("value:");
      Serial.println(note);
      playTone1(tones[i]*2, duration);
    }
  }
}
 
 
void playTone2(int tone, int duration) { 
  for (long i = 0; i < duration * 1000L; i += tone * 2) { 
    digitalWrite(PIN_SPEAKER, HIGH); 
    delayMicroseconds(tone); 
    digitalWrite(PIN_SPEAKER, LOW); 
    delayMicroseconds(tone); 
  } 
} 
 
void playNote2(char note, int duration) { 
  char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' }; 
  int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956 }; 
 
  // play the tone corresponding to the note name 
  for (int i = 0; i < 8; i++) { 
    if (names[i] == note) { 
      playTone2(tones[i], duration); 
    } 
  } 
} 
 
void setup() {
    pinMode(BB,INPUT);
    pinMode(GB,INPUT);
    pinMode(RB,INPUT);
  pinMode(PIN_SPEAKER, OUTPUT);
  digitalRead(GB)==HIGH;
  digitalRead(RB)==HIGH;
  digitalRead(GB)==HIGH;
Serial.begin(9600);
}
 
void loop() {

   digitalRead(GB)==HIGH;
  digitalRead(RB)==HIGH;
  digitalRead(GB)==HIGH;
 if(digitalRead(GB)==LOW)
 {
  delay(1000);
  if(digitalRead(GB)==LOW)
  ok=1;
 }
 else if(digitalRead(BB)==LOW)
 {
  delay(1000);
  if(digitalRead(BB)==LOW)
  ok=2;
 }
 else if(digitalRead(RB)==LOW)
 {
  delay(1000);
  if(digitalRead(RB)==LOW)
  ok=3;
 }
 
  if(ok==1){
  for (int i = 0; i < length; i++) {
    if (notes1[i] == ' ') {
   
  if(digitalRead(BB)==LOW)
 {
  delay(1000);
  if(digitalRead(BB)==LOW)
  ok=2;
  break;
}
 if(digitalRead(RB)==LOW)
 {
  delay(1000);
  if(digitalRead(RB)==LOW)
  ok=3;
  break;
 }
      delay(beats1[i] * tempo); 
    } else{
       playNote1(notes1[i], beats1[i] * tempo);
         if(digitalRead(BB)==LOW)
 {
  delay(1000);
  if(digitalRead(BB)==LOW)
  ok=2;
  break;
}
 if(digitalRead(RB)==LOW)
 {
  delay(1000);
  if(digitalRead(RB)==LOW)
  ok=3;
  break;
 }
}
  }
      
     
    
    delay(tempo / 2); 
  }
  
  else if(ok==2)
  {
        for (int i = 0; i < length; i++) {
    if (notes2[i] == ' ') {
 
    
  if(digitalRead(RB)==LOW)
 {
  delay(1000);
  if(digitalRead(RB)==LOW)
  ok=3;
  break;
 }
   if(digitalRead(GB)==LOW)
 {
  delay(1000);
  if(digitalRead(GB)==LOW)
  ok=1;
  break;
 }

      delay(beats2[i] * tempo); 
    }
    else{

  if(digitalRead(RB)==LOW)
 {
  delay(1000);
  if(digitalRead(RB)==LOW)
  ok=3;
  break;
 }
   if(digitalRead(GB)==LOW)
 {
  delay(1000);
  if(digitalRead(GB)==LOW)
  ok=1;
  break;
 }
      
      playNote2(notes2[i], beats2[i] * tempo);
    }
    delay(tempo / 2); 
  } 
  }
}

代码如下

管脚定义我觉得根据代码就可以看得出来,就不写了

代码是运行成功的

欢迎复制

...全文
183 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

11

社区成员

发帖
与我相关
我的任务
社区描述
阿弥陀佛
物联网 高校 新疆·阿克苏地区
社区管理员
  • 宇宙作家
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧