Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Variables 在Arduino中使用字符串和整数寻址变量_Variables_Int_Arduino - Fatal编程技术网

Variables 在Arduino中使用字符串和整数寻址变量

Variables 在Arduino中使用字符串和整数寻址变量,variables,int,arduino,Variables,Int,Arduino,我想知道如何通过在Arduino中将字符串和int组合在一起来处理变量。 我使用了许多类型相同、名称几乎相同的变量。我只是在每个变量后面加上一个数字。 变量名称为,例如int sensorValue_1;int-sensorValue_2;等等 我希望写得少一些,因为我的代码变得太长了。 在处理变量时,我想这样写:sensorValue\uStrong>[+intVariable] 以下是我的意思的一个例子: int sensorIndex_1 = 1; int sensorIndex_2 =

我想知道如何通过在Arduino中将字符串和int组合在一起来处理变量。 我使用了许多类型相同、名称几乎相同的变量。我只是在每个变量后面加上一个数字。 变量名称为,例如int sensorValue_1;int-sensorValue_2;等等 我希望写得少一些,因为我的代码变得太长了。 在处理变量时,我想这样写:sensorValue\uStrong>[+intVariable]

以下是我的意思的一个例子:

int sensorIndex_1 = 1;
int sensorIndex_2 = 2;
int sensorIndex_3 = 3;

int sensorValue_1;
int sensorValue_2;
int sensorValue_3;


void setup()
{
  Serial.begin(9600);
}

void loop()
{
  doSomething(sensorIndex_1);
//doSomething(sensorIndex_2);
//doSomething(sensorIndex_3);
}

void doSomething(int sensorIndex)
{
  if(sensorIndex == 1)
  {
    Serial.print("Sensor 1: ");
    sensorValue_1 = analogRead(A1);
    Serial.println(sensorValue_1);
  }

  if(sensorIndex == 2)
  {
    Serial.print("Sensor 2: ");
    sensorValue_2 = analogRead(A2);
    Serial.println(sensorValue_2);
  }

  if(sensorIndex == 3)
  {
    Serial.print("Sensor 3: ");
    sensorValue_3 = analogRead(A3);
    Serial.println(sensorValue_3);
  }

  delay(1000);
}  
我想缩短doSomething()方法中的代码。 我想要这样的东西: 请注意“[+sensorIndex]

顺便说一下:如果可能的话,我想避免for循环。 在我的情况下,代码会变得太复杂。

我如何管理它?

您不需要使用
A1
A2
等来定义要读取的模拟管脚。一两个就行了

void doSomething(int sensorIndex)
{
      Serial.print("Sensor ");
      Serial.print(sensorIndex);
      Serial.print(": ");
      sensorValue = analogRead(sensorIndex);
      Serial.println(sensorValue);

      delay(1000);
}  

您不需要使用
A1
A2
等来定义要读取的模拟管脚。一两个就行了

void doSomething(int sensorIndex)
{
      Serial.print("Sensor ");
      Serial.print(sensorIndex);
      Serial.print(": ");
      sensorValue = analogRead(sensorIndex);
      Serial.println(sensorValue);

      delay(1000);
}  

User2461391有一个很好的开始,但我认为您需要的其他难题是:

int array1[3];
int array2[3];
int arrayx[3];


void setup()
{
}

void loop()
{
   int index=1;

   array1[2]=doSomething(2);
   arrayx[index]=doSomething(index);
   Serial.print("Sensor ");
   Serial.print(index);
   Serial.print(": ");
   Serial.println(arrayx[index]);         

   while(1);
}

int doSomething(int sensorIndex) // It probably makes more sense to return the value
{         
      return  (analogRead(sensorIndex));
}  

User2461391有一个很好的开始,但我认为您需要的其他难题是:

int array1[3];
int array2[3];
int arrayx[3];


void setup()
{
}

void loop()
{
   int index=1;

   array1[2]=doSomething(2);
   arrayx[index]=doSomething(index);
   Serial.print("Sensor ");
   Serial.print(index);
   Serial.print(": ");
   Serial.println(arrayx[index]);         

   while(1);
}

int doSomething(int sensorIndex) // It probably makes more sense to return the value
{         
      return  (analogRead(sensorIndex));
}