简体   繁体   English

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

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

I'm trying to write Arduino code for receiver that receives hexadecimal values from another Arduino (Transmitter), then I want to check this values and store them in new array which I don't know its size, while transmitter sends values receiver will store them. 我正在尝试为接收器编写Arduino代码,该代码从另一个Arduino(发送器)接收十六进制值,然后我想检查该值并将它们存储在我不知道其大小的新数组中,而发送器发送值时接收器将存储他们。

Values reaches successfully to receiver but I don't know how to store them in new array. 值成功到达接收者,但我不知道如何将它们存储在新数组中。

My receiver code is like this: 我的接收器代码如下:

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

LEDs works successfully as I want but how to store them in array? LED可以按我希望的方式成功工作,但是如何将它们存储在阵列中?

Two approaches suggested: 建议使用两种方法:

  1. Array pre-defined with fixed size 固定大小的预定义数组

     #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. Use a dynamic array library I like this one . 使用动态数组库我喜欢这一个

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

相关问题 如何在不知道大小的情况下读取文本文件并存储到数组中? - How to read a text file and store into an array without knowing the size? 如何在不知道其大小的情况下初始化容器结构? - How to initialize a container struct without knowing its size? 如何在不知道其大小的情况下迭代读取的字符串? - How to iterate a string in which was read without knowing its size? 在不知道大小的情况下在标头中声明结构数组 - Declaring array of struct in header without knowing size 如何在不知道其父级的情况下旋转子树 - How to rotate a subtree without knowing its parent 如何在不知道大小的情况下编写一个程序来修改动态数组的元素和大小? - How to write a procedure to modify the elements and size of a dynamic array without knowing the size? 有没有办法在不知道 c++ 的大小的情况下迭代枚举 - Is there a way to iterate over an enum without knowing its size in c++ 动态数组的大小或在不知道大小的情况下循环遍历 - Size of dynamic array or loop through it without knowing size 如何在不知道C ++长度的情况下从文件中读取二维数组? - How to read a 2d array from a file without knowing its length in C++? 数组中的C ++ max / min元素,不知道数组大小 - C++ max/min element in an array without knowing array size
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM