[英]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: 建议使用两种方法:
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 } }
Use a dynamic array library I like this one . 使用动态数组库我喜欢这一个 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.