![](/img/trans.png)
[英]How to read a text file and store into an array without knowing the size?
[英]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可以按我希望的方式成功工作,但是如何將它們存儲在陣列中?
建議使用兩種方法:
固定大小的預定義數組
#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 } }
使用動態數組庫我喜歡這一個 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.