I'm pretty stumped by this, hopefully someone can chime in. I have some code for a watchdog wake-up from sleep, and I have code for using an attiny85 with an NRF radio. Each works by itself, but not when I put it together.
What I'm after is to have the attiny wake up periodically, take a reading, transmit that reading, then go back to sleep.
My hunch is that the RF library is using interrupts or a timer which is conflicting with my sleep-code.
Here is my code for the sleep/wake:
#include <avr/sleep.h>
#include <avr/interrupt.h>
#include <avr/wdt.h>
#define LED 0
ISR(WDT_vect)
{
wdt_disable();
}
void myWatchdogEnable(const byte interval)
{
wdt_reset();
MCUSR = 0; // reset various flags
WDTCR |= 0b00011000; // see docs, set WDCE, WDE
WDTCR = 0b01000000 | interval; // set WDIE, and appropriate delay
ADCSRA &= ~_BV(ADEN);
set_sleep_mode (SLEEP_MODE_PWR_DOWN);
sleep_bod_disable();
sei();
sleep_mode();
ADCSRA |= _BV(ADEN);
}
void setup()
{
pinMode (LED, OUTPUT);
}
void loop(){
digitalWrite (LED, HIGH);
delay (2000);
digitalWrite (LED, LOW);
for (int i = 0; i < 8; i++){
myWatchdogEnable (0b000110); // 1 second
}
}
// sleep bit patterns:
// 1 second: 0b000110
// 2 seconds: 0b000111
// 4 seconds: 0b100000
// 8 seconds: 0b100001
Here is the attiny85 radio transmission code:
#include "RF24.h"
#define CE_PIN 6
#define CSN_PIN 6 //Since we are using 3 pin configuration we will use same pin for both CE and CSN
RF24 radio(CE_PIN, CSN_PIN);
byte address[11] = "SimpleNode";
unsigned long payload = 0;
void setup() {
pinMode(4, OUTPUT);
digitalWrite(4, HIGH);
radio.begin(); // Start up the radio
radio.setAutoAck(1); // Ensure autoACK is enabled
radio.setRetries(15,15); // Max delay between retries & number of retries
radio.openWritingPipe(address); // Write to device address 'SimpleNode'
}
void loop(void){
payload++;
radio.write( &payload, sizeof(unsigned long) ); //Send data to 'Receiver' ever second
delay(1000);
}
Each one of those works fine. But here is the combined code that does not work:
#include <avr/sleep.h>
#include <avr/interrupt.h>
#include <avr/wdt.h>
#include "RF24.h"
#define CE_PIN 6
#define CSN_PIN 6
RF24 radio(CE_PIN, CSN_PIN);
byte address[11] = "SimpleNode";
unsigned long payload = 0;
void setup()
{
radio.begin(); // Start up the radio
radio.setAutoAck(1); // Ensure autoACK is enabled
radio.setRetries(15,15); // Max delay between retries & number of retries
radio.openWritingPipe(address); // Write to device address 'SimpleNode'
pinMode (4, OUTPUT);
}
void loop(){
digitalWrite (4, HIGH);
delay(250);
payload++;
radio.write( &payload, sizeof(unsigned long) ); //Send data to 'Receiver' ever second
delay(250);
digitalWrite(4, LOW);
for (int i = 0; i < 8; i++){
myWatchdogEnable (0b000110); // 1 second
}
}
ISR(WDT_vect)
{
wdt_disable();
}
void myWatchdogEnable(const byte interval)
{
noInterrupts();
wdt_reset();
MCUSR = 0; // reset various flags
WDTCR |= 0b00011000; // see docs, set WDCE, WDE
WDTCR = 0b01000000 | interval; // set WDIE, and appropriate delay
//ADCSRA &= ~_BV(ADEN);
set_sleep_mode (SLEEP_MODE_PWR_DOWN);
sleep_bod_disable();
interrupts();
sleep_mode();
//ADCSRA |= _BV(ADEN);
}
// sleep bit patterns:
// 1 second: 0b000110
// 2 seconds: 0b000111
// 4 seconds: 0b100000
// 8 seconds: 0b100001
