manhoosbilli1

Final code V3

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