Skip to content

Instantly share code, notes, and snippets.

@ArduinoDiscordBot
Created August 4, 2019 19:15
Show Gist options
  • Save ArduinoDiscordBot/1b02c776bdc5bdbd6b1bb42dcf262462 to your computer and use it in GitHub Desktop.
Save ArduinoDiscordBot/1b02c776bdc5bdbd6b1bb42dcf262462 to your computer and use it in GitHub Desktop.
Code by RabbiWood#0271 - Sun Aug 04 2019 19:15:18 GMT+0000 (Coordinated Universal Time)
const int Fridge = 2;
const int Freezer = 3;
const int DoorLED = 12;
const int Speaker = 13;
unsigned long startTime;
unsigned long currentTime;
const unsigned long period = 30000;
void setup() {
Serial.begin(9600);
pinMode(Fridge, INPUT_PULLUP);
pinMode(Freezer, INPUT_PULLUP);
pinMode(DoorLED, OUTPUT);
pinMode(Speaker, OUTPUT);
digitalWrite(DoorLED, HIGH);
attachInterrupt(digitalPinToInterrupt(Fridge), wakeUp, HIGH);
attachInterrupt(digitalPinToInterrupt(Freezer), wakeUp, HIGH);
}
bool awake = false;
void loop() {
if (awake == false) {
//We just awoke.
startTime = millis();
awake = true;
Serial.print("Start Time ");
Serial.print(startTime);
}
//Now we are in the loop, start time is set......
if ( (digitalRead(Fridge) == HIGH) || (digitalRead(Freezer) == HIGH) ){
unsigned long elapsedms = startTime - millis();
digitalWrite(DoorLED, HIGH);
if(elapsedms >= period){
tone(Speaker, 50, 500);
}
} else {
//Go to sleep here
GoToSleep();
}
}
void GoToSleep() {
Serial.println("Going to sleep now.");
digitalWrite(DoorLED, LOW);
/* Enable these for you to compile */
// sleep_enable();
// set_sleep_mode(SLEEP_MODE_PWR_DOWN);
digitalWrite(DoorLED, LOW);
awake = false;
// sleep_cpu();
}
void wakeUp() {
Serial.println("Just woke up!");
//Enable this for you
//sleep_disable();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment