c++ - 如何在不知道 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 } }
使用我喜欢的动态数组库。
推荐阅读
- php - Curl 扩展在 Docker 中失败,出现错误“CURL_OPENSSL_3 not found”
- javascript - Rails 5 - 根据先前的 collection_select 更改 collection_select 的表单值
- react-native - 是否有可能在 react-native 的抽屉导航器中嵌套选项卡导航器?
- java - Spring AccessDeniedException 中的自定义消息
- java - 泛型方法是否可以从您传递给它的类型中调用方法
- r - 是否可以确定一行中的最大值是否唯一?
- css - 更改材质滑块拇指大小
- amazon-web-services - 无法从浏览器访问具有弹性 IP 地址的 AWS EC2 实例
- javascript - laravel ajax 返回 500(内部服务器错误)
- c# - 如何在 C# 中为我的不同属性创建动态结构?