129 lines
3.0 KiB
C++
129 lines
3.0 KiB
C++
#include <Servo.h>
|
|
#include "LowPower.h"
|
|
|
|
// Dusplay definitions
|
|
byte sevenSegDigits[9] = { B00001001 , // = 1
|
|
B10110011 , // = 2
|
|
B10011011 , // = 3
|
|
B11001001 , // = 4
|
|
B11011010 , // = 5
|
|
B11111000 , // = 6
|
|
B00001011 , // = 7
|
|
B11111011 , // = 8
|
|
B11001011 // = 9
|
|
};
|
|
byte sevenSegDP = B00000100; // = DP
|
|
|
|
|
|
// Pin definition
|
|
// -buttons
|
|
const int buttonTimeCyclePin = 3;
|
|
const int buttonConfirmPin = 2;
|
|
// -display
|
|
const int latchPin = 11;
|
|
const int clockPin = 9;
|
|
const int dataPin = 12;
|
|
// -servo
|
|
Servo doorServo;
|
|
const int servoPin = 5;
|
|
|
|
const int MIN_TIME = 1800;
|
|
const int MIN_TIME_TEST = 8; // for testing
|
|
|
|
void setup() {
|
|
Serial.begin(9600);
|
|
|
|
// set all pin to the correct mode
|
|
// -buttons
|
|
pinMode(buttonTimeCyclePin, INPUT_PULLUP); //define pinMode and use Pull-up resistor
|
|
pinMode(buttonConfirmPin, INPUT_PULLUP);
|
|
// -display
|
|
pinMode(latchPin, OUTPUT);
|
|
pinMode(clockPin, OUTPUT);
|
|
pinMode(dataPin, OUTPUT);
|
|
// -servo
|
|
doorServo.attach(servoPin);
|
|
doorServo.write(82);
|
|
delay(2000);
|
|
}
|
|
|
|
void displayNumber(int cycleMultiply) {
|
|
bool bDP = ((cycleMultiply % 2) == 0);
|
|
int digitIndex = (cycleMultiply - 1) / 2;
|
|
|
|
Serial.println(digitIndex);
|
|
|
|
digitalWrite(latchPin, LOW);
|
|
|
|
shiftOut(dataPin, clockPin, MSBFIRST, sevenSegDigits[digitIndex]+(sevenSegDP*bDP));
|
|
|
|
digitalWrite(latchPin, HIGH);
|
|
}
|
|
|
|
void displayBlank(){
|
|
digitalWrite(latchPin, LOW);
|
|
shiftOut(dataPin, clockPin, MSBFIRST, B00000000);
|
|
digitalWrite(latchPin, HIGH);
|
|
}
|
|
|
|
void displayBlink(int cycleMultiply) {
|
|
for (int i = 1; i <= 4; i++) {
|
|
displayNumber(cycleMultiply);
|
|
delay(300);
|
|
displayBlank();
|
|
delay(100);
|
|
}
|
|
}
|
|
|
|
int getSleepDuration() {
|
|
int cycleMultiply = 1;
|
|
displayNumber(cycleMultiply);
|
|
|
|
while(true) {
|
|
if (digitalRead(buttonTimeCyclePin) == LOW) {
|
|
cycleMultiply = cycleMultiply % 18;
|
|
cycleMultiply++;
|
|
Serial.print("Increased timer by 1: ");
|
|
Serial.println(cycleMultiply);
|
|
displayNumber(cycleMultiply);
|
|
}
|
|
if (digitalRead(buttonConfirmPin) == LOW) {
|
|
Serial.print("Confirmed timer: ");
|
|
Serial.println(cycleMultiply);
|
|
displayBlink(cycleMultiply);
|
|
return MIN_TIME * cycleMultiply;
|
|
}
|
|
delay(200);
|
|
}
|
|
}
|
|
|
|
void deepSleep(int sleepDuration) {
|
|
int sleepCycles = sleepDuration / 8;
|
|
for (int i = 1; i <= sleepCycles; i++) {
|
|
LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF);
|
|
Serial.print("Finished cycle: ");
|
|
Serial.println(i);
|
|
Serial.flush();
|
|
}
|
|
|
|
Serial.println("Done sleeping");
|
|
}
|
|
|
|
void openDoor() {
|
|
doorServo.write(10);
|
|
delay(1000);
|
|
}
|
|
|
|
void loop() {
|
|
|
|
int sleepDuration = getSleepDuration();
|
|
|
|
Serial.print("Got sleep duration: ");
|
|
Serial.println(sleepDuration);
|
|
Serial.println("Entering sleep mode.");
|
|
Serial.flush();
|
|
|
|
deepSleep(sleepDuration);
|
|
openDoor();
|
|
}
|