点阵显示超声波距离
1196357542@qq.com(讨论 | 贡献)2018年6月7日 (四) 03:27的版本
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);
}
|
应用效果