Skip to content

Commit 84366a6

Browse files
committed
add UNIT_SONIC Example
1 parent fac3497 commit 84366a6

File tree

2 files changed

+98
-0
lines changed

2 files changed

+98
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
*******************************************************************************
3+
* Copyright (c) 2021 by M5Stack
4+
* Equipped with M5Core sample source code
5+
* 配套 M5Core 示例源代码
6+
* Visit the website for more information:https://docs.m5stack.com/en/unit/sonic.i2c
7+
* 获取更多资料请访问:https://docs.m5stack.com/zh_CN/unit/sonic.i2c
8+
*
9+
* describe: Ultrasonic. 超声波测距传感器 I2C版本
10+
* date:2021/11/17
11+
*******************************************************************************
12+
Please connect to Port A,Display the distance measured by ultrasonic
13+
请连接端口A,显示超声波测量的距离
14+
*/
15+
16+
#include <M5Stack.h>
17+
#include <UNIT_SONIC.h>
18+
#include <M5GFX.h>
19+
M5GFX display;
20+
M5Canvas canvas(&display);
21+
22+
SONIC_I2C sensor;
23+
24+
void setup() {
25+
M5.begin(); //Init M5Stack. 初始化M5Stack
26+
// sensor.begin(&Wire, 0x57, 21, 22, 200000L);
27+
sensor.begin();
28+
display.begin();
29+
canvas.setColorDepth(1);
30+
canvas.setFont(&fonts::Orbitron_Light_24);
31+
canvas.setTextSize(1);
32+
canvas.createSprite(display.width(), display.height());
33+
canvas.setPaletteColor(1, ORANGE);
34+
}
35+
36+
int point = 0;
37+
int last_point = 0;
38+
39+
void loop() {
40+
canvas.scroll(4);
41+
float Distance = sensor.getDistance();
42+
Serial.printf("Distance: %.2fmm\r\n",Distance);
43+
point = map((int)Distance,20, 4500, 10, 180);
44+
canvas.drawLine(0, point, 4, last_point, 1);
45+
last_point = point;
46+
canvas.fillRect(0,190,320,50,BLACK);
47+
canvas.drawString(String(Distance)+"mm", 80, 190);
48+
canvas.pushSprite(0,0);
49+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
*******************************************************************************
3+
* Copyright (c) 2021 by M5Stack
4+
* Equipped with M5Core sample source code
5+
* 配套 M5Core 示例源代码
6+
* Visit the website for more information:https://docs.m5stack.com/en/unit/sonic.io
7+
* 获取更多资料请访问:https://docs.m5stack.com/zh_CN/unit/sonic.io
8+
*
9+
* describe: Ultrasonic. 超声波测距传感器 IO版本
10+
* date:2021/11/17
11+
*******************************************************************************
12+
Please connect to Port A,Display the distance measured by ultrasonic
13+
请连接端口B,显示超声波测量的距离
14+
*/
15+
16+
#include <M5Stack.h>
17+
#include <UNIT_SONIC.h>
18+
#include <M5GFX.h>
19+
M5GFX display;
20+
M5Canvas canvas(&display);
21+
22+
SONIC_IO sensor;
23+
24+
void setup() {
25+
M5.begin(); //Init M5Stack. 初始化M5Stack
26+
// begin(uint8_t trig = 26, uint8_t echo = 36);
27+
sensor.begin();
28+
display.begin();
29+
canvas.setColorDepth(1);
30+
canvas.setFont(&fonts::Orbitron_Light_24);
31+
canvas.setTextSize(1);
32+
canvas.createSprite(display.width(), display.height());
33+
canvas.setPaletteColor(1, ORANGE);
34+
}
35+
36+
int point = 0;
37+
int last_point = 0;
38+
39+
void loop() {
40+
canvas.scroll(4);
41+
float Distance = sensor.getDistance();
42+
Serial.printf("Distance: %.2fmm\r\n",Distance);
43+
point = map((int)Distance,20, 4500, 10, 180);
44+
canvas.drawLine(0, point, 4, last_point, 1);
45+
last_point = point;
46+
canvas.fillRect(0,190,320,50,BLACK);
47+
canvas.drawString(String(Distance)+"mm", 80, 190);
48+
canvas.pushSprite(0,0);
49+
}

0 commit comments

Comments
 (0)