我有一个草图,在停止之前会执行大约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。
答案 0 :(得分:2)
我不确定你想用这个草图实现什么,但看了一下代码,我怀疑你的问题就行了:
delay(readDelay-millis()+iterationStart);
我怀疑你正试图让每次迭代完全readDelay
毫秒。如果迭代时间超过readDelay
,则延迟将为负。我假设负delay
参数将回绕到一个非常大的无符号整数。也许您应该检查计算出的延迟,如果是负数,则跳过延迟。
另外,正如@ m0nk3y在评论中建议的那样,您还应该将readDelay
,startMillis
和iterationStart
变量更改为unsigned long
。
这delay
肯定是我首先开始寻找的地方。当然,我也完全有可能咆哮错误的树。