manhoosbilli1

EEPROM ADDED

Jul 19th, 2019
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.50 KB | None | 0 0
  1. #include <EEPROM.h>
  2. #include <Wire.h>
  3. #include <LiquidCrystal_I2C.h>
  4. #include <TimeLib.h>
  5. #include <TimeAlarms.h>
  6. #include "RTClib.h"
  7. #include <JC_Button.h>
  8. #include "DHT.h"
  9. #define DHTPIN 7    // what pin we're connected to
  10. #define DHTTYPE DHT22   // DHT 22  (AM2302)
  11. #define btn_up 11
  12. #define btn_down  12
  13. #define btn_select 10
  14. bool showHatchDay = 0;
  15. bool needToShowTime = 0;
  16. bool updAlarm;
  17. float h;
  18. float t;
  19. int currentPage = 0;
  20.  
  21. unsigned long previousMillis = 0;
  22. unsigned int tDuration = 5000;
  23. unsigned int hatchYear;
  24. unsigned int hatchMonth;
  25. unsigned int hatchDay;
  26. unsigned int hatchHour;
  27.  
  28. RTC_DS3231 rtc;
  29. DHT dht(DHTPIN, DHTTYPE);
  30. LiquidCrystal_I2C lcd(0x27, 16, 2);
  31. AlarmId id;
  32. Button Up(btn_up);
  33. Button Select(btn_select);
  34. Button Down(btn_down);
  35.  
  36.  
  37.  
  38. void setup() {
  39.   // put your setup code here, to run once:
  40.   DateTime now = rtc.now();
  41.   Up.begin();
  42.   Down.begin();
  43.   Select.begin();
  44.   Serial.begin(9600);
  45.   lcd.begin();
  46.   lcd.backlight();
  47.   lcd.setCursor(0, 0);
  48.   lcd.print("   Incubator   ");
  49.   lcd.setCursor(0, 1);
  50.   lcd.print("   Starting   ");
  51.   EEPROM.get(0, hatchYear);
  52.   EEPROM.get(4, hatchMonth);
  53.   EEPROM.get(8, hatchDay);
  54.   Alarm.delay(2000);
  55. }
  56.  
  57.  
  58. void loop() {
  59.  
  60.   Up.read();
  61.   Down.read();
  62.   Select.read();
  63.   DateTime now = rtc.now();
  64.   //RTC code
  65.  
  66.   if (! rtc.begin()) {
  67.     lcd.clear();
  68.     lcd.print("Couldn't find RTC");
  69.     while (1);
  70.   }
  71.  
  72.   if (rtc.lostPower()) {
  73.     lcd.clear();
  74.     lcd.print("RTC lost power");
  75.     // following line sets the RTC to the date & time this sketch was compiled
  76.     //rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  77.     // This line sets the RTC with an explicit date & time, for example to set
  78.     // January 21, 2014 at 3am you would call:
  79.     // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
  80.   }
  81.  
  82.   //RTC code finished
  83.   menu();
  84.  
  85. }
  86.  
  87.  
  88.  
  89.  
  90.  
  91.  
  92.  
  93.  
  94. void menu() {
  95.   switch (currentPage) {
  96.     default:
  97.       Serial.println("in default");
  98.       break;
  99.  
  100.     case 0:
  101.       //show status day remaining for hatch
  102.       //show time remaining for next turn
  103.       lcd.setCursor(0, 0);
  104.       lcd.print("T: ");
  105.       lcd.print("38 C");
  106.       lcd.setCursor(0, 1);
  107.       lcd.print("H: ");
  108.       lcd.print("56%");
  109.       lcd.print(hatchYear, DEC);
  110.       lcd.print("/");
  111.       lcd.print(hatchMonth, DEC);
  112.       lcd.print("/");
  113.       lcd.print(hatchDay, DEC);
  114.  
  115.       //show humidity from sensor
  116.       if (Up.wasPressed()) {
  117.         currentPage += 1;
  118.       }
  119.       if (Down.wasPressed()) {
  120.         currentPage = 5;
  121.       }
  122.       break;
  123.  
  124.     case 1:
  125.       lcd.setCursor(0, 0);
  126.       lcd.print("Press Select To         ");
  127.       lcd.setCursor(0, 1);
  128.       lcd.print("Find HatchDay         ");
  129.       if (Up.wasPressed()) {
  130.         currentPage += 1;
  131.       }
  132.       if (Down.wasPressed()) {
  133.         currentPage -= 1;
  134.       }
  135.  
  136.       if (Select.wasPressed()) {
  137.         DateTime now = rtc.now();
  138.         DateTime future (now.unixtime() + 21 * 86400L + 30);
  139.         hatchYear = future.year();
  140.         hatchMonth = future.month();
  141.         hatchDay = future.day();
  142.         hatchHour = future.hour();
  143.         EEPROM.put(0, hatchYear);
  144.         EEPROM.put(4, hatchMonth);
  145.         EEPROM.put(8, hatchDay);
  146.         showHatchDay = true;
  147.         if (showHatchDay) {
  148.           previousMillis = millis();
  149.           while ((millis() - previousMillis) <= 3000) {
  150.             lcd.setCursor(0, 0);
  151.             lcd.print("   Hatch Day   ");
  152.             lcd.setCursor(0, 1);
  153.             lcd.print("   ");
  154.             lcd.print(future.year(), DEC);
  155.             lcd.print("/");
  156.             lcd.print(future.month(), DEC);
  157.             lcd.print("/");
  158.             lcd.print(future.day(), DEC);
  159.             lcd.print("       ");
  160.             delay(50);
  161.           }
  162.           showHatchDay = false;
  163.         }
  164.       }
  165.  
  166.       break;
  167.  
  168.     case 2:
  169.       lcd.setCursor(0, 0);
  170.       lcd.print("Press Select          ");
  171.       lcd.setCursor(0, 1);
  172.       lcd.print("To Turn Motor     ");
  173.       if (Up.wasPressed()) {
  174.         currentPage += 1;
  175.       }
  176.       if (Down.wasPressed()) {
  177.         currentPage -= 1;
  178.       }
  179.       break;
  180.  
  181.     case 3:
  182.       lcd.setCursor(0, 0);
  183.       lcd.print("Press Select       ");
  184.       lcd.setCursor(0, 1);
  185.       lcd.print("To Run Demo               ");
  186.       if (Up.wasPressed()) {
  187.         currentPage += 1;
  188.       }
  189.       if (Down.wasPressed()) {
  190.         currentPage -= 1;
  191.       }
  192.       break;
  193.  
  194.     case 4:
  195.       lcd.setCursor(0, 0);
  196.       lcd.print("Press Select          ");
  197.       lcd.setCursor(0, 1);
  198.       lcd.print("To Check Fan        ");
  199.       if (Up.wasPressed()) {
  200.         currentPage += 1;
  201.       }
  202.       if (Down.wasPressed()) {
  203.         currentPage -= 1;
  204.       }
  205.       break;
  206.  
  207.  
  208.     case 5:
  209.  
  210.       lcd.setCursor(0, 0);
  211.       lcd.print("Press Select         ");
  212.       lcd.setCursor(0, 1);
  213.       lcd.print("To Show Time         ");
  214.       if (Up.wasPressed()) {
  215.         currentPage = 0;
  216.       }
  217.       if (Down.wasPressed()) {
  218.         currentPage -= 1;
  219.       }
  220.       if (Select.wasPressed()) {
  221.         needToShowTime = true;
  222.         if ( needToShowTime ) {   //triggers a function which will print to lcd
  223.           previousMillis = millis();   //and wait for said time
  224.           while ((millis() - previousMillis) <= tDuration) //show for 5 seconds
  225.           {
  226.             showTime();
  227.             delay(50);       //to slow down arduino
  228.           }
  229.           needToShowTime = false;  //after we have shown the time for time we can go to normal
  230.         }
  231.       }
  232.       break;
  233.  
  234.   }
  235. }
  236.  
  237.  
  238.  
  239. void updateBuzzer() {
  240. }
  241.  
  242. void countDownForHatch() {
  243. }
  244.  
  245.  
  246. void showTime()
  247. {
  248.  
  249.   DateTime now = rtc.now();
  250.   lcd.setCursor(0, 0);
  251.   lcd.print("Date:  ");
  252.   lcd.setCursor(7, 0);
  253.   lcd.print(now.year(), DEC);
  254.   lcd.print('/');
  255.   lcd.print(now.month(), DEC);
  256.   lcd.print('/');
  257.   lcd.print(now.day(), DEC);
  258.   lcd.print("            ");
  259.   lcd.setCursor(0, 1);
  260.   lcd.print("Time:  ");
  261.   lcd.setCursor(7, 1);
  262.   lcd.print("");
  263.   lcd.print(now.hour(), DEC);
  264.   lcd.print(':');
  265.   lcd.print(now.minute(), DEC);
  266.   lcd.print(':');
  267.   lcd.print(now.second(), DEC);
  268.   lcd.print("          ");
  269.  
  270. }
  271.  
  272. void printTime() {
  273.   if ( needToShowTime ) {    //triggers a function which will print to lcd
  274.     previousMillis = millis();   //and wait for said time
  275.     while ((millis() - previousMillis) <= tDuration) //show for 5 seconds
  276.     {
  277.       showTime();
  278.       delay(50);       //to slow down arduino
  279.     }
  280.     needToShowTime = false;  //after we have shown the time for time we can go to normal
  281.   }
  282. }
Add Comment
Please, Sign In to add comment