#include <Arduino.h>
#include <TM1637Display.h>
// Module connection pins (Digital Pins) for Display TM1637
#define CLK 3
#define DIO 2
TM1637Display display(CLK, DIO);
int next_Hatch = 1;
int hour1 = 13; //Alarm 1 hour
int minute1 = 00; // Alarm 1 minute
int time1 = 0; // Variable that shows time in display
int hour2 = 18; // Alarm 2 hour
int minute2 = 00; // Alarm 2 minute
int time2 = 0; // Variable that shows time in display
int alarmChosen = 1; // Variable for choosing which alarm to edit
int buttonMinute = 9; // Button for changing minute
int buttonHour = 8; // Button for changing hour
int slidePin = 10; // SlideSwitch for waking up arduino
int almchooserPin = 12; // Button for changing which alarm to edit
int minuteState = 0; // State of the minute button (if it's pressed or not)
int hourState = 0; // State of the hour button (if it's pressed or not)
int slideState = 0; // State of slideswitch button (if it's active or not)
int almchooserState =0; // State of alarm chooser button (if it's pressed or not)
int awake = 0; // variable for if loop. Millis() start time and end time
unsigned long time_start; // Time when program's starting to run
unsigned long time_elapsed; // Elapsed time form start to end of program
int time_limit = 3000; // Time limit for judging if it's an alarm wakeup or if it's a switch wakeup. Written in miliseconds.
// Create an array that sets individual segments per digit to display the word "A1"
const uint8_t A_one[] = {
SEG_A | SEG_B | SEG_C | SEG_E | SEG_F | SEG_G, // A
SEG_B | SEG_C // 1
};
// Create an array that sets individual segments per digit to display the word "A2"
const uint8_t A_two[] = {
SEG_A | SEG_B | SEG_C | SEG_E | SEG_F | SEG_G, // A
SEG_A | SEG_B | SEG_G | SEG_E | SEG_D // 2
};
const uint8_t H_one[] = {
SEG_B | SEG_C | SEG_E | SEG_F | SEG_G, // H
SEG_B | SEG_C // 1
};
const uint8_t H_two[] = {
SEG_B | SEG_C | SEG_E | SEG_F | SEG_G, // H
SEG_A | SEG_B | SEG_G | SEG_E | SEG_D // 2
};
const uint8_t H_three[] = {
SEG_B | SEG_C | SEG_E | SEG_F | SEG_G, // H
SEG_A | SEG_B | SEG_C | SEG_D | SEG_G // 3
};
const uint8_t H_four[] = {
SEG_B | SEG_C | SEG_E | SEG_F | SEG_G, // H
SEG_B | SEG_C | SEG_F | SEG_G // 4
};
void setup() {
// Setting up all buttons and switches
pinMode(buttonMinute, INPUT);
pinMode(buttonHour, INPUT);
pinMode(slidePin, INPUT_PULLUP);
pinMode(almchooserPin, INPUT);
display.setBrightness(1); // Setting brightness of TM1637 diplay
Serial.begin(9600);
}
void loop() {
// Setting up buttons and switches for state observation
minuteState = digitalRead(buttonMinute);
hourState = digitalRead(buttonHour);
slideState = digitalRead(slidePin);
almchooserState = digitalRead(almchooserPin);
if(slideState == HIGH){
// Starting timer for keeping track if it's a alarm wakeup or if it's switch wakeup
if(awake == 0){
time_start = millis();
// Display which hatch that's opening
if(next_Hatch == 1){
display.setSegments(H_one,2,2);
}else if(next_Hatch == 2){
display.setSegments(H_two,2,2);
}
delay(1000);
display.clear();
awake = 1;
}
// Alarm button switch code
if(almchooserState == HIGH && minuteState == LOW && hourState == LOW){
switch(alarmChosen){
case 1:
alarmChosen = 2;
display.clear();
display.setSegments(A_two,2,2);
delay(1000);
break;
case 2:
alarmChosen = 1;
display.clear();
display.setSegments(A_one,2,2);
delay(1000);
break;
}
}
// Set new time alarm 1
if(alarmChosen == 1 && almchooserState == LOW){
if(minuteState == HIGH && hourState == LOW && minute1 < 60){
minute1++;
delay(300);
}
if(minute1 >= 60){
minute1 = 0;
}
if(hourState == HIGH && minuteState == LOW && hour1 < 24){
hour1++;
delay(300);
}
if(hour1 >= 24){
hour1 = 0;
}
time1 = hour1*100+minute1;
display.showNumberDecEx(time1);
}
// Set new time alarm 2
if (alarmChosen == 2 && almchooserState == LOW){
if(minuteState == HIGH && hourState == LOW && minute2 < 60){
minute2++;
delay(300);
}
if(minute2 >= 60){
minute2 = 0;
}
if(hourState == HIGH && minuteState == LOW && hour2 < 24){
hour2++;
delay(300);
}
if(hour2 >= 24){
hour2 = 0;
}
time2 = hour2*100+minute2;
display.showNumberDecEx(time2);
}
}else{
display.clear();
// Checking how long the arduino has been running
// for later to evaluate if it's a alarm wakeup or switch wakeup
if(awake == 1){
time_elapsed = millis() - time_start;
awake = 0;
Serial.println("Elapsed time:");
Serial.println(time_elapsed); // prints time since program started
// Evaluation if it's an alarm or switch wakeup
// This determines if hatches should open or not
if(time_elapsed > time_limit){
Serial.println("It's a switch wakeup. Don't open hatches");
// enterSleep();
}else{
Serial.println("It's an alarm wakeup. Open hatch!");
// Run OpenDoor();
// enterSleep();
}
}
}
}