arduino-libraries / RTCZero

RTC Library for SAMD21 based boards
http://arduino.cc/en/Reference/RTC
GNU Lesser General Public License v2.1
78 stars 78 forks source link

Adafruit Feather M0 won't wake up? #25

Closed softweyr closed 6 years ago

softweyr commented 7 years ago

I have an Adafruit Feather M0 RFM69HCW that has become redundant from another project, so I thought I'd make an outdoor temperature sensor from it.

The idea is to wake up once a minute, sample the temperature from a DHT-11 I have lying around, blast it out over the RFM radio, then go to sleep. It's all working moderately well, except I can't get the M0 to 'wake up' once I put it in standby. The ISR is called, but loop() never runs again.

I noticed something in the code about standby being problematic when the onboard USB is connected, and this still has a lot of serial debug code in it. Should I give it a try unplugged, perhaps redirecting the prints to Serial1 with my trust FTDI board?

frankleonrose commented 7 years ago

Take a look here:

Serial.end();
USBDevice.detach(); // Safely detach the USB prior to sleeping
rtc.standbyMode();    // Sleep until next alarm match
USBDevice.attach();   // Re-attach the USB, audible sound on windows machines

@cavemoa makes a comment somewhere that this was critical to getting sleep working while USB connected. Also that, depending on your OS and tools, you may have to restart your serial monitor after the detach.

softweyr commented 6 years ago

I finally got back to this. I just tried a simple example that doesn't do any USB I/O, just blinks the LED, and it works perfectly; this trivial example lights the onboard LED for 1 second every minute:

#include <RTCZero.h>

/* Create an rtc object */
RTCZero rtc;

/* Change these values to set the current initial time */
const byte seconds = 0;
const byte minutes = 00;
const byte hours = 17;

/* Change these values to set the current initial date */
const byte day = 17;
const byte month = 11;
const byte year = 15;

void setup()
{
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, LOW);

  rtc.begin();
  rtc.setTime(hours, minutes, seconds);
  rtc.setDate(day, month, year);

  rtc.setAlarmSeconds(10);
  rtc.enableAlarm(rtc.MATCH_SS);
  rtc.attachInterrupt(alarmMatch);

  rtc.standbyMode();
}

void loop()
{
  delay(1000);
  digitalWrite(LED_BUILTIN, LOW);

  rtc.setAlarmSeconds(10);
  rtc.enableAlarm(rtc.MATCH_SS);
  rtc.attachInterrupt(alarmMatch);

  rtc.standbyMode(); // Sleep until next alarm match
}

void alarmMatch()
{
  digitalWrite(LED_BUILTIN, HIGH);
}