Advertisement
Guest User

Untitled

a guest
Jan 21st, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  
  3.    ChickenDoor - V0.6
  4.  
  5.     Opens and closes a linear actuator based on sunrise and sunset tables included in the program.
  6.  
  7.     Uses the Timezone library (url below) to automatically adjust the time twice yearly for Daylight
  8.     Saving Time.
  9.  
  10. */
  11.  
  12. // Includes
  13. //
  14. #include <LinkedList.h>  //https://github.com/ivanseidel/LinkedList
  15. #include <Arduino.h>
  16. #include <DS1307RTC.h>   //http://www.arduino.cc/playground/Code/Time
  17. #include <Time.h>        //http://www.arduino.cc/playground/Code/Time
  18. #include <TimeLib.h>
  19. #include <Timezone.h>    //https://github.com/JChristensen/Timezone
  20. #include <Wire.h>        //http://arduino.cc/en/Reference/Wire (supplied with the Arduino IDE)
  21.  
  22. #pragma GCC diagnostic ignored "-Wwrite-strings"
  23.  
  24. // Defines
  25. //
  26. #define DEBUG
  27.  
  28. #define OPEN     0
  29. #define CLOSED   1
  30. #define OFF      0
  31. #define ON       1
  32. #define SUNRISE_DELAY (120*60)
  33. #define SUNSET_DELAY  (70*60)
  34.  
  35.  
  36. struct TimeWindow {
  37.     long int t1;
  38.     long int t2;
  39. };
  40.  
  41. LinkedList<TimeWindow> timeSchedule;
  42.  
  43. // Iterate of all TimeWindows and check if the timestamp is "in" any of them.
  44. bool checkSchedule(time_t timestamp, LinkedList<TimeWindow> *schedule) {
  45.  
  46.   bool inWindow = false;
  47.  
  48.   for (int i = 0; i < schedule->size(); i++) {
  49.     TimeWindow tw = schedule->get(i);
  50.     inWindow = timeInWindow(timestamp, &tw);
  51.  
  52.     if (inWindow) {
  53.       return true;
  54.     }
  55.   }
  56.  
  57.   return false;
  58. }
  59.  
  60. // Given a single timestamp, check if it lies within the TimeWindow
  61. bool timeInWindow(time_t timestamp, struct TimeWindow *window) {
  62.     if (timestamp > window->t1 && timestamp < window->t2) {
  63.       return true;
  64.     }
  65.  
  66.     return false;
  67. }
  68.  
  69. // Check that schedule is valid
  70. bool isScheduleValid(struct TimeWindow window) {
  71.     return true;
  72. }
  73.  
  74. // Constants
  75. //
  76. const int delayTime      = 1000;
  77. const int relayOnePin    = 1;  // Unused
  78. const int relayTwoPin    = 2;  // Door 1
  79. const int relayThreePin  = 3;  // Door 2
  80. const int relayFourPin   = 4;  // Lights
  81. const int secondsPerHour = 3600;
  82.  
  83. // Days of months array for ordinal calculation
  84. static const int monthLen[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  85.  
  86. // Globals
  87. //
  88. // Timezone rules - US Eastern Time Zone (New York, Detroit)
  89. TimeChangeRule myDST = {"CDT", Second, Sun, Mar, 2, -240};    //Daylight time = UTC - 4 hours
  90. TimeChangeRule myEST = {"CST", First, Sun, Nov, 2, -300};     //Standard time = UTC - 5 hours
  91. Timezone myTZ(myDST, myEST);
  92.  
  93. TimeChangeRule *tcr;        // pointer to the time change rule, use to get TZ abbrev
  94. time_t utc, local;
  95.  
  96. int doorState = CLOSED;
  97. int lightState = OFF;
  98.  
  99. // Init
  100. void setup()
  101. {
  102.     // Initialize Schedule
  103.     timeSchedule.add({.t1 = 2, .t2 = 4});
  104.     timeSchedule.add({.t1 = 5, .t2 = 7});
  105.     timeSchedule.add({.t1 = 10, .t2 = 11});
  106.     timeSchedule.add({.t1 = 15, .t2 = 30});
  107.     timeSchedule.add({.t1 = 40, .t2 = 100});
  108.    
  109.     // Initialize the relay pins
  110.     pinMode(relayOnePin, OUTPUT);
  111.     pinMode(relayTwoPin, OUTPUT);
  112.     pinMode(relayThreePin, OUTPUT);
  113.     pinMode(relayFourPin, OUTPUT);
  114.  
  115. #ifdef DEBUG
  116.     // Start the serial port
  117.     Serial.begin(9600);
  118.     while (!Serial)
  119.       ;
  120.     delay(200);
  121. #endif
  122.  
  123.     Serial.println("Schedule Size:");
  124.     Serial.println(timeSchedule.size());
  125.     // Set the time using the RTC's time
  126.     // setSyncProvider(RTC.get);           // Get the time from the RTC
  127.  
  128. #ifdef DEBUG
  129.     if(timeStatus()!= timeSet)          // Again, this is check the TZ lib status
  130.         Serial.println("Unable to sync with the RTC");
  131.     else
  132.         Serial.println("RTC has set the system time ");
  133. #endif
  134.  
  135.     // Door initialization based on TZ library time
  136.     // utc = now();
  137.  
  138. #ifdef DEBUG
  139.     Serial.print("Start-up time: ");
  140.     printTime(local, tcr -> abbrev);
  141. #endif
  142. }
  143.  
  144. void loop()
  145. {
  146.     bool shouldOpenDoor = false;
  147.     // Wait the current delay time
  148.     delay(delayTime);
  149.  
  150.     // get current date and time
  151.     // utc = now();
  152.     utc = (time_t) (millis() / 1000);
  153.     shouldOpenDoor = checkSchedule(utc, &timeSchedule);
  154.  
  155.     if (shouldOpenDoor) {
  156.       openDoor();
  157.     } else {
  158.       closeDoor();
  159.     }
  160. #ifdef DEBUG
  161. //    printTime(local, tcr -> abbrev);
  162. #endif
  163. }
  164.  
  165. void openDoor()
  166. {
  167.     // Is the door closed?
  168.     if (doorState == CLOSED)
  169.     {
  170. #ifdef DEBUG
  171.         Serial.print("Opening door: ");    
  172.         utc = now();
  173.         local = myTZ.toLocal(utc, &tcr);
  174.         printTime(local, tcr -> abbrev);
  175. #endif
  176.         doorState = OPEN;
  177.  
  178.         // open the door
  179.         digitalWrite(relayTwoPin, HIGH);
  180.         digitalWrite(relayThreePin, HIGH);
  181.     }
  182.  
  183.     return;
  184. }
  185.  
  186. void closeDoor()
  187. {
  188.     // Is the door open?
  189.     if (doorState == OPEN)
  190.     {
  191. #ifdef DEBUG
  192.         Serial.print("Closing door: ");    
  193.         utc = now();
  194.         local = myTZ.toLocal(utc, &tcr);
  195.         printTime(local, tcr -> abbrev);
  196. #endif
  197.            
  198.         doorState = CLOSED;
  199.  
  200.         // close the door
  201.         digitalWrite(relayTwoPin, LOW);
  202.         digitalWrite(relayThreePin, LOW);
  203.     }
  204.  
  205.     return;
  206. }
  207.  
  208.  
  209. #ifdef DEBUG
  210.  
  211. //Function to print time with time zone
  212. void printTime(time_t t, char *tz)
  213. {
  214.     sPrintI00(hour(t));
  215.     sPrintDigits(minute(t));
  216.     sPrintDigits(second(t));
  217.     Serial.print(' ');
  218.     Serial.print(dayShortStr(weekday(t)));
  219.     Serial.print(' ');
  220.     sPrintI00(day(t));
  221.     Serial.print(' ');
  222.     Serial.print(monthShortStr(month(t)));
  223.     Serial.print(' ');
  224.     Serial.print(year(t));
  225.     Serial.print(' ');
  226.     Serial.print(tz);
  227.     Serial.println();
  228. }
  229.  
  230. //Print an integer in "00" format (with leading zero).
  231. //Input value assumed to be between 0 and 99.
  232. void sPrintI00(int val)
  233. {
  234.     if (val < 10) Serial.print('0');
  235.     Serial.print(val, DEC);
  236.     return;
  237. }
  238.  
  239.  
  240.  
  241. //Print an integer in ":00" format (with leading zero).
  242. //Input value assumed to be between 0 and 99.
  243. void sPrintDigits(int val)
  244. {
  245.     Serial.print(':');
  246.     if(val < 10) Serial.print('0');
  247.     Serial.print(val, DEC);
  248. }
  249.  
  250. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement