繁体   English   中英

如何在不知道Arduino大小的情况下创建数组并在其中存储值

[英]How to create array and store values in it without knowing its size in Arduino

我正在尝试为接收器编写Arduino代码,该代码从另一个Arduino(发送器)接收十六进制值,然后我想检查该值并将它们存储在我不知道其大小的新数组中,而发送器发送值时接收器将存储他们。

值成功到达接收者,但我不知道如何将它们存储在新数组中。

我的接收器代码如下:

for (i = 0; i < len; i++)
{
 if (receivedValue[i]==0x60)
          {
    //digitalWrite(LED1,LOW); 
    // store receivedValue[i] in the new array
          }
          else
          {
            if(receivedValue[i]==0x61)
            {
            //digitalWrite(LED3,LOW); 
             // store receivedValue[i] in the new array
            }

            if (receivedValue[i]==0x62)
            {
            //digitalWrite(LED4,LOW); 
              // store receivedValue[i] in the new array
          }

          // any other receivedValue[i] dont do anything
        }
    }

LED可以按我希望的方式成功工作,但是如何将它们存储在阵列中?

建议使用两种方法:

  1. 固定大小的预定义数组

     #define MAX_ITENS 50 // The size of buffer uint8_t buffer[MAX_ITENS]; // The Buffer uint8_t posBuffer = 0; // Pointer to actual position loop() { .... // Add data to buffer posBuffer++; if (posBuffer == MAX_ITENS) { // Buffer overflow - You can set position to 0 // or give some error } else { buffer[posBuffer] = data; // Save the data } } 
  2. 使用动态数组库我喜欢这一个

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM