Skip to content

Instantly share code, notes, and snippets.

@dwblair
Last active February 8, 2016 04:59
Show Gist options
  • Save dwblair/2076508bafa39d2f31b2 to your computer and use it in GitHub Desktop.
Save dwblair/2076508bafa39d2f31b2 to your computer and use it in GitHub Desktop.
// **** INCLUDES *****
#include "LowPower.h"
#include <DS3232RTC.h> //http://github.com/JChristensen/DS3232RTC
#include <Wire.h> //http://arduino.cc/en/Reference/Wire
// Use pin 2 as wake up pin
const int wakeUpPin = 2;
int led = 9;
int nMos = 3;
int sleepInterval = 10;
int onInterval = 5;
volatile boolean alarmIsrWasCalled = false;
void wakeUp()
{
// Just a handler for the pin interrupt.
alarmIsrWasCalled = true;
}
void setup()
{
pinMode(nMos,OUTPUT);
digitalWrite(nMos,LOW);
// Configure wake up pin as input.
// This will consumes few uA of current.
pinMode(wakeUpPin, INPUT_PULLUP);
// pin 5 is the RTC pin -- add an internal pullup
pinMode(5,INPUT_PULLUP);
//Serial.begin(115200);
//Disable the default square wave of the SQW pin.
RTC.squareWave(SQWAVE_NONE);
//int seconds = second(RTC.get());
//int alarmSeconds = (seconds+10)%60;
int alarmSeconds=20;
RTC.setAlarm(ALM1_MATCH_SECONDS, alarmSeconds, 0, 0, 1); //daydate parameter should be between 1 and 7
RTC.alarm(ALARM_1); //ensure RTC interrupt flag is cleared
RTC.alarmInterrupt(ALARM_1, true);
}
void loop()
{
//Disable the default square wave of the SQW pin.
RTC.squareWave(SQWAVE_NONE);
//int seconds = second(RTC.get());
//int alarmSeconds = (seconds+10)%60;
//every interval seconds
int alarmSeconds=(second(RTC.get())+sleepInterval)%60;
RTC.setAlarm(ALM1_MATCH_SECONDS, alarmSeconds, 0, 0, 1); //daydate parameter should be between 1 and 7
RTC.alarm(ALARM_1); //ensure RTC interrupt flag is cleared
RTC.alarmInterrupt(ALARM_1, true);
//every interval minutes
/*
int alarmMinutes=(minute(RTC.get())+interval)%60;
RTC.setAlarm(ALM1_MATCH_MINUTES, 0, alarmMinutes, 0, 1); //daydate parameter should be between 1 and 7
RTC.alarm(ALARM_1); //ensure RTC interrupt flag is cleared
RTC.alarmInterrupt(ALARM_1, true);
*/
// Allow wake up pin to trigger interrupt on low.
attachInterrupt(0, wakeUp, LOW);
Serial.print("Sleeping for ");
Serial.println(sleepInterval);
delay(20);
// Enter power down state with ADC and BOD module disabled.
// Wake up when wake up pin is low.
LowPower.powerDown(SLEEP_FOREVER, ADC_OFF, BOD_OFF);
delay(20);
Serial.print("Waking up for ");
Serial.println(onInterval);
// Disable external pin interrupt on wake up pin.
detachInterrupt(0);
pinMode(led, OUTPUT);
for (int i=0;i<3;i++) {
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(30); // wait for a second
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(30);
}
// turn on the switch
digitalWrite(nMos,HIGH);
// keep it on onInterval
delay(onInterval*1000); // amount of time to keep the switch on
pinMode(led, OUTPUT);
for (int i=0;i<3;i++) {
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(30); // wait for a second
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(30);
}
// turn off the switch
digitalWrite(nMos,LOW);
// Do something here
// Example: Read sensor, data logging, data transmission.
}
@dwblair
Copy link
Author

dwblair commented Feb 8, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment