manhoosbilli1

working incubator code and some extra functions

Aug 14th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 13.63 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 = 60;
  39. int turns =0;
  40. int chk;
  41. int h;
  42. float t;
  43. int currentPage = 0;
  44. bool startMotor = false;
  45. unsigned long previousMillis = 0;
  46. unsigned long previousMillis1 = 0;
  47. unsigned long previousMillis2 = 0;
  48. unsigned long previousMillis3 = 0;
  49. unsigned long previousMillis4 = 0;
  50. unsigned long rememberTurn;
  51. unsigned long currentMillis = 0;
  52. unsigned int tDuration = 5000;
  53. unsigned int turnInterval = 20 * 1000;
  54. unsigned int hatchYear;
  55. unsigned int hatchMonth;
  56. unsigned int hatchDay;
  57. unsigned int hatchHour;
  58. unsigned int lockYear;
  59. unsigned int lockMonth;
  60. unsigned int lockDay;
  61. unsigned int offTime;
  62. unsigned int onTime;
  63. int turnCounter = 0;
  64. int turnsLeft;
  65. RTC_DS3231 rtc;
  66. DHT dht(DHTPIN, DHTTYPE);
  67. LiquidCrystal_I2C lcd(0x27, 16, 2);
  68. Button Up(btn_up);
  69. Button Select(btn_select);
  70. Button Down(btn_down);
  71. Button ls1(lSw1);
  72. Button ls2(lSw2);
  73. byte motorOn[8] = {
  74.   0x00,
  75.   0x00,
  76.   0x11,
  77.   0x1B,
  78.   0x15,
  79.   0x11,
  80.   0x11,
  81.   0x00
  82. };
  83. byte centigrade[8] = {
  84.   0x10,
  85.   0x06,
  86.   0x09,
  87.   0x08,
  88.   0x08,
  89.   0x09,
  90.   0x06,
  91.   0x00
  92. };
  93.  
  94. byte highT[8] =
  95. {B00100,
  96. B01010,
  97. B01010,
  98. B01110,
  99. B01110,
  100. B11111,
  101. B11111,
  102. B01110
  103. }; //thermometer icon
  104.  
  105. byte highH[8] =
  106. {B00100,
  107. B00100,
  108. B01010,
  109. B01010,
  110. B10001,
  111. B10001,
  112. B10001,
  113. B01110
  114. }; //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.   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((currentMillis - rememberTurn) >= 3,600,000){
  217.     turnEggFlag();
  218.     rememberTurn = currentMillis;
  219.   }
  220.   delay(50);
  221. }
  222.  
  223. void turnEggFlag()
  224. {
  225.   beepFor(200,200);
  226.   turns +=1;
  227.   startMotor = true;
  228.   if(turns >= 4){
  229.     turns = 0;
  230.   }
  231. }
  232.  
  233. void turn_once()
  234. {
  235.   if (turnOnce)
  236.   {
  237.     switch (currentState)
  238.     {
  239.       case STATE_IDLE:
  240.         if (ls1.read() == false && ls2.read() == true)
  241.         {
  242.           digitalWrite(m1, HIGH);
  243.           digitalWrite(m2, LOW);
  244.           currentState = STATE_MOVING_RIGHT;
  245.         }
  246.         if (ls1.read() == true && ls2.read() == false)
  247.         {
  248.           digitalWrite(m1, LOW);
  249.           digitalWrite(m2, HIGH);
  250.           currentState = STATE_MOVING_LEFT;
  251.         }
  252.         break;
  253.  
  254.       case STATE_MOVING_RIGHT:
  255.         if (ls1.read() == true && ls2.read() == false)
  256.         {
  257.           digitalWrite(m1, LOW);
  258.           digitalWrite(m2, LOW);
  259.           turnOnce = false;
  260.           currentState = STATE_IDLE;
  261.           break;
  262.         }
  263.         break;
  264.  
  265.       case STATE_MOVING_LEFT:
  266.         if (ls1.read() == false && ls2.read() == true)
  267.         {
  268.           digitalWrite(m1, LOW);
  269.           digitalWrite(m2, LOW);
  270.           turnOnce = false;
  271.           currentState = STATE_IDLE;
  272.           break;
  273.         }
  274.         break;
  275.     }
  276.   }
  277. }
  278.  
  279. void turn() {
  280.   if (startMotor)
  281.   {
  282.     switch (currentState)
  283.     {
  284.       case STATE_IDLE:
  285.         if (ls1.read() == false && ls2.read() == true)
  286.         {
  287.           digitalWrite(m1, HIGH);
  288.           digitalWrite(m2, LOW);
  289.           currentState = STATE_MOVING_RIGHT;
  290.         }
  291.         if (ls1.read() == true && ls2.read() == false)
  292.         {
  293.           digitalWrite(m1, LOW);
  294.           digitalWrite(m2, HIGH);
  295.           currentState = STATE_MOVING_LEFT;
  296.         }
  297.         break;
  298.  
  299.       case STATE_MOVING_RIGHT:
  300.         if (ls1.read() == true && ls2.read() == false)
  301.         {
  302.           digitalWrite(m1, LOW);
  303.           digitalWrite(m2, LOW);
  304.           startMotor = false;
  305.           currentState = STATE_IDLE;
  306.           break;
  307.         }
  308.         break;
  309.  
  310.       case STATE_MOVING_LEFT:
  311.         if (ls1.read() == false && ls2.read() == true)
  312.         {
  313.           digitalWrite(m1, LOW);
  314.           digitalWrite(m2, LOW);
  315.           startMotor = false;
  316.           currentState = STATE_IDLE;
  317.           break;
  318.         }
  319.         break;
  320.     }
  321.   }
  322. }
  323.  
  324. void menu()
  325. {
  326.   switch (currentPage)
  327.   {
  328.     case 0:
  329.       lcd.setCursor(0, 0);
  330.       lcd.write(4);    //temp symbol
  331.       lcd.print(t);    
  332.       lcd.write(1);    //centigrade
  333.       lcd.print("    ");
  334.       lcd.setCursor(11, 0);
  335.       lcd.write(2);    //hum symbole
  336.       lcd.print(h);
  337.       lcd.write(3);    //percentage character
  338.       lcd.setCursor(0,1);
  339.       lcd.print("Turns left: ");
  340.       lcd.print(turns);
  341.       lcd.print("/");
  342.       lcd.print("3 ");
  343.       //show countdown till hatch day
  344.       if (Up.wasPressed())
  345.       {
  346.         currentPage += 1;
  347.       }
  348.       if (Down.wasPressed())
  349.       {
  350.         currentPage = 5;
  351.       }
  352.       break;
  353.  
  354.     case 1:
  355.       lcd.setCursor(0, 0);
  356.       lcd.print("Press Select To       ");
  357.       lcd.setCursor(0, 1);
  358.       lcd.print("Show HatchDay       ");
  359.       if (Up.wasPressed())
  360.       {
  361.         currentPage += 1;
  362.       }
  363.       if (Down.wasPressed())
  364.       {
  365.         currentPage -= 1;
  366.       }
  367.  
  368.       if (Select.wasPressed())
  369.       {
  370.         calcHatchDay();
  371.         showHatchDay = true;
  372.         if (showHatchDay)
  373.         {
  374.           previousMillis = millis();
  375.           while ((millis() - previousMillis) <= 3000)
  376.           {
  377.             lcd.setCursor(0, 0);
  378.             lcd.print("    Hatch Day      ");
  379.             lcd.setCursor(0, 1);
  380.             lcd.print("   ");
  381.             lcd.print(hatchYear);
  382.             lcd.print("/");
  383.             lcd.print(hatchMonth);
  384.             lcd.print("/");
  385.             lcd.print(hatchDay);
  386.             lcd.print("      ");
  387.             delay(50);
  388.           }
  389.           showHatchDay = false;
  390.         }
  391.       }
  392.  
  393.       break;
  394.  
  395.     case 2:
  396.       lcd.setCursor(0, 0);
  397.       lcd.print("Press Select To         ");
  398.       lcd.setCursor(0, 1);
  399.       lcd.print("Turn Motor once     ");
  400.       if (Up.wasPressed())
  401.       {
  402.         currentPage += 1;
  403.       }
  404.       if (Down.wasPressed())
  405.       {
  406.         currentPage -= 1;
  407.       }
  408.  
  409.       if (Select.wasPressed())
  410.       {
  411.  
  412.         beepFor(500, 500);
  413.         turnOnce = true;
  414.  
  415.       }
  416.       break;
  417.  
  418.     case 3:
  419.       lcd.setCursor(0, 0);
  420.       lcd.print("Press Select To          ");
  421.       lcd.setCursor(0, 1);
  422.       lcd.print("Show Lock day        ");
  423.       if (Up.wasPressed())
  424.       {
  425.         currentPage += 1;
  426.       }
  427.       if (Down.wasPressed())
  428.       {
  429.         currentPage -= 1;
  430.       }
  431.       if (Select.wasPressed())
  432.       {
  433.         calcLockDay();
  434.         showLockDay = true;
  435.         if (showLockDay)
  436.         {
  437.           previousMillis = millis();
  438.           while ((millis() - previousMillis) <= 3000)
  439.           {
  440.             lcd.setCursor(0, 0);
  441.             lcd.print("   Lock Day   ");
  442.             lcd.setCursor(0, 1);
  443.             lcd.print("   ");
  444.             lcd.print(lockYear);
  445.             lcd.print("/");
  446.             lcd.print(lockMonth);
  447.             lcd.print("/");
  448.             lcd.print(lockDay);
  449.             delay(50);
  450.           }
  451.           showLockDay = false;
  452.         }
  453.       }
  454.       break;
  455.  
  456.     case 4:
  457.       lcd.setCursor(0, 0);
  458.       lcd.print("Press Select         ");
  459.       lcd.setCursor(0, 1);
  460.       lcd.print("To Show Time         ");
  461.       if (Up.wasPressed())
  462.       {
  463.         currentPage += 1;
  464.       }
  465.       if (Down.wasPressed())
  466.       {
  467.         currentPage -= 1;
  468.       }
  469.       if (Select.wasPressed())
  470.       {
  471.         needToShowTime = true;
  472.         if (needToShowTime)
  473.         { //triggers a function which will print to lcd
  474.           previousMillis3 = millis();                       //and wait for said time
  475.           while ((millis() - previousMillis3) <= tDuration) //show for 5 seconds
  476.           {
  477.             showTime();
  478.             delay(50); //to slow down arduino
  479.           }
  480.           needToShowTime = false; //after we have shown the time for time we can go to normal
  481.         }
  482.       }
  483.       break;
  484.  
  485.     case 5:
  486.       lcd.setCursor(0, 0);
  487.       lcd.print("Press Select To         ");
  488.       lcd.setCursor(0, 1);
  489.       lcd.print("Toggle Light     ");
  490.       if (Up.wasPressed())
  491.       {
  492.         currentPage = 0;
  493.       }
  494.       if (Down.wasPressed())
  495.       {
  496.         currentPage -= 1;
  497.       }
  498.  
  499.       if (Select.wasPressed())
  500.       {
  501.         digitalWrite(relay, !digitalRead(relay));
  502.       }
  503.       break;
  504.   }
  505. }
  506.  
  507. void showTime()
  508. {
  509.  
  510.   DateTime now = rtc.now();
  511.   lcd.setCursor(0, 0);
  512.   lcd.print("Date:  ");
  513.   lcd.setCursor(7, 0);
  514.   lcd.print(now.year(), DEC);
  515.   lcd.print('/');
  516.   lcd.print(now.month(), DEC);
  517.   lcd.print('/');
  518.   lcd.print(now.day(), DEC);
  519.   lcd.print("            ");
  520.   lcd.setCursor(0, 1);
  521.   lcd.print("Time:  ");
  522.   lcd.setCursor(7, 1);
  523.   lcd.print("");
  524.   lcd.print(now.hour(), DEC);
  525.   lcd.print(':');
  526.   lcd.print(now.minute(), DEC);
  527.   lcd.print(':');
  528.   lcd.print(now.second(), DEC);
  529.   lcd.print("          ");
  530. }
  531.  
  532.  
  533. void updateSensor()
  534. {
  535.   if (currentMillis - previousMillis1 > 2000)
  536.   {
  537.     h = dht.readHumidity();
  538.     t = dht.readTemperature();
  539.     if (t > setPointT)
  540.     {
  541.       tempHigh = true;
  542.     }
  543.     else
  544.     {
  545.       tempHigh = false;
  546.     }
  547.  
  548.     if (h > setPointH + 5)
  549.     {
  550.       humidityHigh = true;
  551.     } else
  552.     {
  553.       humidityHigh = false;
  554.     }
  555.     previousMillis1 = currentMillis;
  556.   }
  557. }
  558.  
  559. void Maintain()
  560. {
  561.   if (tempHigh)
  562.   {
  563.     digitalWrite(relay, LOW);
  564.   }
  565.   else
  566.   {
  567.     digitalWrite(relay, HIGH);
  568.   }
  569.   if (humidityHigh)
  570.   {
  571.     digitalWrite(deHumFan, HIGH);
  572.   }
  573.   else
  574.   {
  575.     digitalWrite(deHumFan, LOW);
  576.   }
  577. }
  578.  
  579. void calcHatchDay()
  580. {
  581.   DateTime now = rtc.now();
  582.   DateTime future (now + TimeSpan(21, 12, 30, 6));
  583.   hatchYear = future.year();
  584.   hatchMonth = future.month();
  585.   hatchDay = future.day();
  586.   hatchHour = future.hour();
  587.   EEPROM.update(0, hatchYear);
  588.   EEPROM.update(14, hatchMonth);
  589.   EEPROM.update(18, hatchDay);
  590. }
  591.  
  592.  
  593. void beepFor(int onTime, int OffTime) {   //remember to type previousMillis = millis(); just before calling the function.
  594.   digitalWrite(buzzer, HIGH);
  595.   delay(onTime);
  596.   digitalWrite(buzzer, LOW);
  597.   delay(offTime);
  598. }
  599.  
  600.  
  601. void calcLockDay() {
  602.   DateTime now = rtc.now();
  603.   DateTime future (now + TimeSpan(18, 12, 30, 6));
  604.   lockYear = future.year();
  605.   lockMonth = future.month();
  606.   lockDay = future.day();
  607.   EEPROM.update(22, lockYear);
  608.   EEPROM.update(26, lockMonth);
  609.   EEPROM.update(30, lockDay);
  610. }
Add Comment
Please, Sign In to add comment