Nealbopper

Updated Automatic Coop Door _ Linear Actuator

May 27th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.31 KB | None | 0 0
  1.  
  2. //Locking, Automatic Chicken Coop Door
  3. //By Seth Johnson Land To House llc 2016
  4.  
  5. //This section is for initializing things:
  6.  
  7. // Initialize the motor values to the pins 8, 9, 10.
  8. //Pins 8 and 9 send high or low to the motor controller.
  9. //pin 10 enables motor one on the motor controller.
  10. int enA = 10;
  11. int in1 = 9;
  12. int in2 = 8;
  13.  
  14.  
  15. //Initialize "lightSensor" as the value from pin A0 and read in the value. This is the photoresistor.
  16. int lightSensor = analogRead(A0);
  17. //The lightVal will hold the value of lightsensor in this variable
  18. int lightVal = 0;
  19.  
  20. //These are the pins for the reed switches
  21. // reed1Pin is the lower switch on the door. This is digital pin 2
  22. int reed1Pin = 2;
  23. //reed2Pin is the top switch on the door. This is digital pin 4
  24. int reed2Pin = 4;
  25. //These are the variables that hold the state of the reed switches
  26. int switchState1 = 0;
  27. int switchState2 = 0;
  28.  
  29. //This only runs once.
  30.  
  31.  
  32. #include <Wire.h>
  33. #include "ds3231.h"
  34. #include <LiquidCrystal_I2C.h>
  35.  
  36.  
  37. LiquidCrystal_I2C lcd(0x27,2,1,0,4,5,6,7); // Set the LCD I2C address
  38.  
  39. #define BUFF_MAX 128
  40.  
  41. uint8_t time[8];
  42. char recv[BUFF_MAX];
  43. unsigned int recv_size = 0;
  44. unsigned long prev, interval = 1000;
  45.  
  46. void setup()
  47. {
  48. Serial.begin(115200);
  49. Wire.begin();
  50. DS3231_init(DS3231_INTCN);
  51. memset(recv, 0, BUFF_MAX);
  52. Serial.println("GET time");
  53. lcd.begin (16,2); // for 16 x 2 LCD module
  54. lcd.setBacklightPin(3,POSITIVE);
  55. lcd.setBacklight(HIGH);
  56. lcd.clear();
  57.  
  58.  
  59. // set the motor control pins as outputs. This means pins 8, 9, 10 are outputs to the l298n motor controller.
  60. pinMode(enA, OUTPUT);
  61. pinMode(in1, OUTPUT);
  62. pinMode(in2, OUTPUT);
  63. //read the state of switch 1 (the bottom one) and place it in switchState1
  64. switchState1 = digitalRead(reed1Pin);
  65. //read the state of switch 2 (the top one) and place it in switchState2
  66. switchState2 = digitalRead(reed2Pin);
  67. //this is important to make sure that the door starts up when the program gains power.
  68. //if switchState2 is not high then go to the while statement
  69. if (switchState2 != HIGH)
  70. {
  71. // this function will run the motor down as long as switch 1 has not been triggered
  72. while (switchState2 != HIGH)
  73. {
  74. // turn on motor and set speed to 255
  75. analogWrite(enA, 255);
  76. digitalWrite(in1, LOW);
  77. digitalWrite(in2, HIGH);
  78. //read the state of the switch again to make sure that it has not been triggered
  79. switchState1 = digitalRead(reed1Pin);
  80. //read the state of switch 2 (the top one) and place it in switchState2
  81. switchState2 = digitalRead(reed2Pin);
  82. }
  83. // once switchstate2 has been triggered turn off the motor
  84. digitalWrite(in1, LOW);
  85. digitalWrite(in2, LOW);
  86. }
  87. }
  88.  
  89. //this runs over and over
  90. void loop()
  91. {
  92. //read the light sensor and place it in lightval
  93. lightVal = analogRead(lightSensor);
  94. //read the state of switch 1 (the bottom one) and place it in switchState1
  95. switchState1 = digitalRead(reed1Pin);
  96. //read the state of switch 2 (the top one) and place it in switchState2
  97. switchState2 = digitalRead(reed2Pin);
  98. //the lightSensor is read and placed into lightVal. if its less than 100 and switchState2 is
  99. //equal to high then go to the motor down code. But if the light is greater than 200 and the switchstate1
  100. //is equal to high then call motor up code
  101. if (switchState2 == HIGH && lightVal < 100)
  102. {
  103. delay(2000);
  104. motordown();
  105. }
  106. else if (switchState1 == HIGH && lightVal > 100)
  107. {
  108. delay(2000);
  109. motorup();
  110. }
  111. }
  112.  
  113. void motordown()
  114. {
  115. //Read the state of switch 1 (the Bottom one) and place it in switchState1
  116. switchState1 = digitalRead(reed1Pin);
  117. //read the state of switch 2 (the top one) and place it in switchState2
  118. switchState2 = digitalRead(reed2Pin);
  119.  
  120. //If switchState2 is high and the light is dark then continue
  121. if (switchState2 == HIGH && lightVal < 100)
  122. //wait 1 seconds
  123. delay(1000);
  124. //read the state of switch 1 (the bottom one) and place it in switchState1
  125. switchState1 = digitalRead(reed1Pin);
  126. //read the state of switch 2 (the top one) and place it in switchState2
  127. switchState2 = digitalRead(reed2Pin);
  128.  
  129. while (switchState1 != HIGH)
  130. {
  131. // turn on motor and set speed to 255
  132. analogWrite(enA, 255);
  133. digitalWrite(in1, HIGH);
  134. digitalWrite(in2, LOW);
  135. //read the state of switch 2 (the top one) and place it in switchState2
  136. switchState1 = digitalRead(reed1Pin);
  137. //read the state of switch 2 (the top one) and place it in switchState2
  138. switchState2 = digitalRead(reed2Pin);
  139. }
  140.  
  141. //wait 1 second before turning off the motor to let the locks engage at the bottom
  142. delay(1000);
  143. // now turn off motor
  144. digitalWrite(in1, LOW);
  145. digitalWrite(in2, LOW);
  146. }
  147.  
  148. void motorup()
  149. {
  150. //read the state of switch 1 (the bottom one) and place it in switchState2
  151. switchState1 = digitalRead(reed1Pin);
  152. //read the state of switch 2 (the top one) and place it in switchState2
  153. switchState2 = digitalRead(reed2Pin);
  154.  
  155. //if switchState1 is high and the light is bright then continue
  156. if (switchState1 == HIGH && lightVal > 100)
  157. {
  158.  
  159. //read the state of switch 1 (the bottom one) and place it in switchState1
  160. switchState1 = digitalRead(reed1Pin);
  161. //read the state of switch 2 (the top one) and place it in switchState2
  162. switchState2 = digitalRead(reed2Pin);
  163. //delay 1 seconds
  164. delay(1000);
  165. //while switchState2 is not high turn on the motor up
  166. while (switchState2 != HIGH)
  167. {
  168. // this function will run the motor as long as switch 2 has not been triggered
  169. // turn on motor and set speed to 255
  170. analogWrite(enA, 255);
  171. digitalWrite(in1, LOW);
  172. digitalWrite(in2, HIGH);
  173. //read the state of switch 1 (the bottom one) and place it in switchState2
  174. switchState1 = digitalRead(reed1Pin);
  175. //read the state of switch 2 (the top one) and place it in switchState2
  176. switchState2 = digitalRead(reed2Pin);
  177. }
  178. // now turn off motor
  179. digitalWrite(in1, LOW);
  180. digitalWrite(in2, LOW);
  181. }
  182. }
  183.  
  184.  
  185. void unknown_1()
  186. {
  187. char in;
  188. char tempF[6];
  189. char temperature;
  190. char buff[BUFF_MAX];
  191. unsigned long now = millis();
  192. struct ts t;
  193.  
  194. // show time once in a while
  195. if ((now - prev > interval) && (Serial.available() <= 0))
  196. {
  197. DS3231_get(&t); //Get time
  198. parse_cmd("F",1);
  199. temperature = DS3231_get_treg(); //Get temperature
  200. dtostrf(temperature, 5, 1, tempF);
  201.  
  202. lcd.clear();
  203. lcd.setCursor(1,0);
  204.  
  205. lcd.print(t.mday);
  206.  
  207. printMonth(t.mon);
  208.  
  209. lcd.print(t.year);
  210.  
  211. lcd.setCursor(0,1); //Go to second line of the LCD Screen
  212. lcd.print(t.hour);
  213. lcd.print(":");
  214. if(t.min<10)
  215. {
  216. lcd.print("0");
  217. }
  218. lcd.print(t.min);
  219. lcd.print(":");
  220. if(t.sec<10)
  221. {
  222. lcd.print("0");
  223. }
  224. lcd.print(t.sec);
  225.  
  226. lcd.print(' ');
  227. lcd.print(tempF);
  228. lcd.print((char)223);
  229. lcd.print("F ");
  230. prev = now;
  231. }
  232.  
  233.  
  234. if (Serial.available() > 0)
  235. {
  236. in = Serial.read();
  237.  
  238. if ((in == 10 || in == 13) && (recv_size > 0))
  239. {
  240. parse_cmd(recv, recv_size);
  241. recv_size = 0;
  242. recv[0] = 0;
  243. }
  244. else if (in < 48 || in > 122)
  245. {; // ignore ~[0-9A-Za-z]
  246. }
  247. else if (recv_size > BUFF_MAX - 2)
  248. { // drop lines that are too long
  249. // drop
  250. recv_size = 0;
  251. recv[0] = 0;
  252. }
  253. else if (recv_size < BUFF_MAX - 2)
  254. {
  255. recv[recv_size] = in;
  256. recv[recv_size + 1] = 0;
  257. recv_size += 1;
  258. }
  259. }
  260. }
  261.  
  262. void parse_cmd(char *cmd, int cmdsize)
  263. {
  264. uint8_t i;
  265. uint8_t reg_val;
  266. char buff[BUFF_MAX];
  267. struct ts t;
  268.  
  269. //snprintf(buff, BUFF_MAX, "cmd was '%s' %d\n", cmd, cmdsize);
  270. //Serial.print(buff);
  271.  
  272. // TssmmhhWDDMMYYYY aka set time
  273. if (cmd[0] == 84 && cmdsize == 16) {
  274. //T001408629042017
  275. t.sec = inp2toi(cmd, 1);
  276. t.min = inp2toi(cmd, 3);
  277. t.hour = inp2toi(cmd, 5);
  278. t.wday = inp2toi(cmd, 7);
  279. t.mday = inp2toi(cmd, 8);
  280. t.mon = inp2toi(cmd, 10);
  281. t.year = inp2toi(cmd, 12) * 100 + inp2toi(cmd, 14);
  282. DS3231_set(t);
  283. Serial.println("OK");
  284. } else if (cmd[0] == 49 && cmdsize == 1) { // "1" get alarm 1
  285. DS3231_get_a1(&buff[0], 59);
  286. Serial.println(buff);
  287. } else if (cmd[0] == 50 && cmdsize == 1) { // "2" get alarm 1
  288. DS3231_get_a2(&buff[0], 59);
  289. Serial.println(buff);
  290. } else if (cmd[0] == 51 && cmdsize == 1) { // "3" get aging register
  291. Serial.print("aging reg is ");
  292. Serial.println(DS3231_get_aging(), DEC);
  293. } else if (cmd[0] == 65 && cmdsize == 9) { // "A" set alarm 1
  294. DS3231_set_creg(DS3231_INTCN | DS3231_A1IE);
  295. //ASSMMHHDD
  296. for (i = 0; i < 4; i++) {
  297. time[i] = (cmd[2 * i + 1] - 48) * 10 + cmd[2 * i + 2] - 48; // ss, mm, hh, dd
  298. }
  299. byte flags[5] = { 0, 0, 0, 0, 0 };
  300. DS3231_set_a1(time[0], time[1], time[2], time[3], flags);
  301. DS3231_get_a1(&buff[0], 59);
  302. Serial.println(buff);
  303. } else if (cmd[0] == 66 && cmdsize == 7) { // "B" Set Alarm 2
  304. DS3231_set_creg(DS3231_INTCN | DS3231_A2IE);
  305. //BMMHHDD
  306. for (i = 0; i < 4; i++) {
  307. time[i] = (cmd[2 * i + 1] - 48) * 10 + cmd[2 * i + 2] - 48; // mm, hh, dd
  308. }
  309. byte flags[5] = { 0, 0, 0, 0 };
  310. DS3231_set_a2(time[0], time[1], time[2], flags);
  311. DS3231_get_a2(&buff[0], 59);
  312. Serial.println(buff);
  313. } else if (cmd[0] == 67 && cmdsize == 1) { // "C" - get temperature register
  314. Serial.print("temperature reg is ");
  315. Serial.println(DS3231_get_treg(), DEC);
  316. } else if (cmd[0] == 68 && cmdsize == 1) { // "D" - reset status register alarm flags
  317. reg_val = DS3231_get_sreg();
  318. reg_val &= B11111100;
  319. DS3231_set_sreg(reg_val);
  320. } else if (cmd[0] == 70 && cmdsize == 1) { // "F" - custom fct
  321. reg_val = DS3231_get_addr(0x5);
  322. Serial.print("orig ");
  323. Serial.print(reg_val,DEC);
  324. Serial.print("month is ");
  325. Serial.println(bcdtodec(reg_val & 0x1F),DEC);
  326. } else if (cmd[0] == 71 && cmdsize == 1) { // "G" - set aging status register
  327. DS3231_set_aging(0);
  328. } else if (cmd[0] == 83 && cmdsize == 1) { // "S" - get status register
  329. Serial.print("status reg is ");
  330. Serial.println(DS3231_get_sreg(), DEC);
  331. } else {
  332. Serial.print("unknown command prefix ");
  333. Serial.println(cmd[0]);
  334. Serial.println(cmd[0], DEC);
  335. }
  336. }
  337.  
  338. void printMonth(int month)
  339. {
  340. switch(month)
  341. {
  342. case 1: lcd.print(" January ");break;
  343. case 2: lcd.print(" February ");break;
  344. case 3: lcd.print(" March ");break;
  345. case 4: lcd.print(" April ");break;
  346. case 5: lcd.print(" May ");break;
  347. case 6: lcd.print(" June ");break;
  348. case 7: lcd.print(" July ");break;
  349. case 8: lcd.print(" August ");break;
  350. case 9: lcd.print(" September ");break;
  351. case 10: lcd.print(" October ");break;
  352. case 11: lcd.print(" November ");break;
  353. case 12: lcd.print(" December ");break;
  354. default: lcd.print(" Error ");break;
  355. }
  356. }
Advertisement
Add Comment
Please, Sign In to add comment