arduino 学习(2)

  1. I/O口的高级应用

    1. 调声函数
      1. tone()
        2023-08-07T162658
      2. noTone()
        2023-08-07T162708
    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.
    }
    1. 脉冲宽度测量函数及超声波测距
      1. pulseIn()
        2023-08-07T164233
      2. 超声波测距
    2. 设置ADC参考电压
      2023-08-07T173635
    3. 外部中断
      1. 外部中断的使用
        2023-08-07T173759
      2. 中断模式
        2023-08-07T173812
      3. 中断处理函数
        2023-08-07T173845
      4. 配置
        2023-08-07T174011
        2023-08-07T174020
      5. 外部中断触发蜂鸣器报警
        1
        2
        3
        4
        5
        6
        7
        8
        9
        10
        11
        12
        13
        14
        15
        16
        17
        18
        19
        20
        21
        boolean 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;
        }

        2023-08-07T212155
  2. 使用和编写类库

    1. 将常用函数封装起来使用
    2. 使用他人的类库
      C:\Users\Lei Zhao\AppData\Local\Arduino15\libraries
    3. 编写类库
      1. 编写头文件
      2. 编写cpp文件
      3. 关键字高亮显示
        编写keywords.txt文件
    4. 编写示例程序
  3. 类库的优化和发布
    这里简单写了一个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/
作者
Lei Zhao
发布于
2023年8月7日
许可协议