Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/67.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/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 根据if语句更改数组值_C_Arduino - Fatal编程技术网

C 根据if语句更改数组值

C 根据if语句更改数组值,c,arduino,C,Arduino,我需要在arduino的循环函数中不断地向串行输出发送4条消息,每条消息包含8字节的数据。发送的信息数据取决于主板上的两个数字输入。下面是它的外观: uint8_t msg_1[8] uint8_t msg_2[8] uint8_t msg_3[8] uint8_t msg_4[8] uint8_t first_1[] = {0x89, 0x80, 0xF8, 0x73, 0x00, 0x00, 0xCF, 0xFF}; uint8_t first_2[] = {0x89, 0x80, 0xF

我需要在arduino的循环函数中不断地向串行输出发送4条消息,每条消息包含8字节的数据。发送的信息数据取决于主板上的两个数字输入。下面是它的外观:

uint8_t msg_1[8]
uint8_t msg_2[8]
uint8_t msg_3[8]
uint8_t msg_4[8]

uint8_t first_1[] = {0x89, 0x80, 0xF8, 0x73, 0x00, 0x00, 0xCF, 0xFF};
uint8_t first_2[] = {0x89, 0x80, 0xF8, 0x73, 0x00, 0x00, 0xCF, 0xFF};
uint8_t first_3[] = {0x89, 0x80, 0xF8, 0x73, 0x00, 0x00, 0xCF, 0xFF};
uint8_t first_4[] = {0x89, 0x80, 0xF8, 0x73, 0x00, 0x00, 0xCF, 0xFF};

uint8_t second_1[] = {0x89, 0x80, 0xF8, 0x73, 0x00, 0x00, 0xCF, 0xFF};
uint8_t second_2[] = {0x89, 0x80, 0xF8, 0x73, 0x00, 0x00, 0xCF, 0xFF};
uint8_t second_3[] = {0x89, 0x80, 0xF8, 0x73, 0x00, 0x00, 0xCF, 0xFF};
uint8_t second_4[] = {0x89, 0x80, 0xF8, 0x73, 0x00, 0x00, 0xCF, 0xFF};


void loop (void){

    if(input1 == HIGH && input2 == LOW){
        msg = first; // <====== how to assign it?
    }
    else if (input1 == LOW && input2 == HIGH)
        msg = second; // <===== how to assign it?
}

serial.write(msg_1,8)
serial.write(msg_2,8)
serial.write(msg_3,8)
serial.write(msg_4,8)
}
uint8\u t msg\u 1[8]
uint8_t msg_2[8]
uint8_t msg_3[8]
uint8_t msg_4[8]
uint8_t first_1[]={0x89、0x80、0xF8、0x73、0x00、0x00、0xCF、0xFF};
uint8_t first_2[]={0x89,0x80,0xF8,0x73,0x00,0x00,0xCF,0xFF};
uint8_t first_3[]={0x89、0x80、0xF8、0x73、0x00、0x00、0xCF、0xFF};
uint8_t first_4[]={0x89、0x80、0xF8、0x73、0x00、0x00、0xCF、0xFF};
uint8_t second_1[]={0x89、0x80、0xF8、0x73、0x00、0x00、0xCF、0xFF};
uint8_t second_2[]={0x89,0x80,0xF8,0x73,0x00,0x00,0xCF,0xFF};
uint8_t second_3[]={0x89、0x80、0xF8、0x73、0x00、0x00、0xCF、0xFF};
uint8_t second_4[]={0x89、0x80、0xF8、0x73、0x00、0x00、0xCF、0xFF};
无效循环(无效){
如果(输入1==高和输入2==低){

msg=first;//使用指向值的指针:

uint8_t *msg_1;
uint8_t *msg_2;
uint8_t *msg_3;
uint8_t *msg_4;

uint8_t first_1[] = {0x89, 0x80, 0xF8, 0x73, 0x00, 0x00, 0xCF, 0xFF};
uint8_t first_2[] = {0x89, 0x80, 0xF8, 0x73, 0x00, 0x00, 0xCF, 0xFF};
uint8_t first_3[] = {0x89, 0x80, 0xF8, 0x73, 0x00, 0x00, 0xCF, 0xFF};
uint8_t first_4[] = {0x89, 0x80, 0xF8, 0x73, 0x00, 0x00, 0xCF, 0xFF};  

uint8_t second_1[] = {0x89, 0x80, 0xF8, 0x73, 0x00, 0x00, 0xCF, 0xFF};
uint8_t second_2[] = {0x89, 0x80, 0xF8, 0x73, 0x00, 0x00, 0xCF, 0xFF};
uint8_t second_3[] = {0x89, 0x80, 0xF8, 0x73, 0x00, 0x00, 0xCF, 0xFF};
uint8_t second_4[] = {0x89, 0x80, 0xF8, 0x73, 0x00, 0x00, 0xCF, 0xFF}; 


void loop (void){

  if(input1 == HIGH && input2 == LOW){
    msg_1 = first_1; 
    msg_2 = first_2; 
    msg_3 = first_3; 
    msg_4 = first_4; 
  }
  else if (input1 == LOW && input2 == HIGH) {
    msg_1 = second_1;
    msg_2 = second_2;
    msg_3 = second_3;
    msg_4 = second_4;
  }

  serial.write(msg_1,8)
  serial.write(msg_2,8)
  serial.write(msg_3,8)
  serial.write(msg_4,8)
}

您可以为消息定义一个结构,然后定义该结构的矩阵

例如:

#define MESSAGE_SIZE 8

struct message
{
    uint8_t body[MESSAGE_SIZE];
};

struct message msg[4][2] = {
                            { {{0x89, 0x80, 0xF8, 0x73, 0x00, 0x00, 0xCF, 0xFF}}, {{0x89, 0x81, 0xF8, 0x73, 0x00, 0x00, 0xCF, 0xFF}} },
                            { {{0x89, 0x82, 0xF8, 0x73, 0x00, 0x00, 0xCF, 0xFF}}, {{0x89, 0x83, 0xF8, 0x73, 0x00, 0x00, 0xCF, 0xFF}} },
                            { {{0x89, 0x84, 0xF8, 0x73, 0x00, 0x00, 0xCF, 0xFF}}, {{0x89, 0x85, 0xF8, 0x73, 0x00, 0x00, 0xCF, 0xFF}} },
                            { {{0x89, 0x86, 0xF8, 0x73, 0x00, 0x00, 0xCF, 0xFF}}, {{0x89, 0x87, 0xF8, 0x73, 0x00, 0x00, 0xCF, 0xFF}} }
                           };

void loop (void)
{
    int index = 0;

    if(input1 == HIGH && input2 == LOW)
    {
        index = 0;
    }
    else if (input1 == LOW && input2 == HIGH)
    {
        index = 1;
    }

    int i = 0;
    for (i=0; i< 4; i++)
    {
       serial.write(msg[i][index].body,MESSAGE_SIZE);
    }
}
#定义消息大小8
结构消息
{
uint8_t body[消息大小];
};
结构消息消息消息[4][2]={
{{0x89,0x80,0xF8,0x73,0x00,0x00,0xCF,0xFF},{{0x89,0x81,0xF8,0x73,0x00,0x00,0xCF,0xFF}},
{{0x89,0x82,0xF8,0x73,0x00,0x00,0xCF,0xFF},{{0x89,0x83,0xF8,0x73,0x00,0x00,0xCF,0xFF}},
{{0x89,0x84,0xF8,0x73,0x00,0x00,0xCF,0xFF},{{0x89,0x85,0xF8,0x73,0x00,0x00,0xCF,0xFF}},
{{0x89,0x86,0xF8,0x73,0x00,0x00,0xCF,0xFF},{{0x89,0x87,0xF8,0x73,0x00,0x00,0xCF,0xFF}}
};
无效循环(无效)
{
int指数=0;
如果(输入1==高和输入2==低)
{
指数=0;
}
else if(input1==低&input2==高)
{
指数=1;
}
int i=0;
对于(i=0;i<4;i++)
{
serial.write(msg[i][index].body,消息大小);
}
}
最好是为每条消息添加请求的信号状态:

#define MESSAGE_SIZE 8

struct message
{
    uint8_t input1;
    uint8_t input2;
    uint8_t body[MESSAGE_SIZE];
};

struct message msg[] = {
                            { HIGH, LOW , {0x89, 0x80, 0xF8, 0x73, 0x00, 0x00, 0xCF, 0xFF}},
                            { LOW , HIGH, {0x89, 0x80, 0xF8, 0x73, 0x00, 0x00, 0xCF, 0xFF}},
                            { HIGH, LOW , {0x89, 0x80, 0xF8, 0x73, 0x00, 0x00, 0xCF, 0xFF}},
                            { LOW , HIGH, {0x89, 0x80, 0xF8, 0x73, 0x00, 0x00, 0xCF, 0xFF}},
                            { HIGH, LOW , {0x89, 0x80, 0xF8, 0x73, 0x00, 0x00, 0xCF, 0xFF}},
                            { LOW , HIGH, {0x89, 0x80, 0xF8, 0x73, 0x00, 0x00, 0xCF, 0xFF}},
                            { HIGH, LOW , {0x89, 0x80, 0xF8, 0x73, 0x00, 0x00, 0xCF, 0xFF}},
                            { LOW , HIGH, {0x89, 0x80, 0xF8, 0x73, 0x00, 0x00, 0xCF, 0xFF}}
                       };

#define MESSAGE_COUNT sizeof(msg)/sizeof(msg[0])

void loop (void)
{
    int i = 0;
    for (i=0; i<MESSAGE_COUNT; i++)
    {
        if ((input1 == msg[i].input1) &&
            (input1 == msg[i].input1))
        {
            serial.write(msg[i].body,MESSAGE_SIZE);
        }
    }
}
#定义消息大小8
结构消息
{
单元8_t输入1;
单元8_t输入2;
uint8_t body[消息大小];
};
结构消息消息消息[]={
{高,低,{0x89,0x80,0xF8,0x73,0x00,0x00,0xCF,0xFF},
{低,高,{0x89,0x80,0xF8,0x73,0x00,0x00,0xCF,0xFF},
{高,低,{0x89,0x80,0xF8,0x73,0x00,0x00,0xCF,0xFF},
{低,高,{0x89,0x80,0xF8,0x73,0x00,0x00,0xCF,0xFF},
{高,低,{0x89,0x80,0xF8,0x73,0x00,0x00,0xCF,0xFF},
{低,高,{0x89,0x80,0xF8,0x73,0x00,0x00,0xCF,0xFF},
{高,低,{0x89,0x80,0xF8,0x73,0x00,0x00,0xCF,0xFF},
{低,高,{0x89,0x80,0xF8,0x73,0x00,0x00,0xCF,0xFF}
};
#定义消息\u COUNT sizeof(msg)/sizeof(msg[0])
无效循环(无效)
{
int i=0;

对于(i=0;i)和当您需要消息时?您似乎根本没有使用它。请缩进您的代码。在这里似乎2D数组会更优雅?或者只是一个1D数组?请注意,您可以使用具有4个成员的结构,因此在每种情况下只有一个赋值,而不是4ah ah…肯定至少我的答案会有一个
{
char打开else if scope….;)@LPs-这是原始问题的复制/粘贴,因此是格式。仍然看不到您的答案…:-)如果您好奇,请张贴…)