我有一个草图,在停下来之前会执行9次。我尝试释放我在主循环中声明的所有数组,但这在停止之前将循环数减少到2。
根据MemoryFree的说法,我让它检查可用内存,看看它是否快用完了,但似乎没有泄漏。也许我编码有问题?

// nrf24_reliable_datagram_client.pde
// -*- mode: C++ -*-
// Example sketch showing how to create a simple addressed, reliable messaging client
// with the RHReliableDatagram class, using the RH_NRF24 driver to control a NRF24 radio.
// It is designed to work with the other example nrf24_reliable_datagram_server
// Tested on Uno with Sparkfun WRL-00691 NRF24L01 module
// Tested on Teensy with Sparkfun WRL-00691 NRF24L01 module
// Tested on Anarduino Mini (http://www.anarduino.com/mini/) with RFM73 module
// Tested on Arduino Mega with Sparkfun WRL-00691 NRF25L01 module

#include <RHReliableDatagram.h>
#include <RH_NRF24.h>
#include <SPI.h>
#include <DHT.h>
#include <MemoryFree.h>

DHT dht(5, DHT22);
#define CLIENT_ADDRESS 1
#define SERVER_ADDRESS 2

// Singleton instance of the radio driver
RH_NRF24 driver;
// RH_NRF24 driver(8, 7);   // For RFM73 on Anarduino Mini

// Class to manage message delivery and receipt, using the driver declared above
RHReliableDatagram manager(driver, CLIENT_ADDRESS);

void setup()
{
  pinMode(13,OUTPUT);
  Serial.begin(9600);
  if (!manager.init())
    Serial.println("init failed");
  // Defaults after init are 2.402 GHz (channel 2), 2Mbps, 0dBm
  dht.begin();
}

uint8_t data[70];
// Dont put this on the stack:
uint8_t buf[RH_NRF24_MAX_MESSAGE_LEN];

void looop()
{
  byte data[2];

  getdat(&data[0]);
}

void getdat(byte *pdata)
{
  pdata[0] = 'a';
  pdata[1] = 'b';
}

double getAvgRead(int readDelay, int iterations, double* result){
  int startMillis = millis();
  int iterationStart, i;
  for (i = 0 ; i < iterations; i++){
    iterationStart = millis();

    digitalWrite(13,HIGH);
    result[0] += dht.readTemperature(true);
    //Serial.println(result[0]);
    result[1] += dht.readHumidity();
    digitalWrite(13,LOW);

    delay(readDelay-millis()+iterationStart);
  }

  result[0] /= i; //divide by iterations to produce the average
  result[1] /= i;
  //Serial.println(result[0]);
}

int i = 0;
void loop()
{
  Serial.println("Sending, loop " + String(i++) + ", mem: " + String(freeMemory()));
  //int s = millis();
  double values[2] = {0,0};
  getAvgRead(2020,1,values);
  //Serial.println((millis()-s));

  uint8_t data[28];
  char str[28];
  sprintf(str,"U,DHT;T,%d;H,%d", (int)(values[0] * 100.0), (int)(values[1] * 100.0));
  memcpy(data,(uint8_t*)str,sizeof(str));
  Serial.println((char*)data);

  // Send a message to manager_server
  if (manager.sendtoWait(data, sizeof(data), SERVER_ADDRESS))
  {
    // Now wait for a reply from the server
    uint8_t len = sizeof(buf);
    uint8_t from;

    manager.recvfromAckTimeout(buf, &len, 2000, &from);
  }
  else
    Serial.println("sendtoWait failed");
}

引入getAvgRead函数导致此错误。我可能漏掉了什么?
编辑:这张草图是要从一个DHT传感器上读取很多次,求出读数的平均值,然后将其传输到另一个arduino。

最佳答案

我不确定你想用这个草图来达到什么目的,但是在看了代码之后,我怀疑你的问题在于:

delay(readDelay-millis()+iterationStart);

我怀疑您试图使每个迭代都花费readDelay毫秒。如果迭代时间长于readDelay,则延迟为负。我假设一个负的delay参数将被包装成一个非常大的无符号整数。也许您应该检查计算的延迟,如果为负,则跳过该延迟。
此外,正如@m0nk3y在评论中所建议的,您还应该将readDelaystartMillisiterationStart变量更改为unsigned longs。
这肯定是我首先要找的地方。当然,我也完全有可能找错了方向。

10-04 10:51