“点阵显示超声波距离”的版本间的差异
(创建页面,内容为“{| style="width: 800px;" |- | <p style="color: #4F4E4E;font-size:220%">'''Sensor-Ultrasonic+点阵显示超声波距离'''</p> <br> 本示例给出了超声波传感…”) |
|||
(未显示同一用户的1个中间版本) | |||
第4行: | 第4行: | ||
<p style="color: #4F4E4E;font-size:220%">'''Sensor-Ultrasonic+点阵显示超声波距离'''</p> | <p style="color: #4F4E4E;font-size:220%">'''Sensor-Ultrasonic+点阵显示超声波距离'''</p> | ||
<br> | <br> | ||
− | + | 本示例给出了超声波传感器测得的距离在点阵上显示的方法,显示距离的单位为米,例如显示1.2,表示测得距离为1.2米 | |
<br> | <br> | ||
第20行: | 第20行: | ||
<p style="color: #E87E05;font-size:135%">代码</p> | <p style="color: #E87E05;font-size:135%">代码</p> | ||
<source lang="cpp"> | <source lang="cpp"> | ||
+ | #include <Microduino_Matrix.h> | ||
+ | #include <Microduino_Ultrasonic.h> | ||
+ | uint8_t Addr[MatrixPix_X][MatrixPix_Y] = { | ||
+ | {48} | ||
+ | }; | ||
+ | |||
+ | Ultrasonic Ultrasonic1(ULTRASONIC_ADDR_1); | ||
+ | Matrix display = Matrix(Addr, TYPE_S2); | ||
+ | |||
+ | uint8_t score_num[10][5] = { | ||
+ | 0x70, 0x50, 0x50, 0x50, 0x70, | ||
+ | 0x10, 0x10, 0x10, 0x10, 0x10, | ||
+ | 0x70, 0x10, 0x70, 0x40, 0x70, | ||
+ | 0x70, 0x10, 0x70, 0x10, 0x70, | ||
+ | 0x50, 0x50, 0x70, 0x10, 0x10, | ||
+ | 0x70, 0x40, 0x70, 0x10, 0x70, | ||
+ | 0x70, 0x40, 0x70, 0x50, 0x70, | ||
+ | 0x70, 0x10, 0x10, 0x10, 0x10, | ||
+ | 0x70, 0x50, 0x70, 0x50, 0x70, | ||
+ | 0x70, 0x50, 0x70, 0x10, 0x70, | ||
+ | }; | ||
+ | |||
+ | uint16_t Distance, Dis, lastDis; | ||
+ | |||
+ | uint8_t scoreMap[8]; | ||
+ | |||
+ | void dispScore(uint16_t num) | ||
+ | { | ||
+ | uint8_t score1, score2 , score3; | ||
+ | uint8_t tmp_x, i, j, m, n; | ||
+ | score1 = num / 100; | ||
+ | score2 = (num % 100) / 10; | ||
+ | score3 = num % 10; | ||
+ | m = score1 / 8; | ||
+ | n = score1 % 8; | ||
+ | |||
+ | memset(scoreMap, 0, sizeof(scoreMap)); | ||
+ | |||
+ | for (i = 0; i < m; i++) | ||
+ | scoreMap[i] = 0xff; | ||
+ | |||
+ | for (j = 0; j < n; j++) | ||
+ | scoreMap[m] |= (0x80 >> j); | ||
+ | |||
+ | for (i = 0; i < 5; i++) { | ||
+ | for (j = 0; j < 4; j++) { | ||
+ | if ((score_num[score2][i] & (0x80 >> j)) == (0x80 >> j)) | ||
+ | scoreMap[3 + i] |= 0x80 >> j; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | for (i = 0; i < 5; i++) { | ||
+ | for (j = 0; j < 4; j++) { | ||
+ | if ((score_num[score3][i] & (0x80 >> j)) == (0x80 >> j)) | ||
+ | scoreMap[3 + i] |= 0x80 >> (j + 4); | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | void updateScore() { | ||
+ | for (uint8_t pix = 0; pix < 8; pix++) { | ||
+ | for (uint8_t row = 0; row < 8; row++) { | ||
+ | if ((scoreMap[pix] >> row) & 0x01) | ||
+ | display.setLed(7 - row, pix, true); | ||
+ | else | ||
+ | display.setLed(7 - row, pix, false); | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | |||
+ | void setup() { | ||
+ | Serial.begin(9600); | ||
+ | Wire.begin(); | ||
+ | display.setBrightness(80); | ||
+ | display.clearDisplay(); | ||
+ | if (Ultrasonic1.begin()) { //如果超声波初始化成功 | ||
+ | Serial.println("Ultrasonic is online");//串口打印信息 | ||
+ | } | ||
+ | delay(2000); | ||
+ | } | ||
+ | |||
+ | void loop() { | ||
+ | Distance = Ultrasonic1.getDistance(); | ||
+ | Serial.println(Distance); | ||
+ | Dis = Distance / 100; | ||
+ | if (lastDis != Dis) { | ||
+ | lastDis = Dis; | ||
+ | dispScore(Dis); | ||
+ | updateScore(); | ||
+ | display.setLed(4, 7, true); | ||
+ | } | ||
+ | delay(2); | ||
+ | } | ||
</source> | </source> | ||
<br> | <br> | ||
|} | |} | ||
<br> | <br> | ||
− | |||
− | |||
2018年6月7日 (四) 03:41的最新版本
Sensor-Ultrasonic+点阵显示超声波距离
所需硬件
电路搭建 将Ultrasonic+传感器接到Hub的IIC引脚。
代码 #include <Microduino_Matrix.h>
#include <Microduino_Ultrasonic.h>
uint8_t Addr[MatrixPix_X][MatrixPix_Y] = {
{48}
};
Ultrasonic Ultrasonic1(ULTRASONIC_ADDR_1);
Matrix display = Matrix(Addr, TYPE_S2);
uint8_t score_num[10][5] = {
0x70, 0x50, 0x50, 0x50, 0x70,
0x10, 0x10, 0x10, 0x10, 0x10,
0x70, 0x10, 0x70, 0x40, 0x70,
0x70, 0x10, 0x70, 0x10, 0x70,
0x50, 0x50, 0x70, 0x10, 0x10,
0x70, 0x40, 0x70, 0x10, 0x70,
0x70, 0x40, 0x70, 0x50, 0x70,
0x70, 0x10, 0x10, 0x10, 0x10,
0x70, 0x50, 0x70, 0x50, 0x70,
0x70, 0x50, 0x70, 0x10, 0x70,
};
uint16_t Distance, Dis, lastDis;
uint8_t scoreMap[8];
void dispScore(uint16_t num)
{
uint8_t score1, score2 , score3;
uint8_t tmp_x, i, j, m, n;
score1 = num / 100;
score2 = (num % 100) / 10;
score3 = num % 10;
m = score1 / 8;
n = score1 % 8;
memset(scoreMap, 0, sizeof(scoreMap));
for (i = 0; i < m; i++)
scoreMap[i] = 0xff;
for (j = 0; j < n; j++)
scoreMap[m] |= (0x80 >> j);
for (i = 0; i < 5; i++) {
for (j = 0; j < 4; j++) {
if ((score_num[score2][i] & (0x80 >> j)) == (0x80 >> j))
scoreMap[3 + i] |= 0x80 >> j;
}
}
for (i = 0; i < 5; i++) {
for (j = 0; j < 4; j++) {
if ((score_num[score3][i] & (0x80 >> j)) == (0x80 >> j))
scoreMap[3 + i] |= 0x80 >> (j + 4);
}
}
}
void updateScore() {
for (uint8_t pix = 0; pix < 8; pix++) {
for (uint8_t row = 0; row < 8; row++) {
if ((scoreMap[pix] >> row) & 0x01)
display.setLed(7 - row, pix, true);
else
display.setLed(7 - row, pix, false);
}
}
}
void setup() {
Serial.begin(9600);
Wire.begin();
display.setBrightness(80);
display.clearDisplay();
if (Ultrasonic1.begin()) { //如果超声波初始化成功
Serial.println("Ultrasonic is online");//串口打印信息
}
delay(2000);
}
void loop() {
Distance = Ultrasonic1.getDistance();
Serial.println(Distance);
Dis = Distance / 100;
if (lastDis != Dis) {
lastDis = Dis;
dispScore(Dis);
updateScore();
display.setLed(4, 7, true);
}
delay(2);
}
|