[Diao Ye learns programming] Arduino hands-on (49) --- active and passive buzzer module 5

The reference to 37 sensors and modules has been widely circulated on the Internet. In fact, there must be more than 37 sensor modules compatible with Arduino. In view of the fact that I have accumulated some sensor and actuator modules on hand, according to the concept of true knowledge (must be hands-on), for the purpose of learning and communication, here I am going to try and do more experiments one by one.

[Arduino] 168 kinds of sensor module series experiments (data code + simulation programming + graphic programming)
Experiment 49: Active buzzer module low level triggers buzzer control board sound sensor smart car

insert image description here
Program 11: Triangular wave generator alarm sound
experiment open source simulation programming (Linkboy V4.63)

insert image description here

Program 12: Happy birthday to you
Experimental open source simulation programming (Linkboy V4.63)

insert image description here

Arduino experiment scene diagram

insert image description here

Program 13: Looping alto,
experimenting with open-source graphics programming (Mind+, learning while editing)

insert image description here
[Arduino] 168 sensor module series experiments (data code + simulation programming + graphics programming)

Experiment 49: Active buzzer module low-level trigger buzzer control board sound sensor smart car

Program 14: Control the passive buzzer to emit two tiger tones

Arduino experiment open source code

/*
  【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
  程序十四:播放二只老虎乐曲
*/

int buzzer=3;           //設定蜂鳴器接腳為D3
int duration = 500;
int aSo = 392;          //定义音符频率
int bDo = 523;
int bRe = 587;
int bMi = 659;
int bFa = 698;
int bSo = 784;
int bLa = 880;
int bSi = 988;
int bDDo = 1047;

void setup(){
    
    
pinMode(buzzer,OUTPUT);   //設定蜂鳴器為輸出
}

void loop(){
    
    
  tone(3,bDo,duration);
  delay(600);
  tone(3,bRe,duration);
  delay(600);
  tone(3,bMi,duration);
  delay(600);
  tone(3,bDo,duration);
  delay(800);
  
  tone(3,bDo,duration);
  delay(600);
  tone(3,bRe,duration);
  delay(600);
  tone(3,bMi,duration);
  delay(600);
  tone(3,bDo,duration);
  delay(800);
  
  tone(3,bMi,duration);
  delay(600);
  tone(3,bFa,duration);
  delay(600);
  tone(3,bSo,duration);
  delay(800);
  
  tone(3,bMi,duration);
  delay(600);
  tone(3,bFa,duration);
  delay(600);
  tone(3,bSo,duration);
  delay(800);

  tone(3,bSo,duration);
  delay(600);
  tone(3,bLa,duration);
  delay(600);
  tone(3,bSo,duration);
  delay(600);
  tone(3,bFa,duration);
  delay(600);
  tone(3,bMi,duration);
  delay(700);
  tone(3,bDo,duration);
  delay(800);

  tone(3,bSo,duration);
  delay(600);
  tone(3,bLa,duration);
  delay(600);
  tone(3,bSo,duration);
  delay(600);
  tone(3,bFa,duration);
  delay(600);
  tone(3,bMi,duration);
  delay(700);
  tone(3,bDo,duration);
  delay(800);

  tone(3,bDo,duration);
  delay(700);
  tone(3,aSo,duration);
  delay(700);
  tone(3,bDo,duration);
  delay(800);

  tone(3,bDo,duration);
  delay(700);
  tone(3,aSo,duration);
  delay(700);
  tone(3,bDo,duration);
  delay(800);
  delay(2000);
}

tone() function description:

tone(pin, frequency, duration)

pin=signal interface, frequency=frequency, duration=spacing
The scales used in the experiment are Do(523Hz), Re (587Hz), Mi(659Hz), Fa(698Hz), So(784Hz), La(880Hz), Si(988Hz) to treble Do (1047Hz)

To make the buzzer sing is basically to make the buzzer produce sounds of different frequencies. The unit of frequency is Hertz (Hz), which is how many times it vibrates per second.

An international conference in 1939 determined A (La) = 440Hz as the international standard tone, also known as the concert pitch. In 1955, this standard was recognized by the International Organization for Standardization and finalized in 1975.
In other words ~ we only need to make something vibrate 440 times per second, and the sound we hear is A.

Scale – frequency comparison table (unit: Hz)

insert image description here
[Arduino] 168 sensor module series experiments (data code + simulation programming + graphics programming)

Experiment 49: Active buzzer module low-level trigger buzzer control board sound sensor smart car

Procedure 15: Play do re mi fa sol la si

Arduino Reference Open Source Code

/*
  【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
  程序十五:播放do re mi fa sol la si
*/

const int buzzPin = 3;//无源蜂鸣器接D3
int delayTime = 200;

void setup() {
    
    
  // 把你的设置代码放在这里,运行一次
  pinMode(buzzPin, OUTPUT);//设置为输出
}

void loop() {
    
    
  // 把你的主要代码放在这里,重复运行
  tone(buzzPin, 262, 150);
  delay(delayTime);
  noTone(buzzPin);
  tone(buzzPin, 294, 150);
  delay(delayTime);
  noTone(buzzPin);
  tone(buzzPin, 330, 150);
  delay(delayTime);
  noTone(buzzPin);
  tone(buzzPin, 349, 150);
  delay(delayTime);
  noTone(buzzPin);
  tone(buzzPin, 292, 150);
  delay(delayTime);
  noTone(buzzPin);
  tone(buzzPin, 440, 150);
  delay(delayTime);
  noTone(buzzPin);
  tone(buzzPin, 494, 150);
  delay(delayTime);
  noTone(buzzPin);
}

[Arduino] 168 sensor module series experiments (data code + simulation programming + graphics programming)

Experiment 49: Active buzzer module low-level trigger buzzer control board sound sensor smart car

Program 16: Play melody music

Arduino Reference Open Source Code

/*
  【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
  程序十六:播放melody音乐
*/

#define NOTE_C4  262//诠释旋律
#define NOTE_G3  196
#define NOTE_A3  220
#define NOTE_B3  247

int melody[] = {
    
    
  NOTE_C4, NOTE_G3, NOTE_G3, NOTE_A3, NOTE_G3, 0, NOTE_B3, NOTE_C4
};
// 音符时长:4 = 四分音符,8 = 八分音符,等等
int noteDurations[] = {
    
    
  4, 8, 8, 4, 4, 4, 4, 4
};

void setup() {
    
    
  // 遍历旋律的音符
}

void loop() {
    
    
  for (int thisNote = 0; thisNote < 8; thisNote++) {
    
    
    // 计算音符持续时间,用一秒除以音符类型。
    //例如。 四分音符 = 1000 / 4,八分音符 = 1000/8 等等。
    int noteDuration = 1000 / noteDurations[thisNote];
    tone(3, melody[thisNote], noteDuration);
    // 为了区分音符,设置它们之间的最小时间。
    // 音符的持续时间 + 30% 似乎效果很好
    int pauseBetweenNotes = noteDuration * 1.30;
    delay(pauseBetweenNotes);
    // 停止播放音调
    noTone(3);
  }
}

Arduino experiment scene diagram

insert image description here

Program 17: Use the buzzer to play the castle in the sky

insert image description here
Arduino Reference Open Source Code

/*
  【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
  程序十七:利用蜂鸣器播放天空之城
*/

#define NOTE_D0 -1
#define NOTE_D1 294
#define NOTE_D2 330
#define NOTE_D3 350
#define NOTE_D4 393
#define NOTE_D5 441
#define NOTE_D6 495
#define NOTE_D7 556

#define NOTE_DL1 147
#define NOTE_DL2 165
#define NOTE_DL3 175
#define NOTE_DL4 196
#define NOTE_DL5 221
#define NOTE_DL6 248
#define NOTE_DL7 278

#define NOTE_DH1 589
#define NOTE_DH2 661
#define NOTE_DH3 700
#define NOTE_DH4 786
#define NOTE_DH5 882
#define NOTE_DH6 990
#define NOTE_DH7 112
//以上部分是定义是把每个音符和频率值对应起来,其实不用打这么多,但是都打上了,后面可以随意编写

#define WHOLE 1
#define HALF 0.5
#define QUARTER 0.25
#define EIGHTH 0.25
#define SIXTEENTH 0.625
//这部分是用英文对应了拍子,这样后面也比较好看

int tune[] =
{
    
    
  NOTE_D0, NOTE_D0, NOTE_D0, NOTE_D6, NOTE_D7, NOTE_DH1, NOTE_D7, NOTE_DH1, NOTE_DH3, NOTE_D7, NOTE_D7, NOTE_D7, NOTE_D3, NOTE_D3,
  NOTE_D6, NOTE_D5, NOTE_D6, NOTE_DH1, NOTE_D5, NOTE_D5, NOTE_D5, NOTE_D3, NOTE_D4, NOTE_D3, NOTE_D4, NOTE_DH1,
  NOTE_D3, NOTE_D3, NOTE_D0, NOTE_DH1, NOTE_DH1, NOTE_DH1, NOTE_D7, NOTE_D4, NOTE_D4, NOTE_D7, NOTE_D7, NOTE_D7, NOTE_D0, NOTE_D6, NOTE_D7,
  NOTE_DH1, NOTE_D7, NOTE_DH1, NOTE_DH3, NOTE_D7, NOTE_D7, NOTE_D7, NOTE_D3, NOTE_D3, NOTE_D6, NOTE_D5, NOTE_D6, NOTE_DH1,
  NOTE_D5, NOTE_D5, NOTE_D5, NOTE_D2, NOTE_D3, NOTE_D4, NOTE_DH1, NOTE_D7, NOTE_D7, NOTE_DH1, NOTE_DH1, NOTE_DH2, NOTE_DH2, NOTE_DH3, NOTE_DH1, NOTE_DH1, NOTE_DH1,
  NOTE_DH1, NOTE_D7, NOTE_D6, NOTE_D6, NOTE_D7, NOTE_D5, NOTE_D6, NOTE_D6, NOTE_D6, NOTE_DH1, NOTE_DH2, NOTE_DH3, NOTE_DH2, NOTE_DH3, NOTE_DH5,
  NOTE_DH2, NOTE_DH2, NOTE_DH2, NOTE_D5, NOTE_D5, NOTE_DH1, NOTE_D7, NOTE_DH1, NOTE_DH3, NOTE_DH3, NOTE_DH3, NOTE_DH3, NOTE_DH3,
  NOTE_D6, NOTE_D7, NOTE_DH1, NOTE_D7, NOTE_DH2, NOTE_DH2, NOTE_DH1, NOTE_D5, NOTE_D5, NOTE_D5, NOTE_DH4, NOTE_DH3, NOTE_DH2, NOTE_DH1,
  NOTE_DH3, NOTE_DH3, NOTE_DH3, NOTE_DH3, NOTE_DH6, NOTE_DH6, NOTE_DH5, NOTE_DH5, NOTE_DH3, NOTE_DH2, NOTE_DH1, NOTE_DH1, NOTE_D0, NOTE_DH1,
  NOTE_DH2, NOTE_DH1, NOTE_DH2, NOTE_DH2, NOTE_DH5, NOTE_DH3, NOTE_DH3, NOTE_DH3, NOTE_DH3, NOTE_DH6, NOTE_DH6, NOTE_DH5, NOTE_DH5,
  NOTE_DH3, NOTE_DH2, NOTE_DH1, NOTE_DH1, NOTE_D0, NOTE_DH1, NOTE_DH2, NOTE_DH1, NOTE_DH2, NOTE_DH2, NOTE_D7, NOTE_D6, NOTE_D6, NOTE_D6, NOTE_D6, NOTE_D7
};//这部分就是整首曲子的音符部分,用了一个序列定义为tune,整数

float duration[] =
{
    
    
  1, 1, 1, 0.5, 0.5,     1 + 0.5, 0.5, 1, 1,     1, 1, 1, 0.5, 0.5,
  1 + 0.5, 0.5, 1, 1,     1, 1, 1, 1,          1 + 0.5, 0.5, 1, 1,
  1, 1, 0.5, 0.5, 0.5, 0.5,    1 + 0.5, 0.5, 1, 1,     1, 1, 1, 0.5, 0.5,
  1 + 0.5, 0.5, 1, 1,    1, 1, 1, 0.5, 0.5,     1 + 0.5, 0.5, 1, 1,
  1, 1, 1, 0.5, 0.5,    1, 0.5, 0.25, 0.25, 0.25, 0.5,    0.5, 0.5, 0.5, 0.25, 0.5, 1,
  0.5, 0.5, 0.5, 0.5, 1, 1,    1, 1, 1, 0.5, 0.5,    1 + 0.5, 0.5, 1, 1,
  1, 1, 1, 0.5, 0.5,    1.5, 0.5, 1, 1,    1, 1, 1, 1,
  0.5, 0.5, 1, 1, 0.5, 0.5,    1.5, 0.25, 0.5, 1,    1, 1, 1, 1,
  1, 1, 1, 1,    1, 1, 1, 1,    0.5, 0.5, 1, 1, 0.5, 0.5,
  1, 0.5, 0.5, 1, 1,    1, 1, 1, 1,    1, 1, 1, 1,
  0.5, 0.5, 1, 1, 0.5, 0.5,    1, 0.5, 0.25, 0.5, 1,    1, 1, 1, 0.5, 0.5
};//这部分是整首曲子的节拍部分,也定义个序列duration,浮点(数组的个数和前面音符的个数是一样的,一一对应么)

int length;//这里定义一个变量,后面用来表示共有多少个音符
int tonePin = 3; //蜂鸣器的pin

void setup(){
    
    
  pinMode(tonePin, OUTPUT); //设置蜂鸣器的pin为输出模式
  length = sizeof(tune) / sizeof(tune[0]); //这里用了一个sizeof函数, 可以查出tone序列里有多少个音符
}

void loop(){
    
    
  for (int x = 0; x < length; x++) //循环音符的次数
  {
    
    
    tone(tonePin, tune[x]); //此函数依次播放tune序列里的数组,即每个 音符
    delay(400 * duration[x]); //每个音符持续的时间,即节拍duration,是调整时间的越大,曲子速度越慢,越小曲子速度越快,自己掌握
    noTone(tonePin);//停止当前音符,进入下一音符
  }
  delay(5000);//等待5秒后,循环重新开始
}

Blind obstacle avoider (buzzer sounds when there is an obstacle)
experimental open source simulation programming (Linkboy V4.63)
insert image description here

Arduino experiment scene diagram
insert image description here
insert image description here

Guess you like

Origin blog.csdn.net/weixin_41659040/article/details/131843902