打印不同类型(从同一阵列?)的最有效方法

时间:2018-11-21 13:27:12

标签: c++ arduino

我有一个小的草图,可以读取各种各样的传感器,所有传感器都可以归结为整数或布尔值。

每个循环我都希望将它们打印如下:

  

“ variableName; variableValue”   (例如“ distanceSenso; 432”)

当前看起来像

 int flame, distance, <etc>
 bool flame, touch;
    void loop(){
      noise = analogRead(SOUND_SENSOR);
      distance = analogRead(DISTANCE_SENSOR);
      ..etc..       
          Serial.print("flame;");
          Serial.println(flame);
          Serial.print("distance;");
          Serial.println(dist);
          Serial.print("motion;");
          Serial.println(motion);
          Serial.print("touch;");
          Serial.println(touch);
          Serial.print("noise;");
          Serial.println(noise);
          <etc for many more sensors>
    }

这很好,但效率不高。我想将每个变量添加到数组,并简单地更新每个循环的值。这样就可以使用一个简单的for循环来打印Serial.println中的每个数组元素。

但是,这是不可能的,因为我同时使用了int和bool变量。我可以创建两个单独的数组,也可以不使用for循环而保留当前的方法,但是我正在学习编码,并且想知道最有效的方法是什么。

所以,我的问题是:如何以最有效的方式打印上述语法中的每个变量?

2 个答案:

答案 0 :(得分:3)

就速度而言,代码中的顺序构造通常是最有效的。如果您希望提高可读性(例如,多少行代码),则循环会有所帮助,但可能会以其他方式降低效率。也就是说,要减少行数,请循环播放...

使用enumconst char数组和struct的组合。 (struct在这里是可选的,但是当与大量成员一起工作时,我经常使用它来提高可读性)

我没有您的环境,但是为了使用ANSI C进行说明,下面显示了枚举值如何将字符串描述和测量值绑定到同一struct实例中,从而允许在循环中报告结果:

enum {
    FLAME_SENSOR,    // enumerated from 0 to max_sensor
    DISTANCE_SENSOR,
    MOTION_SENSOR,
    TOUCH_SENSOR,
    SOUND_SENSOR,
    // add more sensors???
    MAX_SENSOR
};

typedef struct { // optional struct
    int val;
    char descr[20];
}PARAM;

const char str[MAX_SENSOR][20] = {"flame","distance","motion","touch","noise"};

//simulation prototype    
int analogRead(int type); 

int main(void)
{
    int i;
    PARAM p;
    char buf[20];
    int result = 0;

    for(i=0;i<MAX_SENSOR;i++)
    {
        p.val = analogRead(i);// for use with struct
        result = analogRead(i);//for use without struct
        if(p.val /*(or result)*/ == some error)
        {
            //handle error
        }
        strcpy(p.descr, str[i]);//for use without struct
        sprintf(buf, "%s: %d\n", p.descr, p.val);//for use with struct
        sprintf(buf, "%s: %d\n", str[i], result);//for use without struct
        printf(buf);
        sleep(10);  //10ms delay, For simulation only, to allow clock tick for rand() function 
    }           
    return 0;
}

//simple simulation of  analog read function
int analogRead(int type)
{
    int meas = 0;
    srand(clock());
    switch(type)  {
        case FLAME_SENSOR:
            // meas = read flame sensor instrument
            meas = rand()%10;
            break;
        case DISTANCE_SENSOR:
            // meas = read dist sensor instrument
            meas = rand()%10;
            break;
        case MOTION_SENSOR:
            // meas = read motion sensor instrument
            meas = rand()%10;
            break;
        case TOUCH_SENSOR:
            // meas = read touch sensor instrument
            meas = rand()%10;
            break;
        case SOUND_SENSOR:
            // meas = read sound sensor instrument
            meas = rand()%10;
            break;
        // add more case statements ???
        default:
            meas = some error
            break;
    }
    return meas;
}

答案 1 :(得分:2)

来自arduino reference

> analogRead()
>  
> Returns  
> int(0 to 1023)

所以无论如何,您都是整数。

通常,我创建一个临时结构,将需要迭代的所有不同数据和上下文。那么一个简单的循环就足够了。

struct data_s {
   const char *desc;
   int pin;
   enum data_type_s {
      DATA_INT,
      DATA_BOOL,
   } data_type;
} const datas[] = {
     { SOUND_SENSOR, "sound", DATA_INT },
     { DISTANCE_SENSOR, "distance", DATA_INT },
     { SOMEBOOLEAN_SENSOR, "someboolean", DATA_BOOL },
      ... and so on ...
};

for (size_t i = 0; i < sizeof(datas)/sizeof(*datas); ++i) {
     Serial.print(data[i].desc);
     Serial.print(";");
     const int readed_value = analogRead(data[i].pin);
     switch (data[i].type) {
     case DATA_BOOL:
          // custom int->bool conversion
          Serial.print(value ? "true" : "false");
          break;
     case DATA_INT:
          Serial.print(value);
          break;
     }
     Serial.print("\n");
}