Skip to content

Instantly share code, notes, and snippets.

@mgruben
Created September 21, 2020 00:23
Show Gist options
  • Save mgruben/8f052953166e712cc454b11febcde525 to your computer and use it in GitHub Desktop.
Save mgruben/8f052953166e712cc454b11febcde525 to your computer and use it in GitHub Desktop.
Low-Power Feed-the-Fish Timer (Earlier bounce)
#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 int ONE_HOUR = 450;
unsigned int elapsedCycles = 0;
// There are (86400 / 8) 8-second cycles 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);
pinMode(EARLIER_PIN, INPUT);
// 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