Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/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
C++ 使用Arduino中的字符串和int访问/寻址变量_C++_C_String_Arduino_Sensors - Fatal编程技术网

C++ 使用Arduino中的字符串和int访问/寻址变量

C++ 使用Arduino中的字符串和int访问/寻址变量,c++,c,string,arduino,sensors,C++,C,String,Arduino,Sensors,在Arduino中已经有一个关于用字符串处理变量的问题,但是给出的答案不适用于我的问题 我有多个传感器(大约14个,数量可能会增加)连接到我的Arduino,我还有继电器、引擎和RFID。我正在创建一个函数,用于检查所有传感器是否处于活动状态 其基本思想是: #define Sensor_1 2 #define Sensor_2 3 #define Sensor_3 4 #define Sensor_4 5 #define Sensor_5 6 int checkSensors(){

在Arduino中已经有一个关于用字符串处理变量的问题,但是给出的答案不适用于我的问题

我有多个传感器(大约14个,数量可能会增加)连接到我的Arduino,我还有继电器、引擎和RFID。我正在创建一个函数,用于检查所有传感器是否处于活动状态

其基本思想是:

#define Sensor_1 2
#define Sensor_2 3
#define Sensor_3 4
#define Sensor_4 5
#define Sensor_5 6

int checkSensors(){
    int all_active = 0;
    int num_sens = 5; 
    int n;
    int active_sens = 0; 

    for(n= 1; n <= num_sens; n++) {
        if( !digitalRead("Sensor_" + n)) {
            active_sens= active_sens+ 1;    
        }
        else {
            all_active = 0;
            return ( all_active);
        }
    }

    if(active_sens== num_sens) {
        all_active = 1;
        return(all_active);
    }
}
#定义传感器1 2
#定义传感器2 3
#定义传感器3 4
#定义传感器4 5
#定义传感器5 6
int checkSensors(){
int all_active=0;
int num_sens=5;
int n;
int active_sens=0;

对于C中的(n=1;n),这条线不起作用

if( !digitalRead("Sensor_" + n))
不能在C中生成这样的字符串。因为您没有发布函数
digitalRead()
,我假定它需要
char*
类型,这里是一个字符串,在C中可以这样生成

char senstr[50];
sprintf(senstr, "Sensor_%d", n);
...
if (!digitalRead(senstr)) { ...

作为一个次要问题,请习惯于从
0
迭代循环。您将
1
添加到与人类的接口。

在C中,这一行将不起作用

if( !digitalRead("Sensor_" + n))
不能在C中生成这样的字符串。因为您没有发布函数
digitalRead()
,我假定它需要
char*
类型,这里是一个字符串,在C中可以这样生成

char senstr[50];
sprintf(senstr, "Sensor_%d", n);
...
if (!digitalRead(senstr)) { ...

作为一个次要问题,请习惯于从
0
迭代循环。您将
1
添加到与人类的接口。

在C中,这一行将不起作用

if( !digitalRead("Sensor_" + n))
不能在C中生成这样的字符串。因为您没有发布函数
digitalRead()
,我假定它需要
char*
类型,这里是一个字符串,在C中可以这样生成

char senstr[50];
sprintf(senstr, "Sensor_%d", n);
...
if (!digitalRead(senstr)) { ...

作为一个次要问题,请习惯于从
0
迭代循环。您将
1
添加到与人类的接口。

在C中,这一行将不起作用

if( !digitalRead("Sensor_" + n))
不能在C中生成这样的字符串。因为您没有发布函数
digitalRead()
,我假定它需要
char*
类型,这里是一个字符串,在C中可以这样生成

char senstr[50];
sprintf(senstr, "Sensor_%d", n);
...
if (!digitalRead(senstr)) { ...

作为一个次要问题,请习惯于从
0
迭代循环。您添加了
1
以与人交互。

您的代码有一些问题。首先,您无法通过动态使用变量名称的字符串来获取变量值或定义。在C中,这种方式不起作用。最简单的方法是o使用一个数组,然后对其进行索引。为了使这项工作正常,我已将for循环从0改为计数,因为数组从0开始索引。我更改了所有活动逻辑,假设在某个时间点以后,您想知道有多少个传感器处于活动状态,而不仅仅是它们是否都处于活动状态。如果您不想那么,您的逻辑也比需要的复杂得多。它可能在for循环的末尾返回1,因为所有人都必须通过测试才能到达那里

#define Sensor_1 2
#define Sensor_2 3
#define Sensor_3 4
#define Sensor_4 5
#define Sensor_5 6
int sensors[] = {Sensor_1, Sensor_2, Sensor_3, Sensor_4, Sensor_5};

int checkSensors(){
    int all_active = 1;
    int num_sens = 5; 
    int n;
    int active_sens = 0; 

    for(n= 0; n < num_sens; n++){
        if( !digitalRead(sensors[n])){
            active_sens= active_sens+ 1;    
        }
        else {
            all_active = 0;
        }
     }
     return all_active;
}
#定义传感器1 2
#定义传感器2 3
#定义传感器3 4
#定义传感器4 5
#定义传感器5 6
int sensors[]={Sensor_1、Sensor_2、Sensor_3、Sensor_4、Sensor_5};
int checkSensors(){
int all_active=1;
int num_sens=5;
int n;
int active_sens=0;
对于(n=0;n
您的代码存在一些问题。首先,您无法通过动态使用变量名称字符串来获取变量或定义的值。在C语言中,这种方法不起作用。最简单的方法是使用数组,然后对其进行索引。为了使这一方法正常工作,我将for循环从0改为0e数组从0开始索引。我更改了all_active逻辑,假设在某个时间点以后,您想知道有多少个传感器处于活动状态,而不仅仅是它们是否都处于活动状态。如果您不想知道,那么您的逻辑也比需要的复杂。它可能只在for循环结束时返回1,因为必须通过考试才能到达那里

#define Sensor_1 2
#define Sensor_2 3
#define Sensor_3 4
#define Sensor_4 5
#define Sensor_5 6
int sensors[] = {Sensor_1, Sensor_2, Sensor_3, Sensor_4, Sensor_5};

int checkSensors(){
    int all_active = 1;
    int num_sens = 5; 
    int n;
    int active_sens = 0; 

    for(n= 0; n < num_sens; n++){
        if( !digitalRead(sensors[n])){
            active_sens= active_sens+ 1;    
        }
        else {
            all_active = 0;
        }
     }
     return all_active;
}
#定义传感器1 2
#定义传感器2 3
#定义传感器3 4
#定义传感器4 5
#定义传感器5 6
int sensors[]={Sensor_1、Sensor_2、Sensor_3、Sensor_4、Sensor_5};
int checkSensors(){
int all_active=1;
int num_sens=5;
int n;
int active_sens=0;
对于(n=0;n
您的代码存在一些问题。首先,您无法通过动态使用变量名称字符串来获取变量或定义的值。在C语言中,这种方法不起作用。最简单的方法是使用数组,然后对其进行索引。为了使这一方法正常工作,我将for循环从0改为0e数组从0开始索引。我更改了all_active逻辑,假设在某个时间点以后,您想知道有多少个传感器处于活动状态,而不仅仅是它们是否都处于活动状态。如果您不想知道,那么您的逻辑也比需要的复杂。它可能只在for循环结束时返回1,因为必须通过考试才能到达那里

#define Sensor_1 2
#define Sensor_2 3
#define Sensor_3 4
#define Sensor_4 5
#define Sensor_5 6
int sensors[] = {Sensor_1, Sensor_2, Sensor_3, Sensor_4, Sensor_5};

int checkSensors(){
    int all_active = 1;
    int num_sens = 5; 
    int n;
    int active_sens = 0; 

    for(n= 0; n < num_sens; n++){
        if( !digitalRead(sensors[n])){
            active_sens= active_sens+ 1;    
        }
        else {
            all_active = 0;
        }
     }
     return all_active;
}
#定义传感器1 2
#定义传感器2 3
#定义传感器3 4
#定义传感器4 5
#定义传感器5 6
int sensors[]={Sensor_1、Sensor_2、Sensor_3、Sensor_4、Sensor_5};
int checkSensors(){
int all_active=1;
int num_sens=5;
int n;
int active_sens=0;
对于(n=0;n
您的代码存在一些问题。首先,您无法通过动态使用str获取变量值或define