So I did some basic tests:
1. Moteino (with radio, and flash) running a baseline code that blinks LED every second:
void setup()
{
pinMode(9, OUTPUT);
}
void loop()
{
digitalWrite(9, HIGH);
delay(1000);
digitalWrite(9, LOW);
delay(1000);
}
Result: No problem on FTDI or battery power - works fine.
2. Moteino waking up every second using watchdog timer interrupts and blinking LED and then going into power-down mode between each blink. I didn't use any library, but used bare AVR commands and bit manipulation instead, to avoid potential errors in any library.
ISR(WDT_vect) // Interrupt service routine for WatchDog Timer
{
asm("nop"); // do nothing
}
void setup()
{
pinMode(9, OUTPUT);
cli(); // disable global interrupts
MCUSR &= ~(1<<WDRF); // Clear WDRF in MCUSR
WDTCSR |= (1<<WDCE) | (1<<WDE); // Write logical one to WDCE and WDE
WDTCSR = (1<<WDIE) | (1<<WDP2) | (1<<WDP1); // Turn on WDT interrupt and set prescale at 1.0s
sei(); // enable global interrupts
SMCR = (1<<SE) | (1<<SM1); // enable power-down mode
asm("wdr"); // Reset Watchdog Timer
}
void loop()
{
asm("sleep"); // put processor in sleep mode
digitalWrite(9, HIGH);
delay(1000);
digitalWrite(9, LOW);
delay(1000);
}
Result: No problem on FTDI or battery power - works fine.
3. Moteino going into power-down mode, configured to wake up on external interrupt generated by the DS3234 clock every 5 seconds and then blink the LED for a sec. I used my own library to interface with the clock and set interrupts. Library available here:
https://github.com/OSBSS/DS3234lib3 (only difference is that I changed the CS pin of the clock to pin 7 on the Moteino)
#include <DS3234lib3.h>
DS3234 RTC; // declare object for DS3234 class
long interval = 5; // set interval in seconds. Example: 300 seconds = 5 min logging interval
//int dayStart = 25, hourStart = 22, minStart = 51; // set start date/time
ISR(PCINT2_vect) // Interrupt service routine for PCINT2
{
asm("nop"); // do nothing
}
void setup()
{
pinMode(9, OUTPUT);
pinMode(3, OUTPUT); // DS3234 is pin powered
digitalWrite(3, HIGH); // enable the RTC
cli(); // disable global interrupts
PORTD |= (1<<PORTD4); //Activate pullup on pin 4
PCICR |= (1<<PCIE2); // enable pin change interrupts
PCMSK2 |= (1<<PCINT20); // set pin change interrupt mask on pin 4
sei(); // enable global interrupts
SMCR = (1<<SE) | (1<<SM1); // enable power-down mode
RTC.setNewAlarm(interval); // set next alarm after specified interval (in seconds)
}
void loop()
{
asm("sleep"); // go to sleep. code pauses here
// code resumes from here after clock generates interrupt on pin 4
RTC.alarmFlagClear(); // clear alarm flag
digitalWrite(9, HIGH);
delay(1000);
digitalWrite(9, LOW);
delay(1000);
RTC.setNextAlarm(); // set next alarm
}
Result: THIS is where the code fails to run after the first boot on battery. When I manually reset it, it runs fine after that, generating interrupts as programmed. The first boot on battery makes it stuck somewhere where it's not entirely in sleep mode or fully awake either. Running the exact same code on Arduino Pro Mini or even bare ATmega328P works fine without problems. Oh, and also, on FTDI it runs fine, since the DTR pin is pulled low and opening serial monitor resets the Mote anyway. Same thing even if I don't open Serial monitor, but have the FTDI plugged in either powering the device via USB or a battery and connecting the remaning 5 pins of FTDI - still works as long as FTDI is connected.
Note that all three code samples above do not use any code related to the radio or the flash. We're talking pure Moteino here. I'm curious to see if this happens on a Moteino without the flash or radio soldered on (but I don't want to desolder one of my Motes to try that out). Oh and another thing - I do have Moteino cores defined in the hardware folder. I see the same issue even if I upload the code as Arduino Uno instead. Using Arduino IDE 1.6.8.
Looks like pin-change interrupts aren't handled that well when the RFM69 is included in the circuit? No idea why, since the exact same code works on another arduino without the radio.