Nealbopper

Arduino Coop Door Project code

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