代码拉取完成,页面将自动刷新
/*!
* @file DFRobot_IRDMSensor.cpp
* @brief DFRobot's Infrared Sensor
* @n Distance Measuring Sensor Unit
* @copyright Copyright (c) 2010 DFRobot Co.Ltd (http://www.dfrobot.com)
* @license The MIT License (MIT)
* @author [PengKaixing]kaixing.peng@dfrobot.com)
* @version V1.0.0
* @date 2022-9-3
* @url https://github.com/DFRobot/DFRobot_IRDMSensor
*/
#include <DFRobot_IRDMSensor.h>
float DFRobot_IRDMSensor::getSensorValue(void)
{
float minNum, maxNum, meanN, t;
static float arr[7] = {0};
uint16_t i=0,sensorValue, flagMin=0, flagMax=0;
while(i<7){
sensorValue = analogRead(_pin);
if(sensorValue>=240&&sensorValue<=512){
arr[i]=sensorValue;
i++;
}
}
maxNum=arr[0];
minNum=arr[0];
for(i=0; i<7; i++){
if(arr[i]<minNum){
minNum = arr[i];
flagMin = i;
}
if(arr[i]>maxNum){
maxNum = arr[i];
flagMax = i;
}
}
t = arr[0];
arr[0] = arr[flagMin];
arr[flagMin] = t;
t = arr[6];
arr[6] = arr[flagMax];
arr[flagMax] = t;
meanN=(arr[1]+arr[2]+arr[3]+arr[4]+arr[5])/5;
return meanN;
}
float DFRobot_IRDMSensor::getDistance(void)
{
uint8_t i;
float distance=0,SensorValue=0;
for(i=0;i<40;i++){
SensorValue=getSensorValue();
if(SensorValue>313.8){
distance+=28900/(SensorValue-218.0);
}
else{
distance+=28800/(SensorValue-217.5);
}
}
distance/=40;
return distance;
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。