manhoosbilli1

Final code V4 extra functions

Aug 8th, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 14.91 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(60, turnEggFlag); //every 10 seconds
  117.   Serial.begin(9600);
  118.   //buttons
  119.   dht.begin();
  120.   Up.begin();
  121.   Down.begin();
  122.   Select.begin();
  123.   ls1.begin();
  124.   ls2.begin();
  125.   lcd.begin();
  126.   //lcd
  127.   lcd.backlight();
  128.   lcd.createChar(0, motorOn);
  129.   lcd.createChar(1, centigrade);
  130.   lcd.createChar(2, highH);
  131.   lcd.createChar(3, percentage);
  132.   lcd.createChar(4, highT);
  133.   lcd.home();
  134.   //rtc
  135.   if (!rtc.begin())
  136.   {
  137.     lcd.setCursor(0, 0);
  138.     lcd.print("Couldn't find...    ");
  139.     lcd.setCursor(0, 1);
  140.     lcd.print("RTC!  Restart!    ");
  141.     while (1)
  142.       ;
  143.   }
  144.   if (rtc.lostPower())
  145.   {
  146.     lcd.setCursor(0, 0);
  147.     lcd.print("RTC lost power   ");
  148.     lcd.setCursor(0, 1);
  149.     lcd.println("Upload Code Again  ");
  150.     // following line sets the RTC to the date & time this sketch was compiled
  151.     //rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  152.     // This line sets the RTC with an explicit date & time, for example to set
  153.     // January 21, 2014 at 3am you would call:
  154.     // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
  155.   }
  156.   EEPROM.get(10, hatchYear);
  157.   EEPROM.get(14, hatchMonth);
  158.   EEPROM.get(18, hatchDay);
  159.   //pinmodes and initialisation
  160.   pinMode(m1, OUTPUT);
  161.   pinMode(m2, OUTPUT);
  162.   pinMode(relay, OUTPUT);
  163.   pinMode(buzzer, OUTPUT);
  164.   pinMode(deHumFan, OUTPUT);
  165.   lcd.setCursor(0, 0);
  166.   lcd.print("Mortor is     ");
  167.   lcd.setCursor(0, 1);
  168.   lcd.print("Calibrating...");
  169.   while (ls1.read() == LOW && ls2.read() == LOW)
  170.   {
  171.     digitalWrite(m1, HIGH);
  172.     digitalWrite(m2, LOW);
  173.   }
  174.   lcd.setCursor(0, 0);
  175.   lcd.print("MOtor Is Now      ");
  176.   lcd.print("Calibrated      ");
  177.   digitalWrite(m1, LOW);
  178.   digitalWrite(m2, LOW);
  179.   Alarm.delay(2000);
  180.   lcd.setCursor(0, 0);
  181.   lcd.print(":)   Incubator  :) ");
  182.   lcd.setCursor(0, 1);
  183.   lcd.print(":)   Starting   :)         ");
  184.   Alarm.delay(1000);
  185. }
  186.  
  187. enum states
  188. {
  189.   STATE_IDLE,
  190.   STATE_MOVING_RIGHT,
  191.   STATE_MOVING_LEFT
  192. };
  193. int currentState = STATE_IDLE;
  194.  
  195. void loop()
  196. {
  197.   DateTime now = rtc.now();
  198.   currentMillis = millis();
  199.   Up.read();
  200.   Down.read();
  201.   Select.read();
  202.   ls1.read();
  203.   ls2.read();
  204.   updateSensor(); //will update sensor every 2 minutes.
  205.   menu();         //will take care of lcd interface
  206.   turn_once();    //will work only when called
  207.   turn();         //will turn motor few times in a day
  208.   Maintain();     //will maintain temp and humidity
  209.  
  210. }
  211.  
  212. void turnEggFlag()
  213. {
  214.  
  215.   startMotor = true;
  216.   turnCounter += 1;
  217.   turnsLeft = 3 - turnCounter;
  218.   if (turnCounter == 3) {   //if maximum number of turns reached then reset the counter
  219.     turnCounter = 1;
  220.   }
  221. }
  222.  
  223. void turn_once()
  224. {
  225.   if (turnOnce)
  226.   {
  227.     switch (currentState)
  228.     {
  229.       case STATE_IDLE:
  230.         if (ls1.read() == false && ls2.read() == true)
  231.         {
  232.           digitalWrite(m1, HIGH);
  233.           digitalWrite(m2, LOW);
  234.           currentState = STATE_MOVING_RIGHT;
  235.         }
  236.         if (ls1.read() == true && ls2.read() == false)
  237.         {
  238.           digitalWrite(m1, LOW);
  239.           digitalWrite(m2, HIGH);
  240.           currentState = STATE_MOVING_LEFT;
  241.         }
  242.         break;
  243.  
  244.       case STATE_MOVING_RIGHT:
  245.         if (ls1.read() == true && ls2.read() == false)
  246.         {
  247.           digitalWrite(m1, LOW);
  248.           digitalWrite(m2, LOW);
  249.           turnOnce = false;
  250.           currentState = STATE_IDLE;
  251.           break;
  252.         }
  253.         break;
  254.  
  255.       case STATE_MOVING_LEFT:
  256.         if (ls1.read() == false && ls2.read() == true)
  257.         {
  258.           digitalWrite(m1, LOW);
  259.           digitalWrite(m2, LOW);
  260.           turnOnce = false;
  261.           currentState = STATE_IDLE;
  262.           break;
  263.         }
  264.         break;
  265.     }
  266.   }
  267. }
  268.  
  269. void turn()
  270. {
  271.   if (startMotor)
  272.   {
  273.     switch (currentState)
  274.     {
  275.       case STATE_IDLE:
  276.         if (ls1.read() == false && ls2.read() == true)
  277.         {
  278.           digitalWrite(m1, HIGH);
  279.           digitalWrite(m2, LOW);
  280.           currentState = STATE_MOVING_RIGHT;
  281.         }
  282.         if (ls1.read() == true && ls2.read() == false)
  283.         {
  284.           digitalWrite(m1, LOW);
  285.           digitalWrite(m2, HIGH);
  286.           currentState = STATE_MOVING_LEFT;
  287.         }
  288.         break;
  289.  
  290.       case STATE_MOVING_RIGHT:
  291.         if (ls1.read() == true && ls2.read() == false)
  292.         {
  293.           digitalWrite(m1, LOW);
  294.           digitalWrite(m2, LOW);
  295.           startMotor = false;
  296.           currentState = STATE_IDLE;
  297.           break;
  298.         }
  299.         break;
  300.  
  301.       case STATE_MOVING_LEFT:
  302.         if (ls1.read() == false && ls2.read() == true)
  303.         {
  304.           digitalWrite(m1, LOW);
  305.           digitalWrite(m2, LOW);
  306.           startMotor = false;
  307.           currentState = STATE_IDLE;
  308.           break;
  309.         }
  310.         break;
  311.     }
  312.   }
  313. }
  314.  
  315. void menu()
  316. {
  317.   switch (currentPage)
  318.   {
  319.     default:
  320.       lcd.setCursor(
  321.         lcd.print("in default");
  322.         break;
  323.  
  324.       case 0:
  325.           //T print
  326.           lcd.setCursor(0, 0);
  327.           lcd.print("T:");
  328.           lcd.print(t);
  329.           lcd.write(1);
  330.           lcd.print("      "); //fill spaces till 14
  331.           //motor print
  332.           if (startMotor)
  333.       {
  334.         lcd.setCursor(12, 0);
  335.           lcd.write(0); /// flash motor on symbol
  336.         }
  337.         else
  338.         {
  339.           lcd.setCursor(12, 0);
  340.           lcd.print(".");
  341.         }
  342.       //turn counter print
  343.       lcd.setCursor(13, 0);
  344.       lcd.print(turnsLeft);
  345.       lcd.print("/");
  346.       lcd.print(3);
  347.       //humidity print
  348.       lcd.setCursor(0, 1);
  349.       lcd.print("H:");
  350.       lcd.print(h);
  351.       lcd.write(3);//percentage character
  352.       lcd.print("      ");
  353.       if (highH)
  354.     {
  355.       lcd.setCursor(14, 1);
  356.       lcd.write(2);
  357.       }
  358.       else
  359.       {
  360.         lcd.setCursor(14, 1);
  361.         lcd.print(".");
  362.       }
  363.       updateSensor();
  364.       //show humidity from sensor
  365.       if (Up.wasPressed())
  366.     {
  367.       currentPage += 1;
  368.     }
  369.     if (Down.wasPressed())
  370.     {
  371.       currentPage = 6;
  372.     }
  373.     break;
  374.  
  375.   case 1:
  376.       lcd.setCursor(0, 0);
  377.       lcd.print("Press Select To       ");
  378.       lcd.setCursor(0, 1);
  379.       lcd.print("Find HatchDay         ");
  380.       if (Up.wasPressed())
  381.       {
  382.         currentPage += 1;
  383.       }
  384.     if (Down.wasPressed())
  385.     {
  386.       currentPage -= 1;
  387.     }
  388.  
  389.     if (Select.wasPressed())
  390.     {
  391.       calcHatchDay();
  392.         showHatchDay = true;
  393.         if (showHatchDay)
  394.         {
  395.           previousMillis = millis();
  396.           while ((millis() - previousMillis) <= 3000)
  397.           {
  398.             lcd.setCursor(0, 0);
  399.             lcd.print("   Hatch Day   ");
  400.             lcd.setCursor(0, 1);
  401.             lcd.print("   ");
  402.             lcd.print(hatchYear);
  403.             lcd.print("/");
  404.             lcd.print(hatchMonth);
  405.             lcd.print("/");
  406.             lcd.print(hatchDay);
  407.             lcd.print("       ");
  408.             Alarm.delay(50);
  409.           }
  410.           showHatchDay = false;
  411.         }
  412.       }
  413.  
  414.       break;
  415.  
  416.     case 2:
  417.         lcd.setCursor(0, 0);
  418.         lcd.print("Press Select To         ");
  419.         lcd.setCursor(0, 1);
  420.         lcd.print("Turn Motor once     ");
  421.         if (Up.wasPressed())
  422.       {
  423.         currentPage += 1;
  424.       }
  425.     if (Down.wasPressed())
  426.     {
  427.       currentPage -= 1;
  428.     }
  429.  
  430.     if (Select.wasPressed())
  431.     {
  432.       turnOnce = true;
  433.       /*BeepActive = true;
  434.          previousMillia2 = millis();
  435.          beepFor(50, 50, 2000);
  436.       */
  437.     }
  438.     break;
  439.  
  440.   case 3:
  441.       lcd.setCursor(0, 0);
  442.       lcd.print("Press Select       ");
  443.       lcd.setCursor(0, 1);
  444.       lcd.print("To Run Demo               ");
  445.       if (Up.wasPressed())
  446.       {
  447.         currentPage += 1;
  448.       }
  449.     if (Down.wasPressed())
  450.     {
  451.       currentPage -= 1;
  452.     }
  453.     if (Select.wasPressed())
  454.     {
  455.       digitalWrite(buzzer, HIGH);
  456.         digitalWrite(deHumFan, HIGH);
  457.         digitalWrite(relay, HIGH);
  458.         runDemo = true;
  459.         if (runDemo)
  460.         {
  461.           previousMillis = millis();
  462.           while (millis() - previousMillis <= 3000)
  463.           {
  464.             lcd.setCursor(0, 0);
  465.             lcd.print("Demo Running      ");
  466.             lcd.setCursor(0, 1);
  467.             lcd.print("Please Wait...    ");
  468.             Alarm.delay(50);
  469.           }
  470.           runDemo = false;
  471.         }
  472.         digitalWrite(deHumFan, LOW);
  473.         digitalWrite(relay, LOW);
  474.         digitalWrite(buzzer, LOW);
  475.       }
  476.       break;
  477.  
  478.     case 4:
  479.         lcd.setCursor(0, 0);
  480.         lcd.print("Press Select To          ");
  481.         lcd.setCursor(0, 1);
  482.         lcd.print("Toggle Fan        ");
  483.         if (Up.wasPressed())
  484.       {
  485.         currentPage += 1;
  486.       }
  487.     if (Down.wasPressed())
  488.     {
  489.       currentPage -= 1;
  490.     }
  491.     if (Select.wasPressed())
  492.     {
  493.       digitalWrite(deHumFan, !digitalRead(deHumFan)); //toggle dehumidifier fan
  494.       }
  495.       break;
  496.  
  497.     case 5:
  498.         lcd.setCursor(0, 0);
  499.         lcd.print("Press Select         ");
  500.         lcd.setCursor(0, 1);
  501.         lcd.print("To Show Time         ");
  502.         if (Up.wasPressed())
  503.       {
  504.         currentPage += 1;
  505.       }
  506.     if (Down.wasPressed())
  507.     {
  508.       currentPage -= 1;
  509.     }
  510.     if (Select.wasPressed())
  511.     {
  512.       needToShowTime = true;
  513.       if (needToShowTime)
  514.         { //triggers a function which will print to lcd
  515.           previousMillis = millis();                       //and wait for said time
  516.           while ((millis() - previousMillis) <= tDuration) //show for 5 seconds
  517.           {
  518.             showTime();
  519.             Alarm.delay(50); //to slow down arduino
  520.           }
  521.           needToShowTime = false; //after we have shown the time for time we can go to normal
  522.         }
  523.       }
  524.       break;
  525.  
  526.     case 6:
  527.         lcd.setCursor(0, 0);
  528.         lcd.print("Press Select To         ");
  529.         lcd.setCursor(0, 1);
  530.         lcd.print("Toggle Light     ");
  531.         if (Up.wasPressed())
  532.       {
  533.         currentPage = 0;
  534.       }
  535.     if (Down.wasPressed())
  536.     {
  537.       currentPage -= 1;
  538.     }
  539.  
  540.     if (Select.wasPressed())
  541.     {
  542.       digitalWrite(relay, !digitalRead(relay));
  543.       }
  544.       break;
  545.   }
  546. }
  547.  
  548. void showTime()
  549. {
  550.  
  551.   DateTime now = rtc.now();
  552.   lcd.setCursor(0, 0);
  553.   lcd.print("Date:  ");
  554.   lcd.setCursor(7, 0);
  555.   lcd.print(now.year(), DEC);
  556.   lcd.print('/');
  557.   lcd.print(now.month(), DEC);
  558.   lcd.print('/');
  559.   lcd.print(now.day(), DEC);
  560.   lcd.print("            ");
  561.   lcd.setCursor(0, 1);
  562.   lcd.print("Time:  ");
  563.   lcd.setCursor(7, 1);
  564.   lcd.print("");
  565.   lcd.print(now.hour(), DEC);
  566.   lcd.print(':');
  567.   lcd.print(now.minute(), DEC);
  568.   lcd.print(':');
  569.   lcd.print(now.second(), DEC);
  570.   lcd.print("          ");
  571. }
  572.  
  573. void printTime()
  574. {
  575.   if (needToShowTime)
  576.   { //triggers a function which will print to lcd
  577.     previousMillis = millis();                       //and wait for said time
  578.     while ((millis() - previousMillis) <= tDuration) //show for 5 seconds
  579.     {
  580.       showTime();
  581.       Alarm.delay(50); //to slow down arduino
  582.     }
  583.     needToShowTime = false; //after we have shown the time for time we can go to normal
  584.   }
  585. }
  586.  
  587. void updateSensor()
  588. {
  589.   if (currentMillis - previousMillis1 > 2000)
  590.   {
  591.     h = dht.readHumidity();
  592.     t = dht.readTemperature();
  593.     if (t > setPointT)
  594.     {
  595.       tempHigh = true;
  596.     }
  597.     else
  598.     {
  599.       tempHigh = false;
  600.     }
  601.  
  602.     if (h > setPointH + 5)
  603.     {
  604.       humidityHigh = true;
  605.     } else
  606.     { humidityHigh = false;
  607.     }
  608.     previousMillis1 = currentMillis;
  609.   }
  610. }
  611.  
  612. void Maintain()
  613. {
  614.   if (tempHigh)
  615.   {
  616.     digitalWrite(relay, LOW);
  617.   }
  618.   else
  619.   {
  620.     digitalWrite(relay, HIGH);
  621.   }
  622.   if (humidityHigh)
  623.   {
  624.     digitalWrite(deHumFan, HIGH);
  625.   }
  626.   else
  627.   {
  628.     digitalWrite(deHumFan, LOW);
  629.   }
  630. }
  631.  
  632. void calcHatchDay()
  633. {
  634.   DateTime now = rtc.now();
  635.   DateTime future (now + TimeSpan(21, 12, 30, 6));
  636.   hatchYear = future.year();
  637.   hatchMonth = future.month();
  638.   hatchDay = future.day();
  639.   hatchHour = future.hour();
  640.   EEPROM.put(10, hatchYear);
  641.   EEPROM.put(14, hatchMonth);
  642.   EEPROM.put(18, hatchDay);
  643. }
  644.  
  645.  
  646. void beepFor(int onTime, int OffTime, int Duration) {   //remember to type previousMillis = millis(); just before calling the function.
  647.   if (beepActive) {
  648.     digitalWrite(buzzer, HIGH);
  649.     Alarm.delay(onTime);
  650.     digitalWrite(buzzer, LOW);
  651.     Alarm.delay(offTime);
  652.     if (currentMillis - previousMillis => Duration) {
  653.       beepActive = false;
  654.     }
  655.   }
  656. }
  657.  
  658.  
  659. void hatchDayProcedure() {
  660.   if(hatchDay == year.now() && hatchMonth == month.now && hatchDay == day.now()){
  661.     if(showHatchMsg){
  662.     previousMillis = millis()
  663.     while(millis - previousMillis <= (10* 1000)){
  664.     previousMillis1 = millis();
  665.     beepFor(100, 100, 60 * 1000);
  666.     //show_hatch_msg();
  667.     lcd.setCursor(0,0);
  668.     lcd.print("Chicks are  :)  ")
  669.     lcd.setCursor(0,1);
  670.     lcd.print("HATCHING!!!!    ");
  671.      }
  672.     }
  673.   }
  674. }
  675.  
  676.  
  677. void calcLockDay() {
  678.   DateTime now = rtc.now();
  679.   DateTime future (now + TimeSpan(18, 12, 30, 6));
  680.   lockYear = future.year();
  681.   lockMonth = future.month();
  682.   lockDay = future.day();
  683.   EEPROM.update(22, hatchYear);
  684.   EEPROM.update(26, hatchMonth);
  685.   EEPROM.update(30, hatchDay);
  686. }
Add Comment
Please, Sign In to add comment