Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- ChickenDoor - V0.6
- Opens and closes a linear actuator based on sunrise and sunset tables included in the program.
- Uses the Timezone library (url below) to automatically adjust the time twice yearly for Daylight
- Saving Time.
- */
- // Includes
- //
- #include <LinkedList.h> //https://github.com/ivanseidel/LinkedList
- #include <Arduino.h>
- #include <DS1307RTC.h> //http://www.arduino.cc/playground/Code/Time
- #include <Time.h> //http://www.arduino.cc/playground/Code/Time
- #include <TimeLib.h>
- #include <Timezone.h> //https://github.com/JChristensen/Timezone
- #include <Wire.h> //http://arduino.cc/en/Reference/Wire (supplied with the Arduino IDE)
- #pragma GCC diagnostic ignored "-Wwrite-strings"
- // Defines
- //
- #define DEBUG
- #define OPEN 0
- #define CLOSED 1
- #define OFF 0
- #define ON 1
- #define SUNRISE_DELAY (120*60)
- #define SUNSET_DELAY (70*60)
- struct TimeWindow {
- long int t1;
- long int t2;
- };
- LinkedList<TimeWindow> timeSchedule;
- // Iterate of all TimeWindows and check if the timestamp is "in" any of them.
- bool checkSchedule(time_t timestamp, LinkedList<TimeWindow> *schedule) {
- bool inWindow = false;
- for (int i = 0; i < schedule->size(); i++) {
- TimeWindow tw = schedule->get(i);
- inWindow = timeInWindow(timestamp, &tw);
- if (inWindow) {
- return true;
- }
- }
- return false;
- }
- // Given a single timestamp, check if it lies within the TimeWindow
- bool timeInWindow(time_t timestamp, struct TimeWindow *window) {
- if (timestamp > window->t1 && timestamp < window->t2) {
- return true;
- }
- return false;
- }
- // Check that schedule is valid
- bool isScheduleValid(struct TimeWindow window) {
- return true;
- }
- // Constants
- //
- const int delayTime = 1000;
- const int relayOnePin = 1; // Unused
- const int relayTwoPin = 2; // Door 1
- const int relayThreePin = 3; // Door 2
- const int relayFourPin = 4; // Lights
- const int secondsPerHour = 3600;
- // Days of months array for ordinal calculation
- static const int monthLen[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
- // Globals
- //
- // Timezone rules - US Eastern Time Zone (New York, Detroit)
- TimeChangeRule myDST = {"CDT", Second, Sun, Mar, 2, -240}; //Daylight time = UTC - 4 hours
- TimeChangeRule myEST = {"CST", First, Sun, Nov, 2, -300}; //Standard time = UTC - 5 hours
- Timezone myTZ(myDST, myEST);
- TimeChangeRule *tcr; // pointer to the time change rule, use to get TZ abbrev
- time_t utc, local;
- int doorState = CLOSED;
- int lightState = OFF;
- // Init
- void setup()
- {
- // Initialize Schedule
- timeSchedule.add({.t1 = 2, .t2 = 4});
- timeSchedule.add({.t1 = 5, .t2 = 7});
- timeSchedule.add({.t1 = 10, .t2 = 11});
- timeSchedule.add({.t1 = 15, .t2 = 30});
- timeSchedule.add({.t1 = 40, .t2 = 100});
- // Initialize the relay pins
- pinMode(relayOnePin, OUTPUT);
- pinMode(relayTwoPin, OUTPUT);
- pinMode(relayThreePin, OUTPUT);
- pinMode(relayFourPin, OUTPUT);
- #ifdef DEBUG
- // Start the serial port
- Serial.begin(9600);
- while (!Serial)
- ;
- delay(200);
- #endif
- Serial.println("Schedule Size:");
- Serial.println(timeSchedule.size());
- // Set the time using the RTC's time
- // setSyncProvider(RTC.get); // Get the time from the RTC
- #ifdef DEBUG
- if(timeStatus()!= timeSet) // Again, this is check the TZ lib status
- Serial.println("Unable to sync with the RTC");
- else
- Serial.println("RTC has set the system time ");
- #endif
- // Door initialization based on TZ library time
- // utc = now();
- #ifdef DEBUG
- Serial.print("Start-up time: ");
- printTime(local, tcr -> abbrev);
- #endif
- }
- void loop()
- {
- bool shouldOpenDoor = false;
- // Wait the current delay time
- delay(delayTime);
- // get current date and time
- // utc = now();
- utc = (time_t) (millis() / 1000);
- shouldOpenDoor = checkSchedule(utc, &timeSchedule);
- if (shouldOpenDoor) {
- openDoor();
- } else {
- closeDoor();
- }
- #ifdef DEBUG
- // printTime(local, tcr -> abbrev);
- #endif
- }
- void openDoor()
- {
- // Is the door closed?
- if (doorState == CLOSED)
- {
- #ifdef DEBUG
- Serial.print("Opening door: ");
- utc = now();
- local = myTZ.toLocal(utc, &tcr);
- printTime(local, tcr -> abbrev);
- #endif
- doorState = OPEN;
- // open the door
- digitalWrite(relayTwoPin, HIGH);
- digitalWrite(relayThreePin, HIGH);
- }
- return;
- }
- void closeDoor()
- {
- // Is the door open?
- if (doorState == OPEN)
- {
- #ifdef DEBUG
- Serial.print("Closing door: ");
- utc = now();
- local = myTZ.toLocal(utc, &tcr);
- printTime(local, tcr -> abbrev);
- #endif
- doorState = CLOSED;
- // close the door
- digitalWrite(relayTwoPin, LOW);
- digitalWrite(relayThreePin, LOW);
- }
- return;
- }
- #ifdef DEBUG
- //Function to print time with time zone
- void printTime(time_t t, char *tz)
- {
- sPrintI00(hour(t));
- sPrintDigits(minute(t));
- sPrintDigits(second(t));
- Serial.print(' ');
- Serial.print(dayShortStr(weekday(t)));
- Serial.print(' ');
- sPrintI00(day(t));
- Serial.print(' ');
- Serial.print(monthShortStr(month(t)));
- Serial.print(' ');
- Serial.print(year(t));
- Serial.print(' ');
- Serial.print(tz);
- Serial.println();
- }
- //Print an integer in "00" format (with leading zero).
- //Input value assumed to be between 0 and 99.
- void sPrintI00(int val)
- {
- if (val < 10) Serial.print('0');
- Serial.print(val, DEC);
- return;
- }
- //Print an integer in ":00" format (with leading zero).
- //Input value assumed to be between 0 and 99.
- void sPrintDigits(int val)
- {
- Serial.print(':');
- if(val < 10) Serial.print('0');
- Serial.print(val, DEC);
- }
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement