Hi, I was wondering if someone would be kind enough to give me some advice I am relatively new to Arduino/programming, and am currently working on a small home project, to build up my knowledge to measure temperature on a low power sensor network, using an Arduino Pro mini and nRF24L01 wireless module.
I have written a small program based on the code written by maniacbug, and have managed to get the receiver and transmitter communicating under continual operation. Although to increase the battery life I am looking to put the Arduino and wireless module into sleep on an 8 sec watchdog timer, wake up take a reading then go back to sleep.
I have trialled the watchdog tutorial using a flashing LED on the transmitter node only, with no RF communication and it works correctly. As soon as I attempt to combine the two codes and transmit data over the network to print to serial, the timings appear to be random, some are 8 seconds apart, but others are up to 30 seconds apart. I have looked over the past few days, and am not sure what else to try, and was wondering if I have missed something obvious, and whether anyone has experienced this before?
Any help you could give would be much appreciated my code for the transmitter and receiver are shown below.
Transmitter
#include <avr/sleep.h>
#include <avr/power.h>
#include <avr/wdt.h>
volatile int f_wdt=1;
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#define CE_PIN 9
#define CSN_PIN 10
#define Sensor_1 A2
const uint64_t pipe = 0xE8E8F0F0E1LL;
RF24 radio(CE_PIN, CSN_PIN);
int TempSensor[1];
ISR(WDT_vect)
{
if(f_wdt == 0)
{
f_wdt=1;
}
}
void enterSleep(void)
{
wdt_reset();
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
sleep_enable();
sleep_mode();
sleep_disable();
power_all_enable();
}
void setup()
{
radio.begin();
radio.openWritingPipe(pipe);
MCUSR &= ~(1<<WDRF);
WDTCSR |= (1<<WDCE) | (1<<WDE);
WDTCSR = 1<<WDP0 | 1<<WDP3; /* 8.0 seconds Sleep*/
WDTCSR |= _BV(WDIE);
}
void loop()
{
if(f_wdt == 1)
{
radio.begin();
radio.openWritingPipe(pipe);
TempSensor[0]=analogRead(Sensor_1);
radio.write(TempSensor, sizeof(TempSensor) );
delay(100);
radio.powerDown();
f_wdt = 0;
enterSleep();
}
}
Receiver
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#define CE_PIN 9
#define CSN_PIN 10
const uint64_t pipe = 0xE8E8F0F0E1LL;
RF24 radio(CE_PIN, CSN_PIN);
int TempSensor[1];
void setup()
{
Serial.begin(9600);
radio.begin();
radio.openReadingPipe(1,pipe);
radio.startListening();
}
void loop(){
if ( radio.available() )
{
bool done = false;
while (!done) {
done = radio.read( TempSensor, sizeof(TempSensor) );
Serial.println(TempSensor[0]);
}
}
}