arduino 学习(2)
I/O口的高级应用
- 调声函数
- tone()
- noTone()
- tone()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50/*
Melody
Plays a melody
circuit:
- 8 ohm speaker on digital pin 8
created 21 Jan 2010
modified 30 Aug 2011
by Tom Igoe
This example code is in the public domain.
https://www.arduino.cc/en/Tutorial/BuiltInExamples/toneMelody
*/
#include "pitches.h"
// notes in the melody:
int melody[] = {
NOTE_C5, NOTE_D5, NOTE_E5, NOTE_F5, NOTE_G5, NOTE_A5, NOTE_B5
};
// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = {
4, 4, 4, 4, 4, 4, 4
};
void setup() {
// iterate over the notes of the melody:
for (int thisNote = 0; thisNote < 8; thisNote++) {
// to calculate the note duration, take one second divided by the note type.
//e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
int noteDuration = 1000 / noteDurations[thisNote];
tone(8, melody[thisNote], noteDuration);
// to distinguish the notes, set a minimum time between them.
// the note's duration + 30% seems to work well:
int pauseBetweenNotes = noteDuration * 1.30;
delay(pauseBetweenNotes);
// stop the tone playing:
noTone(8);
}
}
void loop() {
// no need to repeat the melody.
}- 脉冲宽度测量函数及超声波测距
- pulseIn()
- 超声波测距
- pulseIn()
- 设置ADC参考电压
- 外部中断
- 外部中断的使用
- 中断模式
- 中断处理函数
- 配置
- 外部中断触发蜂鸣器报警
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21boolean RunBuzzer = true;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
attachInterrupt(0, warning, CHANGE);
}
void loop() {
// put your main code here, to run repeatedly:
if(RunBuzzer) {
tone(8, 1000);
} else {
noTone(8);
}
}
void warning() {
RunBuzzer = !RunBuzzer;
}
- 外部中断的使用
- 调声函数
使用和编写类库
- 将常用函数封装起来使用
- 使用他人的类库
C:\Users\Lei Zhao\AppData\Local\Arduino15\libraries - 编写类库
- 编写头文件
- 编写cpp文件
- 关键字高亮显示
编写keywords.txt文件
- 编写示例程序
类库的优化和发布
这里简单写了一个lib并上传到了Github上。
https://github.com/leiz-eng/Ultrasonic_SR04
如果想要自己的lib可以在manager中被别人搜到,因为采用的是Libs Manager的形式,需要参考如下链接
https://github.com/arduino/library-registry/blob/main/FAQ.md#submission-requirements
arduino 学习(2)
https://leiz-eng.github.io/2023/08/07/arduino-2/