manhoosbilli1

hatch#2 without motor

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