單片機源程序如下:
- /* This example shows how to get single-shot range
- measurements from the VL53L0X. The sensor can optionally be
- configured with different ranging profiles, as described in
- the VL53L0X API user manual, to get better performance for
- a certain application. This code is based on the four
- "SingleRanging" examples in the VL53L0X API.
- The range readings are in units of mm. */
- /*
- LiquidCrystal Library
- Demonstrates the use a 16x2 LCD display. The LiquidCrystal
- library works with all LCD displays that are compatible with the
- Hitachi HD44780 driver. There are many of them out there, and you
- can usually tell them by the 16-pin interface.
- This sketch prints "Hello World!" to the LCD
- and shows the time.
- The circuit:
- * 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)
- Library originally added 18 Apr 2008
- by David A. Mellis
- library modified 5 Jul 2009
- example added 9 Jul 2009
- by Tom Igoe
- modified 22 Nov 2010
- by Tom Igoe
- This example code is in the public domain.
- http://www.arduino.cc/en/Tutorial/LiquidCrystal
- */
- #include <Wire.h>
- #include <VL53L0X.h>
- #include <LiquidCrystal.h>
- #define LED 7
- LiquidCrystal lcd(12, 11, 5, 4, 3, 2);// initialize the library with the numbers of the interface pins
- VL53L0X sensor;
- int t = 0;
- int spd = 0;
- int distance;
- int updistance;
- /*int rectification;
- int rectification1;
- //int table[28] = {}; 3cm~31cm*/
- // Uncomment this line to use long range mode. This
- // increases the sensitivity of the sensor and extends its
- // potential range, but increases the likelihood of getting
- // an inaccurate reading because of reflections from objects
- // other than the intended target. It works best in dark
- // conditions.
- //#define LONG_RANGE
- // Uncomment ONE of these two lines to get
- // - higher speed at the cost of lower accuracy OR
- // - higher accuracy at the cost of lower speed
- //#define HIGH_SPEED
- #define HIGH_ACCURACY
- void setup()
- {
- pinMode(LED,OUTPUT);
- Serial.begin(9600);
- Wire.begin();
- sensor.init();
- sensor.setTimeout(500);
- // set up the LCD's number of columns and rows:
- lcd.begin(16, 2);
- #if defined LONG_RANGE
- // lower the return signal rate limit (default is 0.25 MCPS)
- sensor.setSignalRateLimit(0.1);
- // increase laser pulse periods (defaults are 14 and 10 PCLKs)
- sensor.setVcselPulsePeriod(VL53L0X::VcselPeriodPreRange, 18);
- sensor.setVcselPulsePeriod(VL53L0X::VcselPeriodFinalRange, 14);
- #endif
- #if defined HIGH_SPEED
- // reduce timing budget to 20 ms (default is about 33 ms)
- sensor.setMeasurementTimingBudget(20000);
- #elif defined HIGH_ACCURACY
- // increase timing budget to 200 ms
- sensor.setMeasurementTimingBudget(200000);
- #endif
- lcd.clear();//clear lcd
- lcd.setCursor(0, 0);//get location (it is ok outside screen)0-15 inside screen
- lcd.print(" AHU XIN GE");
- lcd.setCursor(0, 1);//get location (it is ok outside screen)0-15 inside screen
- lcd.print("distance sensor");
- delay(6000);
-
- }
- void loop()
- {
-
- //********************************************************************get distance start***************************************************************
-
- updistance = distance;
- distance = sensor.readRangeSingleMillimeters();//get distance
- //********************************************************************get distance end********************************************************************
- //********************************************************************distance rectify & LED start**************************************************************
- distance = distance-32;
- if(distance >= 21 && distance <= 55)
- {
- distance = distance* 0.6050 + 13.3228 ;//matlab a = 0.6050 b = 13.3228
- digitalWrite(LED,!digitalRead(LED));
- }
- else if(distance > 55 && distance <= 276)
- {
- distance = distance*1.0312 - 7.2051;// 1.0312 -7.2051
- digitalWrite(LED,LOW);//LED turn off
- }
- else if(distance >= 277 && distance <= 295)
- {
- distance = distance* 1.0436 -8.0835 ;//matlab a = 1.0436 b = -8.0835
- digitalWrite(LED,!digitalRead(LED));//led twinkle
- }
- else
- {
- digitalWrite(LED,HIGH);//LED turn on
- }
-
- //distance = distance*1.0296 - 6.8681;//matlab a = 1.0296 ,b = -6.8681
- //********************************************************************distance rectify & LED end**************************************************************
- //********************************************************************show distance start***************************************************************
- lcd.clear();//clear lcd
- lcd.setCursor(0, 0);//get location (it is ok outside screen)0-15 inside screen
- lcd.print("distance:");
- lcd.print(distance);
- lcd.setCursor(14, 0);
- lcd.print("mm");
-
- Serial.print("Distance: ");
- Serial.print(distance);
- Serial.print("mm");
- if (sensor.timeoutOccurred())
- {
- Serial.print(" TIMEOUT");
- lcd.print("TIMEOUT");
- }
- Serial.print("\t");
- //********************************************************************show distance end****************************************************************
- //********************************************************************show times start***************************************************************
- lcd.setCursor(0, 1);//get location (it is ok outside screen)0-15 inside screen
- lcd.print("T:");
- lcd.print(t);
-
- t++;
- if(t == 1000)
- {
- t = 0;
- }
- //********************************************************************show times end***************************************************************
- //********************************************************************show speed start***************************************************************
- spd = (updistance - distance)/0.213;
- lcd.setCursor(6, 1);
- lcd.print("V:");
- lcd.print(spd);
- lcd.setCursor(12, 1);
- lcd.print("mm/s");
- Serial.print(" ");
- Serial.print("speed:");
- Serial.print(spd);
- Serial.print("mm/s \n");
- //********************************************************************show speed end***************************************************************
-
-
- }
復制代碼
所有資料51hei提供下載:
測距.zip
(97.1 KB, 下載次數: 16)
2018-7-12 01:20 上傳
點擊文件名下載附件
VL53L0X資料.rar
(7.28 MB, 下載次數: 6)
2018-7-12 01:20 上傳
點擊文件名下載附件
|