我在 Arduino Nano 项目中使用超声波传感器。我正在使用 NewPing.cpp 库文件,特别是 ping_median() 函数,如下所示:
// This is the top of NewPing.cpp:
#include "NewPing.h"
//
// There are other versions of ping routines in here, however this one averages the data samples so this is the one I'm hoping to use.
// ********************* THIS IS THE PART OF THE NEWPING.CPP THAT I WANT TO USE ***********************************
unsigned long NewPing::ping_median(uint8_t it, unsigned int max_cm_distance)
{
unsigned int uS[it], last;
uint8_t j, i = 0;
unsigned long t;
uS[0] = NO_ECHO;
if (max_cm_distance > 0) set_max_distance(max_cm_distance); // Call function to set a new max sensor distance.
while (i < it)
{
t = micros(); // Start ping timestamp.
last = ping(); // Send ping.
if (last != NO_ECHO)
{ // Ping in range, include as part of median.
if (i > 0)
{ // Don't start sort till second ping.
for (j = i; j > 0 && uS[j - 1] < last; j--) // Insertion sort loop.
uS[j] = uS[j - 1]; // Shift ping array to correct position for sort insertion.
} else j = 0; // First ping is sort starting point.
uS[j] = last; // Add last ping to array in sorted position.
i++; // Move to next ping.
}
else it--; // Ping out of range, skip and don't include as part of median.
if (i < it && micros() - t < PING_MEDIAN_DELAY)
delay((PING_MEDIAN_DELAY + t - micros()) >> 10); // Millisecond delay between pings.
}
// ************* I added this line ****************************
iterationCnt = it; // I would like to pass this value into my program loop
// ************************************************************
return (uS[it >> 1]); // Return the ping distance median.
}
// ********************* END OF THIS IS THE PART THAT I WANT TO USE ***********************************
到目前为止我尝试过的:
我将变量“uint8_t it”从本地转换为全局,方法是将函数重命名为:“unsigned long NewPing::ping_median(it, unsigned int max_cm_distance)”并将“uint8_t it”放在文件顶部。
我将“iterationCnt”声明为 uint8_t 以匹配“it”变量并将其放在 NewPing.cpp 的顶部。
我将 NewPing.cpp 和 NewPing.h 文件移至与我的程序相同的目录中,并将程序顶部的 #include 从“#include
我把所有东西都放回原来的样子...