arduino 学习(5)

一些其他传感器

  1. 声音传感器 2023-08-10T153426

暂时还没有读出值,还没有找到问题原因。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}

void loop() {
// put your main code here, to run repeatedly:
int sensorValue = analogRead(A0);

Serial.println(sensorValue);

delay(1000);
}

  1. 触摸传感器
    2023-08-10T153926
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    void setup() {
    // put your setup code here, to run once:
    Serial.begin(9600);
    }

    void loop() {
    // put your main code here, to run repeatedly:
    int sensorValue = digitalRead(5);

    Serial.println(sensorValue);

    delay(100);
    }

2023-08-10T153947
触摸时为1,放开时为0;

  1. 蜂鸣器模块
    2023-08-10T160441
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
#include "pitches.h"

// notes in the melody:
int melody[] = {
659, 659, 740, 659, 440, 830,
659, 659, 740, 659, 494, 440
};

// note durations: 4 = quarter note, 8 = eighth note, etc.:
float noteDurations[] = {
2, 2, 1, 1, 1, 0.5,
2, 2, 1, 1, 1, 0.5
};

void setup() {
// iterate over the notes of the melody:
Serial.begin(9600);
for (int thisNote = 0; thisNote < 12; 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 * 3 / 4 / 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;
Serial.print(melody[thisNote]);
Serial.print(" ");
Serial.println(noteDuration);

delay(pauseBetweenNotes);
// stop the tone playing:
}
noTone(8);
}

void loop() {
// no need to repeat the melody.
}
  1. RGB LED 模块
    2023-08-10T163015
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    void setup() {
    // put your setup code here, to run once:

    }

    void loop() {
    // put your main code here, to run repeatedly:
    pinMode(3, OUTPUT);
    pinMode(5, OUTPUT);
    pinMode(6, OUTPUT);
    analogWrite(3, 155);
    analogWrite(5, 123);
    analogWrite(6, 0);
    delay(1000);
    analogWrite(3, 0);
    analogWrite(5, 255);
    analogWrite(6, 0);
    delay(1000);
    analogWrite(3, 0);
    analogWrite(5, 0);
    analogWrite(6, 255);
    delay(1000);
    }

  2. MQ2烟雾传感器
    2023-08-10T163225
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    // the setup routine runs once when you press reset:
    void setup() {
    // initialize serial communication at 9600 bits per second:
    Serial.begin(9600);
    }

    // the loop routine runs over and over again forever:
    void loop() {
    // read the input on analog pin 0:
    int sensorValue = analogRead(A0);
    // print out the value you read:
    Serial.println(sensorValue);
    delay(1); // delay in between reads for stability
    }

  3. 直流电机
    2023-08-10T172718
    这是一个触摸板操控直流电机小风扇的程序

这里将触摸板放到二号GPIO引脚,做外部中断输入,然后去改变风扇状态,在触摸板不抽风的情况下还挺好用的。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
bool fan_state = false;

void setup() {
pinMode(3, OUTPUT);
attachInterrupt(0, warning, RISING);
Serial.begin(9600);
}

void loop() {
if (fan_state) {
digitalWrite(3, HIGH);
} else {
digitalWrite(3, LOW);
}
}

void warning() {
fan_state = !fan_state;
Serial.print("fan_state: ");
Serial.println(fan_state);

}

遇到的问题,

  1. tone函数和IRremote库定时器冲突。
    ANS:https://blog.csdn.net/GreatSimulation/article/details/108982522
    这里我才用了重新再写一个函数的方法,修改TIMER在我的UNO板子上无法生效。

arduino 学习(5)
https://leiz-eng.github.io/2023/08/10/arduino-5/
作者
Lei Zhao
发布于
2023年8月10日
许可协议