根据if语句更改数组值

时间:2016-05-24 09:24:10

标签: 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, 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)
}

我的问题是如何在if语句中将一个数组分配给另一个数组?我应该使用指针还是什么?

2 个答案:

答案 0 :(得分:1)

使用指向值的指针:

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)
}

答案 1 :(得分:1)

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

示例:

#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);
    }
}

最好是为每条消息添加请求的信号状态:

#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);
        }
    }
}