c++在arduino中传递HIGH和LOW的 vector

标签 c++ arduino

我有一个 arduino,它通过 6 个独立的输入引脚监听 HIGH 和 LOW 的特定组合。我计划将监听与引脚分开,因为我会根据附加功能定期调用引脚。下面的代码概述了我到目前为止的设置

const int InPins[6] = {3,4,5,6,7,8}; 
int *PinsReadOut[6] = {0,0,0,0,0,0};
CntlLP = -1;

void setup() {
     Serial.begin(115200);
     for (int ii = 0, ii > 6, ii++) {
         pinMode(InPins[ii],INPUT)
     }
}

void loop(){
    switch(CntlLP)
         case -1:
            Serial.println("Waiting for Command");
            while (Serial.available()==0){}
            CntlLP=Serial.parseInt();
            break;
         case 1:
            ReadPins(PinsReadOut[6]);
            CntlLP = -1;
            break;
         case 2:
            if (PinsReadOut[0] == 1) && (PinsReadOut[1] == 1) {
                Serial.println("Received 1 & 2");
            }
            if (PinsReadOut[2] == 1) && (PinsReadOut[3] == 1) {
                Serial.println("Received 3 & 4");
            }
            if (PinsReadOut[4] == 1) && (PinsReadOut[5] == 1) {
                Serial.println("Received 5 & 6");
            }
            CntlLP = -1;
            break;
}

void ReadPins(int PinsReadOut[6]) {
     for (int ii = 0, ii > 6, ii++) {
         PinsReadOut[0] = digitalRead(InPins[ii])
     }
}

此代码抛出以下错误

warning: ISO C++ forbids comparison between pointer and integer [-fpermissive]

我认为也许可以将 if 语句更改为 == HIGH 但它会导致不同的错误

            if (PinsReadOut[0] == HIGH) && (PinsReadOut[1] == HIGH) {
                Serial.println("Received 1 & 2");
            }

导致这个错误

 #define HIGH 0x1

编辑:更新问题 我遇到了这个错误。这就是为什么我使用 *.我试着关注 arduino form 中的评论修复此错误。

warning: invalid conversion from 'int' to 'int*' [-fpermissive]

最佳答案

如何让它发挥作用

我建议您使用整数数组而不是指向整数的指针数组。这就是警告的来源。所以改变这个:

int *PinsReadOut[6] = {0,0,0,0,0,0};

对此:

int PinsReadOut[6] = {0,0,0,0,0,0};

此外,您在这里以错误的方式传递了数组:

ReadPins(PinsReadOut[6]);

你应该把它改成:

ReadPins(PinsReadOut);

为什么不起作用

首先 - 正如警告告诉您的那样 - 您正在尝试比较 int *int没有 -fpermissive 是不允许的编译器选项。因此,我建议不要在数组中存储指针,而是存储 int 值。

其次,您没有将数组传递给获取数组的函数,而是试图从该数组传递一个特定值,一个位于位置 7 的值。因为在函数调用的上下文中 array[6]表示 - 数组位置 7 的值(顺便说一句,它超出了数组的范围并导致未定义的行为),因为 C(++) 中的数组是从 0 开始的。

关于c++在arduino中传递HIGH和LOW的 vector ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50580237/

相关文章:

c++ - "this"不能作为函数使用

c++ - GSL Dilogarithm函数在C++中的使用

c++ - 如何使用 kissFFT 计算峰值?

c++ - 了解 C 链接器错误 : multiple definition

c++ - 对 vector 使用[] []运算符?

c++ - GetLastInputInfo 在 Node 插件中失败

arduino - 除法 float 时丢失数字 C++ (Arduino)

c++ - 在 C++ 中将日期时间字符串拆分为日期时间

arduino - 如何在 Arduino 中编译 AVR 代码?

c++ - 通过指针访问时的字节大小