Skip to content

Instantly share code, notes, and snippets.

@mgruben
Created September 19, 2020 00:13
Show Gist options
  • Save mgruben/6b1c221115b3fae3eedb765ce0d5cb83 to your computer and use it in GitHub Desktop.
Save mgruben/6b1c221115b3fae3eedb765ce0d5cb83 to your computer and use it in GitHub Desktop.
24-hour fish feeder timer using Low Power states
#include <LowPower.h>
// Define our physical setup
const int HAVE_FED_PIN = 2;
const int EARLIER_PIN = 3;
const int LED_PIN = 8;
// Declare some state-tracking variables
int haveFedButtonPressed = 0;
int alertSoonerButtonPressed = 0;
boolean alertSoonerButtonActive = false;
// Declare some constants
const unsigned int ONE_HOUR = 450;
const unsigned int SIX_HOURS = 2700;
// Keep track of how many times the Arduino has woken up
volatile unsigned int elapsedCycles = 0;
// There are 86400 / 8 seconds in one day
const unsigned int cyclesToWait = 10800;
void alertSooner() {
elapsedCycles += ONE_HOUR;
}
void turnLightOff() {
digitalWrite(LED_PIN, LOW);
}
void setup() {
pinMode(LED_PIN, OUTPUT);
pinMode(HAVE_FED_PIN, INPUT);
/* We want to use the Arduino's fancy internal 20k pull-up
resistor to help us debounce the button signals
*/
pinMode(EARLIER_PIN, INPUT_PULLUP);
// Hack to turn off the "L" LED?
pinMode(13, OUTPUT);
digitalWrite(13, LOW);
// Attach our interrupt handlers
attachInterrupt(digitalPinToInterrupt(HAVE_FED_PIN), turnLightOff, RISING);
attachInterrupt(digitalPinToInterrupt(EARLIER_PIN), alertSooner, RISING);
}
void loop() {
// Maybe alert the human to feed the fish
if (elapsedCycles++ >= cyclesToWait) {
// Turn the LED ON
digitalWrite(LED_PIN, HIGH);
// Reset the timer
elapsedCycles = 0;
}
// Go to sleep
LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment