Arrays 我的代码无法打印出数组索引0的正确值 #包括 #定义声纳数量6//传感器数量。 #定义最大距离500//ping的最大距离(以厘米为单位)。 新建声纳[sonar_NUM]=//传感器对象阵列。每个传感器的触发引脚、回波引脚和到ping的最大距离。 { NewPing(1,2,最大距离),//1右下角 NewPing(3,4,最大距离),//2右上角 NewPing(5,6,最大距离),//3底部中间 NewPing(7,8,最大距离)//4上中 NewPing(9,10,最大距离),//5左下角 NewPing(11,12,最大距离),//6左上角 }; 国际区; int myDist[6]; int m; int-minIndex; int-minValue; 无效设置() { Serial.begin(115200);//以115200波特打开串行监视器以查看ping结果。 } void循环() { 对于(uint8_t i=0;i
我的代码试图从超声波传感器中找到值并将它们存储在一个数组中。它工作正常,只是当我试图打印数组的0索引时,我的第一个传感器不会打印出一个值。我使用了另一个代码,传感器功能正常,接线正常。我猜我存储它的方式是错误的。我阵列的所有其他索引都从正确的传感器打印出适当的值,但数字1或索引0没有打印出来。索引0的输出是什么 它是否总是相同的数字,例如,Arrays 我的代码无法打印出数组索引0的正确值 #包括 #定义声纳数量6//传感器数量。 #定义最大距离500//ping的最大距离(以厘米为单位)。 新建声纳[sonar_NUM]=//传感器对象阵列。每个传感器的触发引脚、回波引脚和到ping的最大距离。 { NewPing(1,2,最大距离),//1右下角 NewPing(3,4,最大距离),//2右上角 NewPing(5,6,最大距离),//3底部中间 NewPing(7,8,最大距离)//4上中 NewPing(9,10,最大距离),//5左下角 NewPing(11,12,最大距离),//6左上角 }; 国际区; int myDist[6]; int m; int-minIndex; int-minValue; 无效设置() { Serial.begin(115200);//以115200波特打开串行监视器以查看ping结果。 } void循环() { 对于(uint8_t i=0;i,arrays,arduino,arduino-uno,arduino-ide,Arrays,Arduino,Arduino Uno,Arduino Ide,我的代码试图从超声波传感器中找到值并将它们存储在一个数组中。它工作正常,只是当我试图打印数组的0索引时,我的第一个传感器不会打印出一个值。我使用了另一个代码,传感器功能正常,接线正常。我猜我存储它的方式是错误的。我阵列的所有其他索引都从正确的传感器打印出适当的值,但数字1或索引0没有打印出来。索引0的输出是什么 它是否总是相同的数字,例如,0?还是一个随机数?总是积极的?只是想帮助您调试。在math()函数中: int-minIndex 应该是 #include <NewPing.h>
0
?还是一个随机数?总是积极的?只是想帮助您调试。在math()函数中:
int-minIndex
应该是
#include <NewPing.h>
#define SONAR_NUM 6 // Number of sensors.
#define MAX_DISTANCE 500 // Maximum distance (in cm) to ping.
NewPing sonar[SONAR_NUM] = // Sensor object array. Each sensor's trigger pin, echo pin, and max distance to ping.
{
NewPing(1, 2, MAX_DISTANCE), //1 Bottom right
NewPing(3, 4, MAX_DISTANCE), //2 Top right
NewPing(5, 6, MAX_DISTANCE), //3 Bottom middle
NewPing(7, 8, MAX_DISTANCE), //4 Top middle
NewPing(9, 10, MAX_DISTANCE), //5 Bottom left
NewPing(11, 12, MAX_DISTANCE), //6 Top left
};
int Dist;
int myDist[6];
int m;
int minIndex;
int minValue;
void setup()
{
Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results.
}
void loop()
{
for (uint8_t i = 0; i < SONAR_NUM; i++) // Loop through each sensor and store results.
{
delay(30); // Wait 30ms between pings . 29ms should be the shortest delay between pings.
myDist[i] = sonar[i].ping_cm();
}
math(); //go to math function
}
void math()
{
int minIndex;
int myDist_length = sizeof(myDist) / sizeof(myDist[0]); //find length of array
int minValue = myDist[minIndex]; //min value = value from min index of array
for(byte j = 1; j < myDist_length; j++){ //for loop lasts length of array
if(myDist[j] < minValue) { //if loop finds out if value is smaller than previous smallest
minValue = myDist[j];
minIndex = j;
}
}
//Serial.print(minValue);
//Serial.print(", ");
Serial.print(myDist[0]);
Serial.println();
}
您需要给minIndex一个初始值。你假设它从零开始。这不是一个安全的假设,因为它是一个局部变量。您需要将其显式设置为0。它始终为0,其他索引传递正确的数字。我不记得Arduino是否将数组元素初始化为0。我想知道数据中的0来自何处-从传感器轮询数据或刚刚初始化且从未覆盖。您是否可以手动将[0]值设置为一些不太可能的数字,如1111,并查看即使在轮询传感器后它是否仍保持不变?为什么j从1开始而不是从j=0开始?我还注意到您有一个同名的全局变量。它本来是以零开始的,但你对它进行了阴影处理,从未使用过它。在作用域中有多个同名变量通常是不明智的。这会引起各种各样的混乱。要么去掉全局的,要么去掉局部的。
int minIndex = 0;