manhoosbilli1

code for new hatch

Sep 14th, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.77 KB | None | 0 0
  1. /*
  2. You are free to modify the code as much as you want and share it with other people if you want. Try to add any improvements and bugs to the original git hub repository so that i can take a look as well, of course this is not a requirement.
  3. do not delete this comment section, as this will help the next reader link to the original post and original content. This is necessary to avoid any confusion later on. Happy tinkering!
  4. Youtube Channel: https://www.youtube.com/channel/UCtvpQinm9lOqgLaEp4WIo-w?
  5. Github Repository: https://github.com/manhoosbilli1/Arduino-Incubator
  6. Arduino-forum thread: https://forum.arduino.cc/index.php?topic=631158.0
  7. // Written by manhoosbilli1 aka kaka, public domain
  8. */
  9.  
  10. #include <DHT.h>;
  11. #include <EEPROM.h>
  12. #include <Wire.h>
  13. #include <LiquidCrystal_I2C.h>
  14. #include <TimeLib.h>
  15. #include "RTClib.h"
  16. #include <JC_Button.h>
  17. #define DHTPIN 2 // what pin we're connected to
  18. #define DHTTYPE DHT22 // DHT 22 (AM2302)
  19. #define btn_up 12
  20. #define btn_down 11
  21. #define btn_select 10
  22. #define lSw1 8
  23. #define lSw2 9
  24. #define m1 5
  25. #define m2 6
  26. #define relay 3
  27. #define buzzer A1
  28. #define deHumFan A3
  29. bool runDemo = false;
  30. bool showHatchDay = 0;
  31. bool needToShowTime = 0;
  32. bool updAlarm;
  33. bool turnOnce = false;
  34. bool humidityHigh = false;
  35. bool tempHigh = false;
  36. bool showLockDay = false;
  37. float setPointT = 37.5;
  38. int setPointH = 45;
  39. int turns = 0;
  40. int chk;
  41. int h;
  42. int lockDayH = 60;
  43. float t;
  44. boolean humidityLow = true;
  45. boolean isLockDay = false;
  46. boolean tempVeryHigh=false;
  47. int currentPage = 0;
  48. bool startMotor = false;
  49. unsigned long previousMillis = 0;
  50. unsigned long previousMillis1 = 0;
  51. unsigned long previousMillis2 = 0;
  52. unsigned long previousMillis3 = 0;
  53. unsigned long previousMillis4 = 0;
  54. unsigned long rememberTurn;
  55. unsigned long currentMillis = 0;
  56. unsigned int tDuration = 5000;
  57. unsigned int turnInterval = 20 * 1000;
  58. unsigned int hatchYear;
  59. unsigned int hatchMonth;
  60. unsigned int hatchDay;
  61. unsigned int hatchHour;
  62. unsigned int lockYear;
  63. unsigned int lockMonth;
  64. unsigned int lockDay;
  65. unsigned int offTime;
  66. unsigned int onTime;
  67. int turnCounter = 0;
  68. int turnsLeft;
  69. RTC_DS3231 rtc;
  70. DHT dht(DHTPIN, DHTTYPE);
  71. LiquidCrystal_I2C lcd(0x27, 16, 2);
  72. Button Up(btn_up);
  73. Button Select(btn_select);
  74. Button Down(btn_down);
  75. Button ls1(lSw1);
  76. Button ls2(lSw2);
  77. byte motorOn[8] = {
  78. 0x00,
  79. 0x00,
  80. 0x11,
  81. 0x1B,
  82. 0x15,
  83. 0x11,
  84. 0x11,
  85. 0x00
  86. };
  87. byte centigrade[8] = {
  88. 0x10,
  89. 0x06,
  90. 0x09,
  91. 0x08,
  92. 0x08,
  93. 0x09,
  94. 0x06,
  95. 0x00
  96. };
  97.  
  98. byte highT[8] =
  99. { B00100,
  100. B01010,
  101. B01010,
  102. B01110,
  103. B01110,
  104. B11111,
  105. B11111,
  106. B01110
  107. }; //thermometer icon
  108.  
  109. byte highH[8] =
  110. { B00100,
  111. B00100,
  112. B01010,
  113. B01010,
  114. B10001,
  115. B10001,
  116. B10001,
  117. B01110
  118. }; //drop icon
  119.  
  120. byte percentage [8] = {
  121. 0x00,
  122. 0x00,
  123. 0x01,
  124. 0x0A,
  125. 0x04,
  126. 0x0A,
  127. 0x10,
  128. 0x00
  129. };
  130.  
  131.  
  132. void setup()
  133. {
  134. // put your setup code here, to run once:
  135. Serial.begin(9600);
  136. //buttons
  137. dht.begin();
  138. Up.begin();
  139. Down.begin();
  140. Select.begin();
  141. ls1.begin();
  142. ls2.begin();
  143. lcd.begin();
  144. //lcd
  145. lcd.backlight();
  146. lcd.createChar(0, motorOn);
  147. lcd.createChar(1, centigrade);
  148. lcd.createChar(2, highH);
  149. lcd.createChar(3, percentage);
  150. lcd.createChar(4, highT);
  151. lcd.home();
  152. EEPROM.get(10, hatchYear);
  153. EEPROM.get(14, hatchMonth);
  154. EEPROM.get(18, hatchDay);
  155. EEPROM.get(22, lockYear);
  156. EEPROM.get(26, lockMonth);
  157. EEPROM.get(30, lockDay);
  158. //rtc
  159. if (!rtc.begin())
  160. {
  161. lcd.setCursor(0, 0);
  162. lcd.print("Couldn't find... ");
  163. lcd.setCursor(0, 1);
  164. lcd.print("RTC! Restart! ");
  165. while (1)
  166. ;
  167. }
  168. if (rtc.lostPower())
  169. {
  170. lcd.setCursor(0, 0);
  171. lcd.print("RTC lost power ");
  172. lcd.setCursor(0, 1);
  173. lcd.println("Upload Code Again ");
  174. // following line sets the RTC to the date & time this sketch was compiled
  175. // rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  176. // This line sets the RTC with an explicit date & time, for example to set
  177. // January 21, 2014 at 3am you would call:
  178. // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
  179. }
  180.  
  181. //pinmodes and initialisation
  182. pinMode(m1, OUTPUT);
  183. pinMode(m2, OUTPUT);
  184. pinMode(relay, OUTPUT);
  185. pinMode(buzzer, OUTPUT);
  186. pinMode(deHumFan, OUTPUT);
  187. /*
  188. lcd.setCursor(0, 0);
  189. lcd.print("Motor is ");
  190. lcd.setCursor(0, 1);
  191. lcd.print("Calibrating...");
  192. while (ls1.read() == LOW && ls2.read() == LOW) {
  193. digitalWrite(m1, HIGH);
  194. digitalWrite(m2, LOW);
  195. }
  196. lcd.setCursor(0, 0);
  197. lcd.print("Motor Is Now ");
  198. lcd.print("Calibrated ");
  199. digitalWrite(m1, LOW);
  200. digitalWrite(m2, LOW);
  201. */
  202. delay(2000);
  203. }
  204.  
  205. enum states
  206. {STATE_IDLE, STATE_MOVING_RIGHT, STATE_MOVING_LEFT};
  207. int currentState = STATE_IDLE;
  208.  
  209. void loop()
  210. {
  211. DateTime now = rtc.now();
  212. currentMillis = millis();
  213. Up.read();
  214. Down.read();
  215. Select.read();
  216. ls1.read();
  217. ls2.read();
  218. updateSensor(); //will update sensor every 2 minutes.
  219. menu(); //will take care of lcd interface
  220. Maintain(); //will maintain temp and humidity
  221. ifIsLockDay();
  222. if(alarmOn == true && SelectBtn.pressedFor(1000)){
  223. alarmOn = false;
  224. timer = millis();
  225. }
  226. if(alarmOn == false){
  227. if(currentMillis - timer >= 5000){
  228. alarmOn = true;
  229. }
  230. }
  231.  
  232. void turnEggFlag()
  233. {
  234. beepFor(200, 200);
  235. startMotor = true;
  236.  
  237. }
  238.  
  239. void turn_once()
  240. {
  241. if (turnOnce)
  242. {
  243. switch (currentState)
  244. {
  245. case STATE_IDLE:
  246. if (ls1.read() == false && ls2.read() == true)
  247. {
  248. digitalWrite(m1, HIGH);
  249. digitalWrite(m2, LOW);
  250. currentState = STATE_MOVING_RIGHT;
  251. }
  252. if (ls1.read() == true && ls2.read() == false)
  253. {
  254. digitalWrite(m1, LOW);
  255. digitalWrite(m2, HIGH);
  256. currentState = STATE_MOVING_LEFT;
  257. }
  258. break;
  259.  
  260. case STATE_MOVING_RIGHT:
  261. if (ls1.read() == true && ls2.read() == false)
  262. {
  263. digitalWrite(m1, LOW);
  264. digitalWrite(m2, LOW);
  265. turnOnce = false;
  266. currentState = STATE_IDLE;
  267. break;
  268. }
  269. break;
  270.  
  271. case STATE_MOVING_LEFT:
  272. if (ls1.read() == false && ls2.read() == true)
  273. {
  274. digitalWrite(m1, LOW);
  275. digitalWrite(m2, LOW);
  276. turnOnce = false;
  277. currentState = STATE_IDLE;
  278. break;
  279. }
  280. break;
  281. }
  282. }
  283. }
  284.  
  285. void turn() {
  286. if (startMotor)
  287. {
  288. switch (currentState)
  289. {
  290. case STATE_IDLE:
  291. if (ls1.read() == false && ls2.read() == true)
  292. {
  293. digitalWrite(m1, HIGH);
  294. digitalWrite(m2, LOW);
  295. currentState = STATE_MOVING_RIGHT;
  296. }
  297. if (ls1.read() == true && ls2.read() == false)
  298. {
  299. digitalWrite(m1, LOW);
  300. digitalWrite(m2, HIGH);
  301. currentState = STATE_MOVING_LEFT;
  302. }
  303. break;
  304.  
  305. case STATE_MOVING_RIGHT:
  306. if (ls1.read() == true && ls2.read() == false)
  307. {
  308. digitalWrite(m1, LOW);
  309. digitalWrite(m2, LOW);
  310. startMotor = false;
  311. currentState = STATE_IDLE;
  312. break;
  313. }
  314. break;
  315.  
  316. case STATE_MOVING_LEFT:
  317. if (ls1.read() == false && ls2.read() == true)
  318. {
  319. digitalWrite(m1, LOW);
  320. digitalWrite(m2, LOW);
  321. startMotor = false;
  322. currentState = STATE_IDLE;
  323. break;
  324. }
  325. break;
  326. }
  327. }
  328. }
  329.  
  330. void menu()
  331. {
  332. switch (currentPage) {
  333. case 0:
  334. //screen messages
  335. lcd.setCursor(0, 0);
  336. lcd.write(4); //temp symbol
  337. lcd.print(t);
  338. lcd.write(1); //centigrade
  339. lcd.print(" ");
  340. lcd.setCursor(11, 0);
  341. lcd.write(2); //hum symbol
  342. lcd.print(h);
  343. lcd.write(3); //percentage character
  344. if (isLockDay == true && tempVeryHighMsg == false) {
  345. lcd.setCursor(0, 1);
  346. lcd.print("No turns Please ");
  347. } else if (isLockDay == false && tempVeryHighMsg == false) {
  348. lcd.setCursor(0, 1);
  349. lcd.print("Turns left: ");
  350. lcd.print(turns);
  351. lcd.print("/");
  352. lcd.print("5 ");
  353. } else if (isLockDay == true && tempVeryHighMsg == true) {
  354. lcd.setCursor(0,1);
  355. lcd.print("Temp Very High ");
  356. } else if (isLockDay == false && tempVeryHighMsg == true) {
  357. lcd.setCursor(0,1);
  358. lcd.print("Temp Very High ");
  359. }
  360. //show countdown till hatch day
  361. if (Up.wasPressed())
  362. {
  363. currentPage += 1;
  364. }
  365. if (Down.wasPressed())
  366. {
  367. currentPage = 4;
  368. }
  369. if (Select.wasPressed()) {
  370. turns += 1;
  371. if (turns >= 5 && Select.wasPressed()) {
  372. turns = 0;
  373. }
  374. }
  375. break;
  376.  
  377. case 1:
  378. lcd.setCursor(0, 0);
  379. lcd.print("Press Select To ");
  380. lcd.setCursor(0, 1);
  381. lcd.print("Calc HatchDay ");
  382. if (Up.wasPressed())
  383. {
  384. currentPage += 1;
  385. }
  386. if (Down.wasPressed())
  387. {
  388. currentPage -= 1;
  389. }
  390.  
  391. if (Select.wasPressed())
  392. {
  393. calcLockDay();
  394. calcHatchDay();
  395. lcd.setCursor(0,0);
  396. lcd.print("Calculating ");
  397. lcd.setCursor(0,1);
  398. lcd.print("Hatch Day Now ")
  399. delay(2000);
  400. }
  401.  
  402. break;
  403.  
  404. case 2:
  405. lcd.setCursor(0, 0);
  406. lcd.print("Press Select To ");
  407. lcd.setCursor(0, 1);
  408. lcd.print("Show Hatch Day ");
  409. if (Up.wasPressed())
  410. {
  411. currentPage += 1;
  412. }
  413. if (Down.wasPressed())
  414. {
  415. currentPage -= 1;
  416. }
  417.  
  418. if (Select.wasPressed())
  419. {
  420.  
  421. /*beepFor(500, 500);
  422. turnOnce = true;
  423. */
  424. showHatchDay = true;
  425. if (showHatchDay)
  426. {
  427. previousMillis = millis();
  428. while ((millis() - previousMillis) <= 3000)
  429. {
  430. lcd.setCursor(0, 0);
  431. lcd.print(" Hatch Day ");
  432. lcd.setCursor(0, 1);
  433. lcd.print(" ");
  434. lcd.print(hatchYear);
  435. lcd.print("/");
  436. lcd.print(hatchMonth);
  437. lcd.print("/");
  438. lcd.print(hatchDay);
  439. lcd.print(" ");
  440. delay(50);
  441. }
  442. showHatchDay = false;
  443. }
  444. }
  445. break;
  446.  
  447. case 3:
  448. lcd.setCursor(0, 0);
  449. lcd.print("Press Select To ");
  450. lcd.setCursor(0, 1);
  451. lcd.print("Show Lock day ");
  452. if (Up.wasPressed())
  453. {
  454. currentPage += 1;
  455. }
  456. if (Down.wasPressed())
  457. {
  458. currentPage -= 1;
  459. }
  460. if (Select.wasPressed())
  461. {
  462. showLockDay = true;
  463. if (showLockDay)
  464. {
  465. previousMillis = millis();
  466. while ((millis() - previousMillis) <= 3000)
  467. {
  468. lcd.setCursor(0, 0);
  469. lcd.print(" Lock Day ");
  470. lcd.setCursor(0, 1);
  471. lcd.print(" ");
  472. lcd.print(lockYear);
  473. lcd.print("/");
  474. lcd.print(lockMonth);
  475. lcd.print("/");
  476. lcd.print(lockDay);
  477. lcd.print(" ");
  478. delay(50);
  479. }
  480. showLockDay = false;
  481. }
  482. }
  483. break;
  484.  
  485. case 4:
  486. lcd.setCursor(0, 0);
  487. lcd.print("Press Select ");
  488. lcd.setCursor(0, 1);
  489. lcd.print("To Show Time ");
  490. if (Up.wasPressed())
  491. {
  492. currentPage = 0;
  493. }
  494. if (Down.wasPressed())
  495. {
  496. currentPage -= 1;
  497. }
  498. if (Select.wasPressed())
  499. {
  500. needToShowTime = true;
  501. if (needToShowTime)
  502. { //triggers a function which will print to lcd
  503. previousMillis3 = millis(); //and wait for said time
  504. while ((millis() - previousMillis3) <= tDuration) //show for 5 seconds
  505. {
  506. showTime();
  507. delay(50); //to slow down arduino
  508. }
  509. needToShowTime = false; //after we have shown the time for time we can go to normal
  510. }
  511. }
  512.  
  513. break;
  514. }
  515. }
  516.  
  517. void showTime()
  518. {
  519.  
  520. DateTime now = rtc.now();
  521. lcd.setCursor(0, 0);
  522. lcd.print("Date: ");
  523. lcd.setCursor(7, 0);
  524. lcd.print(now.year(), DEC);
  525. lcd.print('/');
  526. lcd.print(now.month(), DEC);
  527. lcd.print('/');
  528. lcd.print(now.day(), DEC);
  529. lcd.print(" ");
  530. lcd.setCursor(0, 1);
  531. lcd.print("Time: ");
  532. lcd.setCursor(7, 1);
  533. lcd.print("");
  534. lcd.print(now.hour(), DEC);
  535. lcd.print(':');
  536. lcd.print(now.minute(), DEC);
  537. lcd.print(':');
  538. lcd.print(now.second(), DEC);
  539. lcd.print(" ");
  540. }
  541.  
  542.  
  543. void updateSensor()
  544. {
  545. if (currentMillis - previousMillis1 > 2000)
  546. {
  547. h = dht.readHumidity();
  548. t = dht.readTemperature();
  549. if (t > setPointT)
  550. {
  551. tempHigh = true;
  552. }
  553. else
  554. {
  555. tempHigh = false;
  556. }
  557.  
  558. if(t>=setPoint+2){
  559. tempVeryHigh = true;
  560. } else {
  561. tempVeryHigh = false;
  562. }
  563.  
  564. if (h > (setPointH + 5))
  565. {
  566. humidityHigh = true;
  567. } else {
  568. humidityHigh = false;
  569. }
  570.  
  571. if (h < (setPointH - 5)) {
  572. humidityLow = true;
  573. } else
  574. {
  575. humidityLow = false;
  576. }
  577.  
  578. previousMillis1 = currentMillis;
  579. }
  580. }
  581.  
  582. void Maintain()
  583. {
  584. if (tempHigh)
  585. {
  586. digitalWrite(relay, LOW);
  587. }
  588. else
  589. {
  590. digitalWrite(relay, HIGH);
  591. }
  592. if (humidityHigh)
  593. {
  594. digitalWrite(deHumFan, HIGH);
  595. }
  596. else
  597. {
  598. digitalWrite(deHumFan, LOW);
  599. }
  600. if (humidityLow == true && alarmOn == true) {
  601. beepFor(200, 200);
  602. }
  603. if(tempVeryHigh == true && alarmOn == true) {
  604. beepFor(100, 100);
  605. tempVeryHighMsg = true;
  606. } else {
  607. tempVeryHighMsg = false;
  608. }
  609. }
  610.  
  611. void calcHatchDay()
  612. {
  613. DateTime now = rtc.now();
  614. DateTime future (now + TimeSpan(20, 12, 30, 6));
  615. hatchYear = future.year();
  616. hatchMonth = future.month();
  617. hatchDay = future.day();
  618. hatchHour = future.hour();
  619. EEPROM.update(0, hatchYear);
  620. EEPROM.update(14, hatchMonth);
  621. EEPROM.update(18, hatchDay);
  622. }
  623.  
  624.  
  625. void beepFor(int onTime, int OffTime) { //remember to type previousMillis = millis(); just before calling the function.
  626. digitalWrite(buzzer, HIGH);
  627. delay(onTime);
  628. digitalWrite(buzzer, LOW);
  629. delay(offTime);
  630. }
  631.  
  632.  
  633. void calcLockDay() {
  634. DateTime now = rtc.now();
  635. DateTime future (now + TimeSpan(17, 12, 30, 6));
  636. lockYear = future.year();
  637. lockMonth = future.month();
  638. lockDay = future.day();
  639. EEPROM.update(22, lockYear);
  640. EEPROM.update(26, lockMonth);
  641. EEPROM.update(30, lockDay);
  642. }
  643.  
  644. void ifIsLockDay() {
  645. DateTime now = rtc.now();
  646. if (now.day() == lockDay && now.month() == lockMonth) {
  647. isLockDay = true;
  648. setPointH = 60;
  649. }
  650. }
Add Comment
Please, Sign In to add comment