将 HC-SR04 超声波距离传感器与 Arduino 结合使用

对于机器人项目来说最有用的传感器之一是距离传感器。HC-SR04 是一款廉价的超声波距离传感器,可以帮助您的机器人在房间内导航。只要稍加小心并添加一个附加组件,它也可以用作测量设备。在本文中,您将了解如何将这款奇妙的小设备与 Arduino 结合使用所需的一切知识。

HC-SR04 超声波距离传感器

HC-SR04 超声波距离传感器是一种价格低廉的设备,对于机器人和测试设备项目非常有用。这个微型传感器能够测量其自身与最近的固体物体之间的距离,如果您想避免撞墙,这确实是很好的信息!

HC-SR04 可以直接连接到 Arduino 或其他微控制器,工作电压为 5 伏。它也可以与 Raspberry Pi 一起使用,但由于 HC-SR04 需要 5 伏逻辑,因此您需要几个电阻器将其与 Pi 的 3.3 伏 GPIO 端口连接。

这款超声波距离传感器能够测量 2 厘米到 400 厘米之间的距离(对于那些不会“讲”公制的人来说,大约是 1 英寸到 13 英尺)。它是一种低电流设备,因此适用于电池供电的设备。作为奖励,它甚至看起来很酷,就像你最新的机器人发明的一双 Wall-E 机器人眼睛!

请继续阅读,我将向您展示如何连接和使用 HC-SR04 超声波距离传感器。我们还将对其进行一些测试,以了解其准确性,并研究如何提高准确性。当然,我还会提供一些示例代码和项目供您尝试。

让我们开始吧!

 

HC-SR04 的工作原理

超声波距离传感器使用超声波脉冲(高于人类听觉范围的声音)来检测它们与附近固体物体之间的距离。传感器由两个主要部件组成:

  • 超声波发射器– 发射超声波脉冲,工作频率为 40 KHz
  • 超声波接收器——接收器监听传输的脉冲。如果它接收到它们,就会产生一个输出脉冲,其宽度可用于确定脉冲行进的距离。

HC-SR04 有以下四个连接:

  • VCC – 这是 5 伏正极电源。
  • Trig——这是“触发”引脚,被驱动以发送超声波脉冲。
  • ECHO– 这是在接收到反射信号时产生脉冲的引脚。脉冲的长度与检测到传输信号所需的时间成正比。
  • GND – 这是接地引脚。

HC-SR04 引脚分配

该装置的工作原理如下:

  1. 持续时间至少为 10 uS(10 微秒)的 5 伏脉冲施加到触发引脚(Trig)
  2. HC-SR04 将开始发送 40 KHz 的八个脉冲突发进行响应。
  3. 八个超声波脉冲通过发射器发射出去。同时,Echo 引脚变高,开始准备接收回弹的脉冲信号。
  4. 如果38毫秒还未收到回弹的脉冲信号,则 Echo 返回低电平,表明传感器范围内没有障碍物。
  5. 如果脉冲被反射回来,则 Echo 引脚在接收到信号时由原来的高电平变成低电平。这样就产生一个脉冲,其宽度在 150 uS 到 25 mS 之间变化,具体取决于接收信号所需的时间。
  6. 接收到的脉冲宽度用于计算到反射物体的距离。请记住,脉冲表示信号发送出去和反射回来所花费的时间,因此为了获得需要将结果除以一半的距离。

下图显示了HC-SR04超声波距离传感器的尺寸以及有效操作角度。正如您所看到的,当要检测的物体位于传感器正前方时,传感器最为准确,但您确实会从 45 度“窗口”内的物体获得响应。文档建议将该窗口限制在 30 度(两侧 15 度)以获得准确的读数。

HC-SR04传感器

连接 HC-SR04

将 HC-SR04 连接到 Arduino 非常简单。您需要几个数字 I/O 端口以及与 Arduino 的 5 伏和接地引脚的连接。  

HC-SR04 基本连接

实际上,如果您缺少引脚,您甚至可以将 HC-SR04 的 Trigger 和 Echo 引脚连接到 Arduino 上的一个数字 I/O 引脚,并使用代码在输出之间切换引脚(以发送 10 uS 脉冲) )和输入(接收回波脉冲)。一些超声波传感器实际上只有一个引脚可以同时执行触发和回波操作。我将讨论这个问题并进一步给出一个例子,所以请继续阅读。

我将在这里向您展示的大多数示例都使用更传统的两针方法。任何空闲的 Arduino 和任何数字 I/O 引脚都可以使用,因此如果您希望将其连接到一组不同的 I/O 引脚,则只需更改草图即可反映这些更改。在我们的演示中,我将使用 Arduino Uno 和引脚 10 作为触发器,引脚 13 作为 Echo。

HC-SR04 的应用说明强调,在连接 VCC(5 伏)之前,您需要连接接地引脚,因此,如果您在无焊面包板上进行“实时”实验,您可能需要记住这一点。

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
51
52
53
54
55
56
57
 
#define trigPin 10
#define echoPin 13
 
float duration, distance;
 
void setup() {
  Serial.begin (9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
}
 
void loop() {
  
  // 设置trig为低电平,做好准备。
  
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  
  // 计量脉冲的长度
  duration = pulseIn(echoPin, HIGH);
  
  // Determine distance from duration
  // Use 343 metres per second as speed of sound
  
  distance = (duration / 2) * 0.0343;
  
  // Send results to Serial Monitor
 
  Serial.print("Distance = ");
  if (distance >= 400 || distance <= 2) {
     Serial.println("Out of range");
  }
  else {
    Serial.print(distance);
    Serial.println(" cm");
    delay(500);
  }
  delay(500);
}

Arduino 代码库

在我们的第一个草图中,我们没有使用任何代码库,我们只是使用Arduino的delayMicrosecond命令来创建用于触发的10 uS脉冲,并使用pulseIn命令来测量接收到的信号脉冲宽度。然而,还有其他方法可以使用特殊的代码库来做到这一点。其中有很多可用的,最通用的是“NewPing”。

NewPing 库由Tim Eckel编写,它取代了由 Caleb Zulawski 编写的旧 Ping 库,主要为 Parallax Ping 超声波传感器设计(尽管如果您在 3 针模式下使用它,它将与 HC-SR04 一起使用) 。

NewPing 库非常先进,它大大提高了我们原始草图的准确性。它还支持同时支持多达15个超声波传感器,并且可以直接以厘米、英寸或持续时间为单位输出。

这是我们使用 NewPing 库重写的草图:

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
#include "NewPing.h"
 
// Hook up HC-SR04 with Trig to Arduino Pin 10, Echo to Arduino pin 13
// Maximum Distance is 400 cm
 
#define TRIGGER_PIN  10
#define ECHO_PIN     13
#define MAX_DISTANCE 400
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
 
float distance;
 
void setup() {
  Serial.begin (9600);
}
 
void loop() {
  
  distance = sonar.ping_cm();
  
  // Send results to Serial Monitor
  Serial.print("Distance = ");
  if (distance >= 400 || distance <= 2) {
    Serial.println("Out of range");
  }
  else {
    Serial.print(distance);
    Serial.println(" cm");
    delay(500);
  }
  delay(500);
}

上面的草图很简单,效果也很好,但分辨率只有一厘米。如果您想恢复小数点值,您可以在持续时间模式而不是距离模式下使用 NewPing。然后,我们可以使用持续时间来计算距离,就像我们在第一个草图中所做的那样。

这是我们重写的 NewPing 草图,使用持续时间而不是距离。

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
51
#include "NewPing.h"
 
// Hook up HC-SR04 with Trig to Arduino Pin 10, Echo to Arduino pin 13
// Maximum Distance is 400 cm
 
#define TRIGGER_PIN  10
#define ECHO_PIN     13
#define MAX_DISTANCE 400
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
 
float duration, distance;
 
void setup() {
  Serial.begin (9600);
}
 
void loop() {
  
  duration = sonar.ping();
  
  // Determine distance from duration
  // Use 343 metres per second as speed of sound
  
  distance = (duration / 2) * 0.0343;
  
  // Send results to Serial Monitor
  Serial.print("Distance = ");
  if (distance >= 400 || distance <= 2) {
    Serial.println("Out of range");
  }
  else {
    Serial.print(distance);
    Serial.println(" cm");
    delay(500);
  }
  delay(500);

NewPing 的另一个功能是“迭代” 迭代意味着多次遍历某件事,而这正是迭代模式的作用。它需要多次而不是一次持续时间测量,丢弃所有无效读数,然后对剩余读数进行平均。默认情况下需要 5 个读数,但您实际上可以指定任意数量的读数。

在这里,我们再次使用为使用迭代而编写的 NewPing 草图。正如您所看到的,它实际上与之前的草图相同,所添加的只是一个用于指定迭代次数的变量。

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
51
52
53
/*
  HC-SR04 NewPing Iteration Demonstration
  HC-SR04-NewPing-Iteration.ino
  Demonstrates using Iteration function of NewPing Library for HC-SR04 Ultrasonic Range Finder
  Displays results on Serial Monitor
 
  DroneBot Workshop 2017
  http://dronebotworkshop.com
*/
 
// This uses Serial Monitor to display Range Finder distance readings
 
// Include NewPing Library
#include "NewPing.h"
 
// Hook up HC-SR04 with Trig to Arduino Pin 10, Echo to Arduino pin 13
// Maximum Distance is 400 cm
 
#define TRIGGER_PIN  10
#define ECHO_PIN     13
#define MAX_DISTANCE 400
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
 
float duration, distance;
 
int iterations = 5;
 
void setup() {
  Serial.begin (9600);
}
 
void loop() {
  
  duration = sonar.ping_median(iterations);
  
  // Determine distance from duration
  // Use 343 metres per second as speed of sound
  
  distance = (duration / 2) * 0.0343;
  
  // Send results to Serial Monitor
  Serial.print("Distance = ");
  if (distance >= 400 || distance <= 2) {
    Serial.println("Out of range");
  }
  else {
    Serial.print(distance);
    Serial.println(" cm");
    delay(500);
  }
  delay(500);
}

提高准确性

HC-SR04 相当准确,其基本形式对于机器人、入侵者检测或接近警报非常有用。但有时您可能需要更高的准确性,例如您可能正在构建测量工具或可能正在使用机器人来绘制房间的周长。如果是这样,您可以采取一些措施来提高 HC-SR04 的准确性。

正如我在上一节中提到的,NewPing 库已经实现了许多内部技术来提高传感器的准确性。在大多数情况下,这就是提高阅读能力所需的全部内容。

如果您正在设计在室外或异常炎热或寒冷的环境中使用的设备,您可能需要考虑空气中的声速随温度、气压和湿度的变化而变化的事实。由于声速会影响我们的 HC-SR04 距离计算,如果温度比室温高或低很多,这可能会影响我们的读数。

为了考虑温度和湿度,我决定使用 DHT22 传感器,它相对便宜但非常准确。您还可以使用较便宜的 DHT-11 进行此实验,但精度稍差。这是我连接它的方法:

HC-SR04 带 DHT22

DHT22 需要几个代码库才能运行,Adafruit 有两个库可以与 DHT22 和 DHT11 很好地配合使用。Adafruit AM2315 库和 Adafruit 统一传感器库都可以使用库管​​理器直接安装在 Arduino IDE 中。

安装 Adafruit 库后,我编写了一个快速测试草图,以确保我的 DHT22 正常工作。

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
51
52
#include "DHT.h";
 
// Define Constants
 
#define DHTPIN 7     // DHT-22 Output Pin connection
#define DHTTYPE DHT22   // DHT Type is DHT 22 (AM2302)
 
// Initialize DHT sensor for normal 16mhz Arduino
 
DHT dht(DHTPIN, DHTTYPE);
 
 
// Define Variables
 
float hum;  //Stores humidity value
float temp; //Stores temperature value
 
void setup()
{
  Serial.begin(9600);
  dht.begin();
}
 
void loop()
{
    delay(2000);  // Delay so sensor can stabalize
  
    hum = dht.readHumidity();  // Get Humidity value
    temp= dht.readTemperature();  // Get Temperature value
    
    // Print temperature and humidity values to serial monitor
    
    Serial.print("Humidity: ");
    Serial.print(hum);
    Serial.print(" %, Temp: ");
    Serial.print(temp);
    Serial.println(" Celsius");
  
}

最后,这是一个草图,考虑了温度和湿度因素,以提高使用 DHT22 的精度。

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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
  HC-SR04 with Temp and Humidity Demonstration
  HC-SR04-Temp-Humid-Demo.ino
  Demonstrates enhancements of HC-SR04 Ultrasonic Range Finder
  With DHT22 Temperature and Humidity Sensor
  Displays results on Serial Monitor
 
  DroneBot Workshop 2017
  http://dronebotworkshop.com
*/
 
// Include DHT Libraries from Adafruit
// Dependant upon Adafruit_Sensors Library
#include "DHT.h";
 
// Include NewPing Library
#include "NewPing.h"
 
// Define Constants
 
#define DHTPIN 7       // DHT-22 Output Pin connection
#define DHTTYPE DHT22   // DHT Type is DHT 22 (AM2302)
#define TRIGGER_PIN  10
#define ECHO_PIN     13
#define MAX_DISTANCE 400
 
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
 
// Define Variables
 
float hum;    // Stores humidity value in percent
float temp;   // Stores temperature value in Celcius
float duration; // Stores HC-SR04 pulse duration value
float distance; // Stores calculated distance in cm
float soundsp;  // Stores calculated speed of sound in M/S
float soundcm;  // Stores calculated speed of sound in cm/ms
int iterations = 5;
 
// Initialize DHT sensor for normal 16mhz Arduino
 
DHT dht(DHTPIN, DHTTYPE);
 
void setup() {
  Serial.begin (9600);
  dht.begin();
}
 
void loop()
{
 
  delay(2000);  // Delay so DHT-22 sensor can stabalize
  
    hum = dht.readHumidity();  // Get Humidity value
    temp= dht.readTemperature();  // Get Temperature value
    
    // Calculate the Speed of Sound in M/S
    soundsp = 331.4 + (0.606 * temp) + (0.0124 * hum);
    
    // Convert to cm/ms
    
    soundcm = soundsp / 10000;
    
  duration = sonar.ping_median(iterations);
  
  // Calculate the distance
  distance = (duration / 2) * soundcm;
  
  // Send results to Serial Monitor
  
    Serial.print("Sound: ");
    Serial.print(soundsp);
    Serial.print(" m/s, ");
    Serial.print("Humid: ");
    Serial.print(hum);
    Serial.print(" %, Temp: ");
    Serial.print(temp);
    Serial.print(" C, ");
    Serial.print("Distance: ");
 
    if (distance >= 400 || distance <= 2) {
    Serial.print("Out of range");
    }
    else {
    Serial.print(distance);
    Serial.print(" cm");
    delay(500);
    }
  
  Serial.println(" ");
}

在我在工作台上的测试中,我发现它确实提高了读数的准确性。

使用 3 线模式

正如我之前提到的,可以在“3 线模式”下使用 HC-SR04。在此模式下,您只需要与单个 Arduino 数字 I/O 引脚进行一次连接。还有其他仅在 3 线模式下运行的超声波传感器。

在 3 线模式下,单个 I/O 引脚既用作输入又用作输出。这是可能的,因为从来没有同时使用输入和输出的时刻。通过消除一个 I/O 引脚需求,我们可以保留与 Arduino 的连接并将其用于其他用途。当使用像 ATtiny85 这样的 I/O 引脚数量有限的芯片时,它也很有用。

以下是我将 HC-SR04 连接到 Arduino 的方法

这里提醒使用图形化编程的朋友,接收信号前,将管脚模式改成INPUT模式否则无法正确接收信号。例如使用mixly编程的朋友。

HC-SR04 3线模式

正如你所看到的,我所做的就是将触发器和接收器连接到 Arduino 引脚 10。

这是我为使用它而编写的草图。请注意,此草图与前一个草图之间的唯一区别是我为 Trigger 和 Echo 引脚值指定了引脚 10。草图的其余部分是相同的。

#include "DHT.h";
 
// Include NewPing Library
#include "NewPing.h"
 
// Define Constants
 
#define DHTPIN 7       // DHT-22 Output Pin connection
#define DHTTYPE DHT22   // DHT Type is DHT 22 (AM2302)
#define TRIGGER_PIN  10  // Trigger and Echo both on pin 10
#define ECHO_PIN     10
#define MAX_DISTANCE 400
 
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
 
// Define Variables
 
float hum;    // Stores humidity value in percent
float temp;   // Stores temperature value in Celcius
float duration; // Stores HC-SR04 pulse duration value
float distance; // Stores calculated distance in cm
float soundsp;  // Stores calculated speed of sound in M/S
float soundcm;  // Stores calculated speed of sound in cm/ms
int iterations = 5;
 
// Initialize DHT sensor for normal 16mhz Arduino
 
DHT dht(DHTPIN, DHTTYPE);
 
void setup() {
  Serial.begin (9600);
  dht.begin();
}
 
void loop()
{
 
  delay(2000);  // Delay so DHT-22 sensor can stabalize
  
    hum = dht.readHumidity();  // Get Humidity value
    temp= dht.readTemperature();  // Get Temperature value
    
    // Calculate the Speed of Sound in M/S
    soundsp = 331.4 + (0.606 * temp) + (0.0124 * hum);
    
    // Convert to cm/ms
    
    soundcm = soundsp / 10000;
    
  duration = sonar.ping_median(iterations);
  
  // Calculate the distance
  distance = (duration / 2) * soundcm;
  
  // Send results to Serial Monitor
  
    Serial.print("Sound: ");
    Serial.print(soundsp);
    Serial.print(" m/s, ");
    Serial.print("Humid: ");
    Serial.print(hum);
    Serial.print(" %, Temp: ");
    Serial.print(temp);
    Serial.print(" C, ");
    Serial.print("Distance: ");
 
    if (distance >= 400 || distance <= 2) {
    Serial.print("Out of range");
    }
    else {
    Serial.print(distance);
    Serial.print(" cm");
    delay(500);
    }
  
  Serial.println(" ");
}

 

使用多个 HC-SR04 传感器

在许多应用中,您需要在设计中使用多个 HC-SR04 超声波传感器。当您想要从机器人或项目的不同侧面监控到外部物体的距离时,可能会发生这种情况。其中两个可用于前后传感器,或连接其中 6 个并监控立方体的每一侧 - 这取决于您!

使用多个传感器时,一个明显的考虑因素是您需要防止一个传感器发出的信号被另一个传感器拾取和测量。实现此目的的最简单方法是简单地在一个传感器上触发触发器,并等待收到回波,然后再继续处理下一个传感器。在读数之间留一点延迟可能是明智之举,以防之前的传感器声波仍在房间内反弹!

以下是我如何在 Arduino 上使用两个 HC-SR04 超声波传感器。请注意,我以 3 线模式连接它们,如果您愿意,也可以以传统的 4 线方式连接它们。如果您这样做,则只需修改草图以指定不同的触发器和回声引脚。

HC-SR04 两个传感器

这是使用 NewPing 库和两个传感器的草图。与所有其他草图一样,它将结果输出到串行监视器。

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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*
  Dual HC-SR04 with Temp and Humidity Demonstration
  HC-SR04-Temp-Humid-Dual-Demo.ino
  Demonstrates enhancements of HC-SR04 Ultrasonic Range Finder
  With DHT22 Temperature and Humidity Sensor
  Displays results on Serial Monitor
 
  DroneBot Workshop 2017
  http://dronebotworkshop.com
*/
 
// Include DHT Libraries from Adafruit
// Dependant upon Adafruit_Sensors Library
#include "DHT.h";
 
// Include NewPing Library
#include "NewPing.h"
 
// Define Constants
 
#define DHTPIN 7       // DHT-22 Output Pin connection
#define DHTTYPE DHT22   // DHT Type is DHT 22 (AM2302)
#define TRIGGER_PIN_1  10
#define ECHO_PIN_1     10
#define TRIGGER_PIN_2  5
#define ECHO_PIN_2     5
#define MAX_DISTANCE 400
 
NewPing sonar1(TRIGGER_PIN_1, ECHO_PIN_1, MAX_DISTANCE);
NewPing sonar2(TRIGGER_PIN_2, ECHO_PIN_2, MAX_DISTANCE);
 
// Define Variables
 
float hum;    // Stores humidity value in percent
float temp;   // Stores temperature value in Celcius
float duration1; // Stores First HC-SR04 pulse duration value
float duration2; // Stores Second HC-SR04 pulse duration value
float distance1; // Stores calculated distance in cm for First Sensor
float distance2; // Stores calculated distance in cm for Second Sensor
float soundsp;  // Stores calculated speed of sound in M/S
float soundcm;  // Stores calculated speed of sound in cm/ms
int iterations = 5;
 
// Initialize DHT sensor for normal 16mhz Arduino
 
DHT dht(DHTPIN, DHTTYPE);
 
void setup() {
  Serial.begin (9600);
  dht.begin();
}
 
void loop()
{
 
  delay(1000);  // Delay so DHT-22 sensor can stabalize
  
    hum = dht.readHumidity();  // Get Humidity value
    temp= dht.readTemperature();  // Get Temperature value
    
    // Calculate the Speed of Sound in M/S
    soundsp = 331.4 + (0.606 * temp) + (0.0124 * hum);
    
    // Convert to cm/ms
    
soundcm = soundsp / 10000;
// Measure duration for first sensor
    
  duration1 = sonar1.ping_median(iterations);
  
  // Add a delay between sensor readings
  
  delay(1000);
  
  // Measure duration for first sensor
  
  duration2 = sonar2.ping_median(iterations);
  
  // Calculate the distances for both sensors
  
  distance1 = (duration1 / 2) * soundcm;
  distance2 = (duration2 / 2) * soundcm;
  
  // Send results to Serial Monitor
  
    Serial.print("Distance 1: ");
 
    if (distance1 >= 400 || distance1 <= 2) {
    Serial.print("Out of range");
    }
    else {
    Serial.print(distance1);
    Serial.print(" cm ");
    }
    
    Serial.print("Distance 2: ");
 
    if (distance2 >= 400 || distance2 <= 2) {
    Serial.print("Out of range");
    }
    else {
    Serial.print(distance2);
    Serial.print(" cm");
    }
  
  Serial.println(" ");
}
  • 0
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值