点阵显示超声波距离

来自Microduino Wikipedia
1196357542@qq.com讨论 | 贡献2018年6月7日 (四) 03:41的版本
(差异) ←上一版本 | 最后版本 (差异) | 下一版本→ (差异)
跳转至: 导航搜索

Sensor-Ultrasonic+点阵显示超声波距离


本示例给出了超声波传感器测得的距离在点阵上显示的方法,显示距离的单位为米,例如显示1.2,表示测得距离为1.2米



所需硬件


电路搭建

将Ultrasonic+传感器接到Hub的IIC引脚。
将mCenter+(或者用Battery、Core、Hub堆叠在一起代替mCenter+,代替后核心选择core)通过MicroUSB数据线接入电脑,核心选择core+ 5V。初次使用请参考:Getting Started

代码

#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);
}





返回Sensor-Ultrasonic+ 界面