Advertisement
Guest User

Untitled

a guest
Jun 13th, 2017
551
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.34 KB | None | 0 0
  1. // libraries
  2.  
  3.  
  4. #include <SimpleTimer.h> // load the SimpleTimer library to make timers, instead of delays & too many millis statements
  5.  
  6.  
  7. // print debug messages or not to serial
  8. const boolean SerialDisplay = true; //true or false
  9.  
  10. /*
  11. * Copyright 2016, David Naves (http://daveworks.net, http://davenaves.com)
  12. *
  13. * This program is free software; you can redistribute it and/or
  14. * modify it under the terms of the GNU General Public License
  15. * as published by the Free Software Foundation; either version 3
  16. * of the License, or (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, see
  25.  
  26. */
  27.  
  28. /*
  29. * I'm hoping that if you use/modify this code, you will share your
  30. * coop project with me and the world (pictures, whatever)
  31. * I'm big on sharing.
  32. * Cheers,
  33. * //D
  34. */
  35.  
  36.  
  37.  
  38. // pins assignments
  39.  
  40. // temperature chip i/o
  41. const int photocellPin = A0; // photocell connected to analog 0
  42. const int enableCoopDoorMotorB = 4; // enable motor b - pin 4
  43. const int directionCloseCoopDoorMotorB = 2; // direction close motor b - pin 2
  44. const int directionOpenCoopDoorMotorB = 3; // direction open motor b - pin 3
  45. const int bottomSwitchPin = 8; // bottom switch is connected to pin 8
  46. const int topSwitchPin = 9; // top switch is connected to pin 9
  47. const int relayInteriorLight = 12 ; // interior lights relay set to digital pin 12
  48.  
  49.  
  50. // variables
  51.  
  52.  
  53. // photocell
  54. int photocellReading; // analog reading of the photocel
  55. int photocellReadingLevel; // photocel reading levels (dark, twilight, light)
  56.  
  57. // reed switches top and bottom of coop door
  58.  
  59. // top switch
  60.  
  61. int topSwitchPinVal; // top switch var for reading the pin status
  62. int topSwitchPinVal2; // top switch var for reading the pin delay/debounce status
  63. int topSwitchState; // top switch var for to hold the switch state
  64.  
  65. // bottom switch
  66.  
  67. int bottomSwitchPinVal; // bottom switch var for reading the pin status
  68. int bottomSwitchPinVal2; // bottom switch var for reading the pin delay/debounce status
  69. int bottomSwitchState; // bottom switch var for to hold the switch state
  70.  
  71. // SimpleTimer objects
  72. SimpleTimer coopPhotoCellTimer;
  73.  
  74.  
  75. // debounce delay
  76. long lastDebounceTime = 0;
  77. long debounceDelay = 100;
  78.  
  79.  
  80.  
  81. // interior lights twighlight delay
  82. long lastTwilightTime = 0;
  83. long TwilightDelay = 300000; // 5 minutes
  84.  
  85.  
  86.  
  87. // ************************************** the setup **************************************
  88.  
  89. void setup(void) {
  90.  
  91. Serial.begin(9600); // initialize serial port hardware
  92.  
  93.  
  94. // welcome message
  95. if(SerialDisplay){
  96. Serial.println(" Processes running:");
  97. Serial.println(" Timer readPhotoCell every 10 minutes - light levels: open or close door");
  98. }
  99.  
  100. // coop door
  101.  
  102. // coop door motor
  103. pinMode (enableCoopDoorMotorB, OUTPUT); // enable motor pin = output
  104. pinMode (directionCloseCoopDoorMotorB, OUTPUT); // motor close direction pin = output
  105. pinMode (directionOpenCoopDoorMotorB, OUTPUT); // motor open direction pin = output
  106.  
  107. // coop door switches
  108. // bottom switch
  109. pinMode(bottomSwitchPin, INPUT); // set bottom switch pin as input
  110. digitalWrite(bottomSwitchPin, HIGH); // activate bottom switch resistor
  111.  
  112. // top switch
  113. pinMode(topSwitchPin, INPUT); // set top switch pin as input
  114. digitalWrite(topSwitchPin, HIGH); // activate top switch resistor
  115.  
  116. // interior lights relay
  117. pinMode(relayInteriorLight, OUTPUT);
  118. digitalWrite(relayInteriorLight, HIGH);
  119.  
  120. // timed actions setup
  121. coopPhotoCellTimer.setInterval(600000, readPhotoCell); // read the photocell every 10 minutes
  122.  
  123.  
  124.  
  125. }
  126.  
  127. // ************************************** functions **************************************
  128. // operate the coop door
  129.  
  130. // photocel to read levels of exterior light
  131.  
  132. void readPhotoCell() { // function to be called repeatedly - per coopPhotoCellTimer set in setup
  133.  
  134. photocellReading = analogRead(photocellPin);
  135. if(SerialDisplay){
  136. Serial.print(" Photocel Analog Reading = ");
  137. Serial.println(photocellReading);
  138. }
  139.  
  140.  
  141. // set photocel threshholds
  142. if (photocellReading >= 0 && photocellReading <= 445) {
  143. photocellReadingLevel = '1';
  144.  
  145. if(SerialDisplay){
  146. Serial.print(" Photocel Reading Level:");
  147. Serial.println(" - Dark");
  148. }
  149. }
  150. else if (photocellReading >= 450 && photocellReading <= 595){
  151. photocellReadingLevel = '2';
  152. if(SerialDisplay){
  153. Serial.print(" Photocel Reading Level:");
  154. Serial.println(" - Twilight");
  155. }
  156. }
  157. else if (photocellReading >= 600 ) {
  158. photocellReadingLevel = '3';
  159. if(SerialDisplay){
  160. Serial.print(" Photocel Reading Level:");
  161. Serial.println(" - Light");
  162. }
  163. }
  164. }
  165.  
  166. //debounce bottom reed switch
  167.  
  168. void debounceBottomReedSwitch() {
  169.  
  170. //debounce bottom reed switch
  171. bottomSwitchPinVal = digitalRead(bottomSwitchPin); // read input value and store it in val
  172. // delay(10);
  173.  
  174. if ((millis() - lastDebounceTime) > debounceDelay) { // delay 10ms for consistent readings
  175.  
  176. bottomSwitchPinVal2 = digitalRead(bottomSwitchPin); // read input value again to check or bounce
  177.  
  178. if (bottomSwitchPinVal == bottomSwitchPinVal2) { // make sure we have 2 consistant readings
  179. if (bottomSwitchPinVal != bottomSwitchState) { // the switch state has changed!
  180. bottomSwitchState = bottomSwitchPinVal;
  181. }
  182. if(SerialDisplay){
  183. Serial.print (" Bottom Switch Value: "); // display "Bottom Switch Value:"
  184. Serial.println(digitalRead(bottomSwitchPin)); // display current value of bottom switch;
  185. }
  186. }
  187. }
  188. }
  189.  
  190.  
  191.  
  192. // debounce top reed switch
  193. void debounceTopReedSwitch() {
  194.  
  195. topSwitchPinVal = digitalRead(topSwitchPin); // read input value and store it in val
  196. // delay(10);
  197.  
  198. if ((millis() - lastDebounceTime) > debounceDelay) { // delay 10ms for consistent readings
  199.  
  200. topSwitchPinVal2 = digitalRead(topSwitchPin); // read input value again to check or bounce
  201.  
  202. if (topSwitchPinVal == topSwitchPinVal2) { // make sure we have 2 consistant readings
  203. if (topSwitchPinVal != topSwitchState) { // the button state has changed!
  204. topSwitchState = topSwitchPinVal;
  205. }
  206. if(SerialDisplay){
  207. Serial.print (" Top Switch Value: "); // display "Bottom Switch Value:"
  208. Serial.println(digitalRead(topSwitchPin)); // display current value of bottom switch;
  209. }
  210. }
  211. }
  212. }
  213.  
  214.  
  215. // stop the coop door motor
  216. void stopCoopDoorMotorB(){
  217. digitalWrite (directionCloseCoopDoorMotorB, LOW); // turn off motor close direction
  218. digitalWrite (directionOpenCoopDoorMotorB, LOW); // turn on motor open direction
  219. analogWrite (enableCoopDoorMotorB, 0); // enable motor, 0 speed
  220. }
  221.  
  222.  
  223.  
  224. // close the coop door motor (motor dir close = clockwise)
  225. void closeCoopDoorMotorB() {
  226. digitalWrite (directionCloseCoopDoorMotorB, HIGH); // turn on motor close direction
  227. digitalWrite (directionOpenCoopDoorMotorB, LOW); // turn off motor open direction
  228. analogWrite (enableCoopDoorMotorB, 255); // enable motor, full speed
  229. if (bottomSwitchPinVal == 0) { // if bottom reed switch circuit is closed
  230. stopCoopDoorMotorB();
  231. if(SerialDisplay){
  232. Serial.print(" Coop Door Closed - no danger");
  233. }
  234. }
  235. }
  236.  
  237.  
  238.  
  239. // open the coop door (motor dir open = counter-clockwise)
  240. void openCoopDoorMotorB() {
  241. digitalWrite(directionCloseCoopDoorMotorB, LOW); // turn off motor close direction
  242. digitalWrite(directionOpenCoopDoorMotorB, HIGH); // turn on motor open direction
  243. analogWrite(enableCoopDoorMotorB, 255); // enable motor, full speed
  244. if (topSwitchPinVal == 0) { // if top reed switch circuit is closed
  245. stopCoopDoorMotorB();
  246. if(SerialDisplay){
  247. Serial.print(" Coop Door open - danger!");
  248. }
  249. }
  250. }
  251.  
  252.  
  253. // turn on interior lights at dusk and turn off after door shuts
  254.  
  255. void doCoopInteriorLightDusk() {
  256.  
  257. if ((millis() - lastTwilightTime) > TwilightDelay) { // delay 5 mins
  258.  
  259. readPhotoCell();
  260. bottomSwitchPinVal = digitalRead(bottomSwitchPin);
  261. if (bottomSwitchPinVal == 1 && photocellReading >= 4 && photocellReading <= 445) { // if bottom reed switch circuit is open and it's twilight
  262. digitalWrite (relayInteriorLight, HIGH);
  263. if (SerialDisplay) {
  264. Serial.println(" Interior Light: On");
  265. }
  266. }
  267. else if (bottomSwitchPinVal == 0) {
  268. digitalWrite (relayInteriorLight, LOW);
  269. if (SerialDisplay) {
  270. Serial.println(" Interior Light: Off");
  271. }
  272. }
  273. }
  274. }
  275.  
  276.  
  277.  
  278.  
  279. // do the coop door
  280. void doCoopDoor(){
  281. if (photocellReadingLevel == '1') { // if it's dark
  282. if (photocellReadingLevel != '2') { // if it's not twilight
  283. if (photocellReadingLevel != '3') { // if it's not light
  284. debounceTopReedSwitch(); // read and debounce the switches
  285. debounceBottomReedSwitch();
  286. closeCoopDoorMotorB(); // close the door
  287. }
  288. }
  289. }
  290. if (photocellReadingLevel == '3') { // if it's light
  291. if (photocellReadingLevel != '2') { // if it's not twilight
  292. if (photocellReadingLevel != '1') { // if it's not dark
  293. debounceTopReedSwitch(); // read and debounce the switches
  294. debounceBottomReedSwitch();
  295. openCoopDoorMotorB(); // Open the door
  296. }
  297. }
  298. }
  299. }
  300.  
  301.  
  302.  
  303.  
  304.  
  305. // ************************************** the loop **************************************
  306.  
  307. void loop() {
  308. coopPhotoCellTimer.run(); // timer for readPhotoCell
  309. doCoopDoor();
  310. doCoopInteriorLightDusk();
  311. //delay(2000);
  312. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement