回到首页 返回首页
回到顶部 回到顶部
返回上一页 返回上一页

打造一款超声波测距仪 简单

头像 小怼怼 2021.01.09 842 0

材料清单

  • 扩展件 X1
  • 超声波模块 X1
  • OLED屏幕 X1
  • 连接线 X1
  • LED(可加蜂鸣器等传感器) X1

步骤1 组装

project-image

连接要点是,超声波传感器的正极推荐是5V,但是屏幕也是推荐5V。这个项目的重点是显示,所以屏幕的正极是5V,超声波是3.3V。屏幕的通讯方式是I2C,接TT27 28 脚。超声波随意,注意在程序内选择

project-image

看连接方式

步骤2 编写程序

project-image
代码
/*!
 * MindPlus
 * telloesp32
 *
 */
#include <RMTT_Libs.h>
#include <DFRobot_URM10.h>
#include <DFRobot_SSD1306_I2C.h>

// 动态变量
String         mind_s_my_string_variable;
volatile float mind_n_my_float_variable;
// 静态常量
const chCode chBuf[] = {
	{0x8ddd,0xe8b79d,0xBEE0,{0x00,0x00,0x7d,0xfe,0x45,0x00,0x45,0x00,0x45,0x00,0x7d,0xfc,0x11,0x04,0x11,0x04,0x5d,0x04,0x51,0x04,0x51,0xfc,0x51,0x00,0x5d,0x00,0xe1,0x00,0x01,0xfe,0x00,0x00}},
	{0x79bb,0xe7a6bb,0xC0EB,{0x02,0x00,0x01,0x00,0xff,0xfe,0x00,0x00,0x14,0x50,0x13,0x90,0x14,0x50,0x1f,0xf0,0x01,0x00,0x7f,0xfc,0x42,0x04,0x44,0x44,0x4f,0xe4,0x44,0x24,0x40,0x14,0x40,0x08}},
	{0x8d85,0xe8b685,0xB3AC,{0x08,0x00,0x09,0xfc,0x08,0x44,0x7e,0x44,0x08,0x44,0x08,0x94,0xff,0x08,0x08,0xfc,0x28,0x84,0x28,0x84,0x2e,0x84,0x28,0xfc,0x28,0x00,0x58,0x00,0x4f,0xfe,0x80,0x00}},
	{0x51fa,0xe587ba,0xB3F6,{0x01,0x00,0x01,0x00,0x21,0x08,0x21,0x08,0x21,0x08,0x21,0x08,0x3f,0xf8,0x01,0x08,0x01,0x00,0x01,0x00,0x41,0x04,0x41,0x04,0x41,0x04,0x41,0x04,0x7f,0xfc,0x00,0x04}},
	{0x8303,0xe88c83,0xB7B6,{0x04,0x40,0x04,0x40,0xff,0xfe,0x04,0x40,0x10,0x00,0x09,0xf8,0x49,0x08,0x21,0x08,0x25,0x08,0x05,0x28,0x09,0x10,0x71,0x02,0x11,0x02,0x11,0x02,0x10,0xfe,0x00,0x00}},
	{0x56f4,0xe59bb4,0xCEA7,{0x00,0x00,0x7f,0xfc,0x41,0x04,0x41,0x04,0x5f,0xf4,0x41,0x04,0x4f,0xe4,0x41,0x04,0x5f,0xf4,0x41,0x14,0x41,0x14,0x41,0x54,0x41,0x24,0x41,0x04,0x7f,0xfc,0x40,0x04}},
	{0x8bf7,0xe8afb7,0xC7EB,{0x00,0x40,0x40,0x40,0x27,0xfc,0x20,0x40,0x03,0xf8,0x00,0x40,0xe7,0xfe,0x20,0x00,0x23,0xf8,0x22,0x08,0x23,0xf8,0x22,0x08,0x2b,0xf8,0x32,0x08,0x22,0x28,0x02,0x10}},
	{0x8d70,0xe8b5b0,0xD7DF,{0x01,0x00,0x01,0x00,0x01,0x00,0x3f,0xf8,0x01,0x00,0x01,0x00,0xff,0xfe,0x01,0x00,0x01,0x00,0x11,0x00,0x11,0xf8,0x11,0x00,0x11,0x00,0x29,0x00,0x47,0xfe,0x80,0x00}},
	{0x8fd1,0xe8bf91,0xBDFC,{0x00,0x08,0x20,0x1c,0x11,0xe0,0x11,0x00,0x01,0x00,0x01,0xfe,0xf1,0x10,0x11,0x10,0x11,0x10,0x11,0x10,0x12,0x10,0x12,0x10,0x14,0x10,0x28,0x00,0x47,0xfe,0x00,0x00}},
	{0x4e00,0xe4b880,0xD2BB,{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xff,0xfe,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}},
	{0x4e9b,0xe4ba9b,0xD0A9,{0x04,0x40,0x04,0x40,0x24,0x44,0x27,0x48,0x24,0x70,0x24,0x42,0x24,0x42,0x2f,0x42,0xf0,0x3e,0x40,0x00,0x00,0x00,0x1f,0xf0,0x00,0x00,0x00,0x00,0x7f,0xfc,0x00,0x00}}
};
// 创建对象
RMTT_RGB            tt_rgb;
DFRobot_SSD1306_I2C oled12864;
DFRobot_URM10       urm10;


// 主程序开始
void setup() {
	tt_rgb.Init();
	oled12864.setChCode(chBuf);
	tt_rgb.SetRGB(0,255,0);
	oled12864.begin(0x3c);
}
void loop() {
	mind_s_my_string_variable = (urm10.getDistanceCM(13, 14));
	oled12864.setCursorLine(1);
	oled12864.printLine(mind_s_my_string_variable);
	if ((mind_n_my_float_variable>80)) {
		digitalWrite(13, HIGH);
		tt_rgb.SetRGB(255,0,0);
		oled12864.setCursorLine(2);
		oled12864.printLine("距离超出范围");
		oled12864.setCursorLine(3);
		oled12864.printLine("请走近一些");
		oled12864.fillScreen(0);
	}
}

步骤3 代码解读

思路很简单,就是将超声波的消息封装在一个变量内部。

然后将此变量传输到OLED的屏幕上面。

 

要点有,注意OLED屏幕的初始化积木。

这里为了初学者看着清晰字符串用了中文,可以看到程序的大小变大了。如果是未来加其他的传感器或者逻辑的话,可能程序存储空间不够。请考虑将字符串用英文字符串定义。程序的后面又加入了一个判断语句,当距离与判断数值不符合时。会闪烁灯光或者蜂鸣器来提醒用户。后期可以做更多的扩展。

 

未来展望:传感器的使用上面可以换激光测距传感器,体积,精度会更上一个档次。或者是两种传感器的一个配合使用来适应不同的应用场景。注意TT扩展件的I2C接口,理论上是都可以支持I2C通讯的,但是MInd+只默认了27 28。需要去Ardunio里面做一些引脚的Mapping~自己模拟时序也是可以的。

评论

user-avatar