manhoosbilli1

Incubator fully tested and working

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