manhoosbilli1

Current incubator code running

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