rocketscream / Low-Power

Low Power Library for Arduino
www.rocketscream.com
1.27k stars 346 forks source link

Calling a Interrupt Service Routine using DS3231 and the LowPower library #87

Open mbobinger opened 5 years ago

mbobinger commented 5 years ago

Hey Guys,

I'd like to wake up an Itsybitsy M0 from Adafruit each minute, let it record a temperature value and put it to sleep again. Can you please have a look at my code below? I tried putting the LowPower.standby(); command at the end of the void section but the MCU went to sleep forever. Using LowPower.powerDown gave me a powerDown class not defined error. I think this was discussed here earlier for ATSAMD21 MCUs.

Thank you! Marco

//Code works with following DS3231 library:
// https://github.com/Makuna/Rtc/tree/master/src
// DS18 temperature sensor is connected to digital input 7
// DS3131 is connected to interrupt pin 2

#include <Wire.h>
#include <OneWire.h>
#include <RtcDS3231.h>
#include <DallasTemperature.h>
#include <LowPower.h>
const byte interruptPin = 11;
volatile bool alarm = 0;
RtcDS3231<TwoWire> rtcObject(Wire);
#define ONE_WIRE_BUS 7        // Data wire For Temp Probe is plugged into pin 10 on the Arduino

// Temperature and conductivity
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
float Temperature=10;

void setup() {

  Serial.begin(115200);
  pinMode(interruptPin, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(interruptPin), handleInterrupt, FALLING);
  rtcObject.Begin();
  RtcDateTime timestamp = RtcDateTime(__DATE__, __TIME__);
  rtcObject.SetDateTime(timestamp);
  rtcObject.Enable32kHzPin(false);
  rtcObject.SetSquareWavePin(DS3231SquareWavePin_ModeAlarmOne);

  DS3231AlarmOne alarm1(
    0,
    0,
    0,
    22,
    DS3231AlarmOneControl_SecondsMatch);

  rtcObject.SetAlarmOne(alarm1);
  rtcObject.LatchAlarmsTriggeredFlags();
 sensors.begin();
}

void loop() {

  if (alarm == true) {
    handleAlarm();
  }

}

void handleAlarm() {
  alarm = false;

  RtcDateTime timestamp = rtcObject.GetDateTime();
  Serial.print("time interrupt at: ");
  char time[10];
  sprintf(time, "%d:%d:%d",
          timestamp.Hour(),
          timestamp.Minute(),
          timestamp.Second()
         );
Serial.println(time);
GetEC();          //Calls Code to Go into GetEC() Loop [Below Main Loop] dont call this more that 1/5 hhz [once every five seconds] or you will polarise the water
GetpH();
PrintReadings();  // Cals Print routine [below main loop]

  rtcObject.LatchAlarmsTriggeredFlags();
//LowPower.powerDown(SLEEP_FOREVER, ADC_OFF, BOD_OFF);
}

void GetTEMP(){
sensors.requestTemperatures();// Send the command to get temperatures
Temperature=sensors.getTempCByIndex(0); //Stores Value in Variable
Serial.print(Temperature);
Serial.print(" °C ");
}

void handleInterrupt() {
  alarm = true;
}