找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 3352|回复: 1
打印 上一主题 下一主题
收起左侧

基于arduino的VL53L0X模块的测速测距并且在蓝牙和lcd上显示

[复制链接]
跳转到指定楼层
楼主


单片机源程序如下:
  1. /* This example shows how to get single-shot range
  2. measurements from the VL53L0X. The sensor can optionally be
  3. configured with different ranging profiles, as described in
  4. the VL53L0X API user manual, to get better performance for
  5. a certain application. This code is based on the four
  6. "SingleRanging" examples in the VL53L0X API.

  7. The range readings are in units of mm. */

  8. /*
  9.   LiquidCrystal Library

  10. Demonstrates the use a 16x2 LCD display.  The LiquidCrystal
  11. library works with all LCD displays that are compatible with the
  12. Hitachi HD44780 driver. There are many of them out there, and you
  13. can usually tell them by the 16-pin interface.

  14. This sketch prints "Hello World!" to the LCD
  15. and shows the time.

  16.   The circuit:
  17. * LCD RS pin to digital pin 12
  18. * LCD Enable pin to digital pin 11
  19. * LCD D4 pin to digital pin 5
  20. * LCD D5 pin to digital pin 4
  21. * LCD D6 pin to digital pin 3
  22. * LCD D7 pin to digital pin 2
  23. * LCD R/W pin to ground
  24. * LCD VSS pin to ground
  25. * LCD VCC pin to 5V
  26. * 10K resistor:
  27. * ends to +5V and ground
  28. * wiper to LCD VO pin (pin 3)

  29. Library originally added 18 Apr 2008
  30. by David A. Mellis
  31. library modified 5 Jul 2009
  32. example added 9 Jul 2009
  33. by Tom Igoe
  34. modified 22 Nov 2010
  35. by Tom Igoe

  36. This example code is in the public domain.

  37. http://www.arduino.cc/en/Tutorial/LiquidCrystal
  38. */

  39. #include <Wire.h>
  40. #include <VL53L0X.h>
  41. #include <LiquidCrystal.h>

  42. #define LED 7

  43. LiquidCrystal lcd(12, 11, 5, 4, 3, 2);// initialize the library with the numbers of the interface pins

  44. VL53L0X sensor;

  45. int t = 0;
  46. int spd = 0;
  47. int distance;
  48. int updistance;
  49. /*int rectification;
  50. int rectification1;

  51. //int table[28] = {};  3cm~31cm*/


  52. // Uncomment this line to use long range mode. This
  53. // increases the sensitivity of the sensor and extends its
  54. // potential range, but increases the likelihood of getting
  55. // an inaccurate reading because of reflections from objects
  56. // other than the intended target. It works best in dark
  57. // conditions.

  58. //#define LONG_RANGE


  59. // Uncomment ONE of these two lines to get
  60. // - higher speed at the cost of lower accuracy OR
  61. // - higher accuracy at the cost of lower speed

  62. //#define HIGH_SPEED
  63. #define HIGH_ACCURACY


  64. void setup()
  65. {
  66.   pinMode(LED,OUTPUT);
  67.   Serial.begin(9600);
  68.   Wire.begin();

  69.   sensor.init();
  70.   sensor.setTimeout(500);

  71.    // set up the LCD's number of columns and rows:
  72.   lcd.begin(16, 2);

  73. #if defined LONG_RANGE
  74.   // lower the return signal rate limit (default is 0.25 MCPS)
  75.   sensor.setSignalRateLimit(0.1);
  76.   // increase laser pulse periods (defaults are 14 and 10 PCLKs)
  77.   sensor.setVcselPulsePeriod(VL53L0X::VcselPeriodPreRange, 18);
  78.   sensor.setVcselPulsePeriod(VL53L0X::VcselPeriodFinalRange, 14);
  79. #endif

  80. #if defined HIGH_SPEED
  81.   // reduce timing budget to 20 ms (default is about 33 ms)
  82.   sensor.setMeasurementTimingBudget(20000);
  83. #elif defined HIGH_ACCURACY
  84.   // increase timing budget to 200 ms
  85.   sensor.setMeasurementTimingBudget(200000);
  86. #endif


  87.   lcd.clear();//clear lcd
  88.   lcd.setCursor(0, 0);//get location (it is ok outside screen)0-15 inside screen
  89.   lcd.print(" AHU      XIN GE");
  90.   lcd.setCursor(0, 1);//get location (it is ok outside screen)0-15 inside screen
  91.   lcd.print("distance sensor");
  92.   delay(6000);
  93.   
  94. }

  95. void loop()
  96. {


  97.   
  98. //********************************************************************get distance start***************************************************************
  99.   
  100.   updistance = distance;
  101.   distance = sensor.readRangeSingleMillimeters();//get distance
  102. //********************************************************************get distance end********************************************************************


  103. //********************************************************************distance rectify & LED start**************************************************************                                                               
  104.   distance = distance-32;
  105.   if(distance >= 21 && distance <= 55)
  106.   {
  107.      distance = distance* 0.6050 + 13.3228 ;//matlab a = 0.6050    b = 13.3228
  108.      digitalWrite(LED,!digitalRead(LED));
  109.   }
  110.   else if(distance > 55 && distance <= 276)
  111.   {
  112.     distance = distance*1.0312  - 7.2051;//  1.0312   -7.2051
  113.     digitalWrite(LED,LOW);//LED turn off
  114.   }
  115.   else if(distance >= 277 && distance <= 295)
  116.   {
  117.      distance = distance* 1.0436 -8.0835 ;//matlab a = 1.0436  b =   -8.0835
  118.      digitalWrite(LED,!digitalRead(LED));//led twinkle
  119.   }
  120.   else
  121.   {
  122.     digitalWrite(LED,HIGH);//LED turn on
  123.   }
  124.   

  125.   //distance = distance*1.0296 - 6.8681;//matlab a = 1.0296 ,b = -6.8681
  126. //********************************************************************distance rectify & LED end**************************************************************


  127. //********************************************************************show distance start***************************************************************
  128.   lcd.clear();//clear lcd
  129.   lcd.setCursor(0, 0);//get location (it is ok outside screen)0-15 inside screen
  130.   lcd.print("distance:");
  131.   lcd.print(distance);
  132.   lcd.setCursor(14, 0);
  133.   lcd.print("mm");
  134.   
  135.   Serial.print("Distance: ");
  136.   Serial.print(distance);
  137.   Serial.print("mm");
  138.   if (sensor.timeoutOccurred())
  139.   {
  140.     Serial.print(" TIMEOUT");
  141.     lcd.print("TIMEOUT");
  142.   }
  143.   Serial.print("\t");
  144. //********************************************************************show distance end****************************************************************


  145. //********************************************************************show times start***************************************************************
  146.   lcd.setCursor(0, 1);//get location (it is ok outside screen)0-15 inside screen
  147.   lcd.print("T:");
  148.   lcd.print(t);
  149.   
  150.   t++;
  151.   if(t == 1000)
  152.   {
  153.     t = 0;
  154.   }
  155. //********************************************************************show times end***************************************************************


  156. //********************************************************************show speed start***************************************************************
  157.   spd = (updistance - distance)/0.213;
  158.   lcd.setCursor(6, 1);
  159.   lcd.print("V:");
  160.   lcd.print(spd);
  161.   lcd.setCursor(12, 1);
  162.   lcd.print("mm/s");
  163.   Serial.print("  ");
  164.   Serial.print("speed:");
  165.   Serial.print(spd);
  166.   Serial.print("mm/s \n");
  167. //********************************************************************show speed end***************************************************************

  168.   
  169.   
  170. }
复制代码

所有资料51hei提供下载:


测距.zip (97.1 KB, 下载次数: 16)
VL53L0X资料.rar (7.28 MB, 下载次数: 6)


评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:363596 发表于 2018-7-12 01:23 | 只看该作者
* LCD RS pin to digital pin 12 * LCD Enable pin to digital pin 11 * LCD D4 pin to digital pin 5 * LCD D5 pin to digital pin 4 * LCD D6 pin to digital pin 3 * LCD D7 pin to digital pin 2 * LCD R/W pin to ground * LCD VSS pin to ground * LCD VCC pin to 5V * 10K resistor: * ends to +5V and ground * wiper to LCD VO pin (pin 3)
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表