manhoosbilli1

Complete incubator lcd menu code

Oct 17th, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 12.00 KB | None | 0 0
  1.  
  2. //libraries
  3.  
  4. #include <Bounce2.h>
  5. #include <Wire.h>
  6. #include <LiquidCrystal_I2C.h>
  7. #include "DS1307.h"
  8. #include "cactus_io_DHT22.h"
  9. DS1307 clock;//define a object of DS1307 class
  10.  
  11. LiquidCrystal_I2C lcd(0x27, 16, 2);
  12.  
  13. //definitions
  14.  
  15. #define up 7
  16. #define down 6
  17. #define select 5
  18. #define LED_PIN 13
  19. #define light 3   //heater pin
  20. #define buzzer A3  //buzzer pin
  21. #define dehumidifier_fan A4   //dehumidifier fan
  22. #define heater 8
  23. #define DHT22_PIN 9
  24. DHT22 dht(DHT22_PIN);
  25.  
  26. //counters                                  // i know i am using only a few of these but i plan to use most of them later
  27.  
  28. unsigned int counter = 0;
  29. unsigned long currentMillis;
  30. unsigned long previousMillis;
  31. unsigned long previousMillis0;
  32. unsigned long previousMillis1;
  33. unsigned long previousMillis2;
  34.  
  35. //values
  36.  
  37. const int temp_update_interval = 3000;
  38. const float max_temp = 35.5;              //fill with required numbers
  39. const int max_humidity = 50;
  40. const int min_temp = 34.5;
  41. const int min_humidity = 30;
  42.  
  43. //lcd
  44.  
  45. int menuSize = 8;
  46. bool mainMenu = true;
  47. int currentMenu =0;
  48. int subMenu = 0;
  49.  
  50. //Flags
  51.  
  52. bool high_humid = 0;
  53. bool high_temp = 0;        
  54. bool low_humid =0;
  55. bool low_tmep =0;
  56. bool sensor_ok= 1;
  57. bool RTC_ok =1;
  58. bool emergency = 0;
  59.  
  60. int hour_now, minute_now, second_now, month_now, year_now, day_now;
  61.  
  62. byte arrow[] = {
  63.   B00000,
  64.   B00100,
  65.   B00010,
  66.   B11111,
  67.   B00010,
  68.   B00100,
  69.   B00000,
  70.   B00000
  71. };
  72.  
  73. byte degree[] = {
  74.   B00000,
  75.   B01110,
  76.   B01010,
  77.   B01110,
  78.   B00000,
  79.   B00000,
  80.   B00000,
  81.   B00000
  82. };
  83.  
  84. byte celcius[] = {
  85.   0x00,
  86.   0x06,
  87.   0x09,
  88.   0x08,
  89.   0x08,
  90.   0x09,
  91.   0x06,
  92.   0x00
  93. };
  94.  
  95. byte on[] = {
  96.   0x00,
  97.   0x00,
  98.   0x00,
  99.   0x01,
  100.   0x12,
  101.   0x14,
  102.   0x18,
  103.   0x10
  104. };
  105.  
  106. byte off[] = {
  107.   0x00,
  108.   0x00,
  109.   0x00,
  110.   0x11,
  111.   0x0A,
  112.   0x04,
  113.   0x0A,
  114.   0x11
  115. };
  116.  
  117. Bounce debouncerup = Bounce(); // Instantiate a Bounce object
  118. Bounce debouncerdown = Bounce(); // Instantiate a Bounce object
  119. Bounce debouncerselect = Bounce();
  120.  
  121. void setup() {
  122.  
  123.   clock.begin();
  124.   clock.fillByYMD(2018,10,17);//Jan 19,2013
  125.   clock.fillByHMS(15,49,05);//15:28 30"
  126.   clock.fillDayOfWeek(WED);//Saturday
  127.   clock.setTime();//write time to the RTC chip
  128.   debouncerup.attach(up,INPUT_PULLUP); // Attach the debouncer to a pin with INPUT_PULLUP mode
  129.   debouncerdown.attach(down, INPUT_PULLUP);
  130.   debouncerselect.attach(select, INPUT_PULLUP);
  131.   pinMode(LED_BUILTIN,OUTPUT); // Setup the LED
  132.   pinMode(buzzer, OUTPUT);
  133.   pinMode(dehumidifier_fan, OUTPUT);
  134.   pinMode(light, OUTPUT);
  135.   pinMode(heater, OUTPUT);
  136.   debouncerup.interval(50);
  137.   debouncerdown.interval(50); // Use a debounce interval of 25 milliseconds
  138.   debouncerselect.interval(50);
  139.   dht.begin();
  140.   lcd.begin();
  141.   lcd.backlight();
  142.   lcd.setCursor(0,0);
  143.   lcd.print("  Welcome to the     ");
  144.   lcd.setCursor(0,1);
  145.   lcd.print("   Smart-bator     ");
  146.   lcd.createChar(0, arrow);                  
  147.   lcd.createChar(1, degree);                    /JUST ADDING SOME GRAPHICS
  148.   lcd.createChar(2, celcius);
  149.   lcd.createChar(3, on);
  150.   lcd.createChar(4, off);
  151.   lcd.home();
  152.   currentMenu = 1;
  153.   delay(3000);
  154.   lcd.clear();
  155.  
  156.  
  157. }
  158.  
  159. void loop() {
  160.   currentMillis = millis();
  161.   updateTime();
  162.   debouncerup.update();       // Update the Bounce instance
  163.   debouncerdown.update();
  164.   debouncerselect.update();
  165.   regulate_humidity();
  166.   regulate_temperature();
  167.   calculateMenu();           //decides to decrement or increment
  168.   checkMenu();               //shows different text on lcd according to menu system
  169.   menuSelection();           //CARRIES OUT RESPECTIVE ACTIONS BASED ON THE CURRENTMENU NUMBER
  170.  
  171.  
  172. }
  173.  
  174.  
  175.  
  176.  
  177. //--------------------------------FUNCTIONSSSSSSSS-------------------------------------------------
  178.  
  179.  
  180.  
  181.  
  182.  
  183. void checkMenu() {
  184.  
  185. switch(currentMenu) {  
  186. case 0:
  187. currentMenu = 1;
  188. break;
  189.  
  190. case 1:                 //shows temp and humidity default case
  191.  
  192. print_sensor_data();
  193.  
  194. break;                                  //remember to turn mainmenu variable to false when getting into sub menus and change it back as you go into main menu
  195.                                         //this will help me differentiate between main menu and sub menu variables and also the counters.
  196.  
  197. case 2:  //Motor settings  
  198. lcd.setCursor(0,0);
  199. lcd.write(0);
  200. lcd.print("Motor demo           ");
  201. lcd.setCursor(0,1);
  202. lcd.print(" Off backlight       ");
  203.  
  204. break;                            //to execute a process. makea an if statement saying if the menu matches that number and select button is pressed. start the process
  205.  
  206. case 3:   //Stop motor
  207. lcd.setCursor(0,0);
  208. lcd.print(" Motor demo         ");
  209. lcd.setCursor(0,1);
  210. lcd.write(0);
  211. lcd.print("Off backlight         ");       // turn off backlight
  212. break;
  213.  
  214. case 4:  //light setting
  215. lcd.setCursor(0,0);
  216. lcd.write(0);
  217. lcd.print("On backlight         ");
  218. lcd.setCursor(0,1);
  219. lcd.print(" Show time            ");
  220. //add a up or down character to show the user that you can press up and down to change menu
  221. break;
  222.  
  223. case 5:  //show time
  224. lcd.setCursor(0,0);
  225. lcd.print(" On backlight                 ");
  226. lcd.setCursor(0,1);
  227. lcd.write(0);   //selected
  228. lcd.print("Show time             ");   //this goes into sub menu. make another sub menu switch case when select is pressed it should change into that sub menu and
  229.               //when the down button is pressed for 5 sec it should take you back to home
  230. break;
  231.  
  232.  
  233. case 6:  //  toggle alarm
  234.   //digitalWritw(alarmPin, !digitalWrite(alarmPin
  235. break;
  236.  
  237. case 7:  // calculate hatch day
  238. lcd.setCursor(0,0);
  239. lcd.write(0);   //selected
  240. lcd.print("Calc hatch N/A           ");
  241. lcd.setCursor(0,1);
  242. lcd.print(" Show hatch N/A       ");
  243. break;
  244.  
  245. case 8:  //show hatch day
  246. lcd.setCursor(0,0);
  247.  
  248. lcd.print(" Calc hatch N/A        ");
  249. lcd.setCursor(0,1);
  250. lcd.write(0);   //selected
  251. lcd.print("Show hatch N/A       ");
  252. break;
  253.  
  254. case 9:
  255. currentMenu = 8;
  256. break;
  257.  
  258.  
  259. }
  260. }
  261.  
  262. //--------------------Setting up led rate function--------------------
  263.  
  264. void ledRate() {
  265.  if ( debouncerup.fell() ) {  // Call code if button transitions from HIGH to LOW
  266.  
  267.   lcd.clear();
  268.   counter= counter + 50;
  269.   lcd.setCursor(0,0);
  270.   lcd.print("Counter:");
  271.   lcd.setCursor(9,0);
  272.   lcd.print(counter);
  273.   return;
  274.  }
  275.  
  276.  if ( debouncerdown.fell() ) {  // Call code if button transitions from HIGH to LOW
  277.   lcd.clear();
  278.   counter = counter-50;
  279.   lcd.setCursor(0,0);
  280.   lcd.print("Counter:");
  281.   lcd.setCursor(9,0);
  282.   lcd.print(counter);
  283.   return;
  284.  }
  285.  
  286. }
  287.  
  288. //-------------------Blink led function-------------------
  289.  
  290.  
  291. void blinkLed() {
  292.  if(currentMillis - previousMillis >= 200) {
  293.   digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
  294.   previousMillis = currentMillis;
  295.  }
  296. }
  297.  
  298.  
  299. //--------------------Update Menu function--------------------
  300.  
  301. void calculateMenu() {  
  302.  if(mainMenu == true) {
  303.  if(debouncerup.fell()){
  304.   if(currentMenu < menuSize) {
  305.   currentMenu = currentMenu + 1;
  306.    }
  307.  }
  308.   if(debouncerdown.fell()) {
  309.     if(currentMenu > 0) {
  310.       currentMenu = currentMenu - 1;
  311.     }
  312.   }    
  313. }
  314. }
  315.  
  316. //--------------------Action functions--------------------
  317.  
  318. void action1()    //show temp and humidity
  319. {        
  320.  
  321.   // updateTemp();
  322.  
  323. }
  324.  
  325.  
  326.  
  327. void action2()
  328.  {              //start the motor and do the motion as demo.
  329.  
  330.  //  motorDemo();  
  331.  }
  332.  
  333.  
  334.  
  335.  
  336.  
  337.  
  338. void action3()     //turn on lcd backlight
  339. {
  340.  
  341.   lcd.noBacklight(); ;
  342.  
  343. }
  344.  
  345.  
  346.  
  347.  
  348. void action4()    //turn off lcd backlight
  349. {  
  350.  
  351.   lcd.backlight();
  352. }
  353.  
  354.  
  355. void action5()      //show time for 10 seconds
  356. {
  357. lcd.clear();
  358. bool inaction5 = true;
  359. while(inaction5) {
  360. for ( int i; i< 10; i++)
  361. {
  362.     updateTime();
  363.     printTime();
  364.     delay(1000);
  365.  
  366. }
  367. inaction5 = false;
  368. }
  369. }
  370.  
  371. void action6()    //toggle alarm
  372. {
  373.   digitalWrite(buzzer, !digitalRead(buzzer));
  374.   if(digitalRead(buzzer) == HIGH) {
  375.     lcd.clear();
  376.     lcd.setCursor(3, 0);
  377.     lcd.print("Buzzer Is Now       ");
  378.     lcd.setCursor(7,1);
  379.     lcd.print("Off         ");
  380.     delay(3000);
  381.   }
  382.   if(digitalRead(buzzer) == LOW) {
  383.     lcd.clear();
  384.     lcd.setCursor(3, 0);
  385.     lcd.print("Buzzer Is Now       ");
  386.     lcd.setCursor(7,1);
  387.     lcd.print("On         ");
  388.     delay(3000);
  389.   }
  390.  
  391. }
  392.  
  393. void action7()      //calculate hatch date
  394. {
  395.  
  396. }
  397.  
  398. void action8()     //show hatch date
  399.  
  400. {
  401.    //read from eeprom
  402. }
  403.  
  404. void action9 ()     //toggle light
  405. {
  406.   digitalWrite(light, !digitalRead(light));
  407. }
  408.  
  409.  
  410. //-----------------------------------------
  411.  
  412.  
  413. void menuSelection() {                        //based on the current menu we are on it will carry out respective action
  414. if(debouncerselect.fell())
  415. switch(currentMenu) {
  416.   case 1:
  417.   action1();
  418.   break;
  419.  
  420.   case 2:  
  421.   action2();
  422.  
  423.   break;
  424.  
  425.   case 3:
  426.   action3();   //turn off backlight
  427.   break;
  428.  
  429.   case 4:
  430.   action4();   //turn on back light
  431.   break;
  432.  
  433.   case 5:
  434.   action5();    //show time
  435.   break;
  436.  
  437.   case 6:       //calc hatch date
  438.   action6();
  439.   break;
  440.  
  441.   case 7:      //show hatch date
  442.   action7();
  443.   break;
  444.  
  445.   case 8:
  446.   action8();
  447.   break;
  448.  
  449.   case 9:
  450.   action9();    //toggle light
  451.   break;
  452.  
  453.  
  454. }
  455. }
  456.  
  457. //----------------------------
  458.  
  459. void calcSubMenu()
  460. {  
  461.  if(mainMenu == false) {
  462.  if(debouncerup.fell()){
  463.   if(currentMenu < 8 ) {  //subMenuSize
  464.   subMenu = subMenu + 1;
  465.    }
  466.  }
  467.   if(debouncerdown.fell()) {
  468.     if(subMenu > 0) {
  469.       subMenu = subMenu - 1;
  470.     }
  471.   }    
  472. }
  473. }
  474.  
  475.  
  476. void goHome()
  477. {
  478.   //nest this function in every action
  479.   if(debouncerselect.fell()) {
  480.     //if the button is pressed for more than 2s then jump back to home/main meu
  481.   }
  482. }
  483.  
  484. //-----------------------------------
  485.  
  486. void updateTime()
  487. {
  488.   clock.getTime();
  489.   hour_now = (clock.hour);
  490.   minute_now =(clock.minute);
  491.   second_now =(clock.second);
  492.   month_now =(clock.month);
  493.   year_now =(clock.year+2000);
  494.   day_now =(clock.dayOfMonth);
  495.  
  496. }
  497.  
  498. void printTime()
  499. {
  500.  
  501.   lcd.setCursor(0,0);
  502.   lcd.print("Time: ");
  503.   lcd.print(hour_now);
  504.   lcd.print(":");
  505.   lcd.print(minute_now);
  506.   lcd.print(":");
  507.   lcd.print(second_now);
  508.   lcd.print("        ");
  509.   lcd.setCursor(0,1);
  510.   lcd.print("Date: ");
  511.   lcd.print(day_now);
  512.   lcd.print("/");
  513.   lcd.print(month_now);
  514.   lcd.print("/");
  515.   lcd.print(year_now);
  516.   lcd.print("         ");
  517.  
  518. }
  519.  
  520. void print_sensor_data()
  521. {
  522.   if(currentMillis - previousMillis >= temp_update_interval) {
  523.    if (isnan(dht.humidity) || isnan(dht.temperature_C)) {
  524.  
  525.   lcd.clear();
  526.   lcd.setCursor(0,0);
  527.   lcd.print("Sensor failure!");
  528.   digitalWrite(buzzer, HIGH);
  529.   return;
  530.   }
  531.  
  532.    dht.readHumidity();
  533.    dht.readTemperature();
  534.    lcd.clear();
  535.    lcd.setCursor(0,0);
  536.    lcd.print("T: ");
  537.    lcd.print(dht.temperature_C);
  538.    lcd.write(1);  //degree
  539.    lcd.write(2);  //celcius
  540.    if(digitalRead(buzzer) == HIGH)   //buzzer status
  541.    {
  542.    lcd.setCursor(13,0);
  543.    lcd.write(3);
  544.    } else if (digitalRead(buzzer) == LOW)
  545.    {
  546.     lcd.setCursor(13,0);
  547.     lcd.write(4);
  548.    }
  549.    if(digitalRead(light) == HIGH) //if the light is off
  550.    {
  551.     lcd.setCursor(14, 0);
  552.     lcd.write(4);
  553.    
  554.    } else if (digitalRead(light) == LOW) //light on
  555.    {
  556.     lcd.setCursor(14,0);
  557.     lcd.write(3);
  558.    }
  559.     if(digitalRead(dehumidifier_fan) == HIGH) //if the light is on
  560.    {
  561.     lcd.setCursor(15, 0);
  562.     lcd.write(3);
  563.    
  564.    } else if (digitalRead(dehumidifier_fan) == LOW) //light off
  565.    {
  566.     lcd.setCursor(15,0);
  567.     lcd.write(4);
  568.    }
  569.  
  570.  
  571.    
  572. //   lcd.setCursor(15,0);    temp and humid graphic
  573. //   lcd.write(1);
  574.  
  575.    lcd.setCursor(0,1);
  576.    lcd.print("H: ");
  577.    lcd.print(dht.humidity);
  578.    lcd.print(" %");
  579.    lcd.print("        ");
  580.    previousMillis = currentMillis;
  581. }
  582. }
  583.  
  584. //----------------------------------------
  585.  
  586. void regulate_humidity()
  587. {
  588.   if(dht.humidity >= max_humidity)
  589.   {
  590.      digitalWrite(dehumidifier_fan, HIGH);
  591.      //blink_led();
  592.   }
  593.   else
  594.   {
  595.     digitalWrite(dehumidifier_fan, LOW);
  596.   }
  597. }
  598.  
  599.  
  600. void regulate_temperature()
  601. {
  602.   if(dht.temperature_C >= max_temp)
  603.   {
  604.     digitalWrite(heater, HIGH);    //inverse for relay high = low
  605.   }
  606.   if(dht.temperature_C <= min_temp)
  607.   {
  608.     digitalWrite(heater, LOW);     //turn on.
  609.   }
  610. }
Add Comment
Please, Sign In to add comment