Advertisement
Guest User

Harys - kurnik

a guest
Oct 25th, 2016
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 15.50 KB | None | 0 0
  1. #include <Wire.h>
  2. #include <DHT.h>
  3. #include <Time.h>
  4. #include <BH1750.h>
  5. #include <DS3232RTC.h>
  6. #include <EEPROM.h>
  7. #include <LiquidCrystal_I2C.h>
  8.  
  9. #define MOTOR_OPEN 9   //int1
  10. #define MOTOR_CLOSE 10 //int2
  11. #define BUTTON_OPEN 8  
  12. #define BUTTON_CLOSE 7
  13. #define SENSOR_1 12
  14. #define SENSOR_2 13
  15. #define PIN_DHT 5
  16. #define LED_G 4
  17. #define LED_R 3
  18. #define BEEP 6 // druhy pin do minus
  19. // SCL - pin 5 , SDA - pin 4
  20.  
  21. BH1750 lightMeter;
  22. DHT mojeDHT(PIN_DHT, DHT11);
  23. LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
  24. tmElements_t tm, tm_hour_start, tm_hour_end;
  25. time_t now_, t_hour_start, t_hour_end;
  26. unsigned long previousMillis = 0;
  27. int counterLuxOtvirani = 0;
  28. int counterLuxZavirani = 0;
  29. int lux_otevreni; // nacita se z pameti.. ADDR 10
  30. int lux_zavreni;  // nacita se z pameti.. ADDR 20
  31. String ulozenyCas = "";
  32. bool muzeSeOtvirat = false;
  33. bool muzeSeZavirat = false;
  34. bool otevrene = false; //  
  35.  
  36. // Button variables
  37. int debounce = 10;
  38. int holdTime = 2000;
  39. boolean buttonVal1 = HIGH;      boolean buttonVal2 = HIGH;
  40. boolean buttonLast1 = HIGH;     boolean buttonLast2 = HIGH;
  41. long downTime1 = -1;            long downTime2 = -1;
  42. long upTime1 = -1;              long upTime2 = -1;
  43. boolean ignoreUp1 = false;      boolean ignoreUp2 = false;
  44. boolean waitForUp1 = false;     boolean waitForUp2 = false;
  45. boolean holdEventPast1 = false; boolean holdEventPast2 = false;
  46.  
  47. // time variables.. /////////////////////////
  48. int otvirani_OD[] = {5,0};  // od 5:00
  49. int otvirani_DO[] = {7,30}; // do 7:30
  50. int zavirani_OD[] = {17,0}; // od 17:00
  51. int zavirani_DO[] = {22,0}; // do 22:00
  52. /////////////////////////////////////////////
  53.  
  54. // declare functions  
  55. void beeping(int count);
  56. void opening(long cas);
  57. void closing(long cas);
  58. void showLuxTempHumid();
  59. int checkOpenButton();  // 1-press, 2-hold
  60. int checkCloseButton(); // 1-press, 2-hold
  61. void printDigits(int digits);
  62. void printTimeNow(); //hh:mm
  63. bool isOpenSensor1(); //sensor1
  64. bool isOpenSensor2(); //sensor2
  65. void firstStartCheckDoor();
  66. bool watchClosingTime();
  67. bool watchOpeningTime();
  68. void setOpeningLux();
  69. void setClosingLux();
  70. void LEDcontrol();
  71. bool isTimeBetween(int startHour, int startMinute,
  72.                    int endHour, int endMinute);
  73.  
  74.  
  75. void setup() {  
  76.   pinMode(MOTOR_OPEN, OUTPUT);
  77.   pinMode(MOTOR_CLOSE, OUTPUT);  
  78.   digitalWrite(MOTOR_OPEN,HIGH);  //
  79.   digitalWrite(MOTOR_CLOSE,HIGH); //
  80.   pinMode(BUTTON_OPEN, INPUT);
  81.   pinMode(BUTTON_CLOSE, INPUT);    
  82.   pinMode(SENSOR_1, INPUT);
  83.   pinMode(SENSOR_2, INPUT);
  84.   pinMode(BEEP, OUTPUT);
  85.   pinMode(LED_G, OUTPUT);
  86.   pinMode(LED_R, OUTPUT);
  87.  
  88.   lightMeter.begin();
  89.   mojeDHT.begin();  
  90.   lcd.begin(16,2);
  91.   lcd.backlight();
  92.  
  93.   // nactu hodnoty Luxu z pameti..
  94.   lux_otevreni = EEPROM.read(10);
  95.   lux_zavreni = EEPROM.read(20);
  96.  
  97.   firstStartCheckDoor();
  98.   LEDcontrol();
  99.  
  100.   //prvni ulozeni Lux do EEPROM, defaultne 5 Lx pro oba
  101.     //EEPROM.write(10, 5); EEPROM.write(20, 5);  
  102.   //set the system time to 23h31m30s on 13Feb2009
  103.     //setTime(12, 34, 0, 6, 9, 2016); RTC.set(now());
  104. }
  105.  
  106. void loop() {                                       // co 1min..
  107.   if ((unsigned long)(millis() - previousMillis) >= 1000UL*60* 1){
  108.     if (watchOpeningTime() && !otevrene) opening();
  109.     if (watchClosingTime() && otevrene) closing();  
  110.     previousMillis = millis(); }
  111.  
  112.   switch(checkOpenButton()){
  113.     case 1: if(!otevrene) opening(); break; // press
  114.     case 2: setOpeningLux(); break;         // hold
  115.     default: break; }
  116.    
  117.   switch(checkCloseButton()){
  118.     case 1: if(otevrene) closing(); break; // press
  119.     case 2: setClosingLux(); break;        // hold
  120.     default: break; }
  121.    
  122.   showLuxTempHumid(); LEDcontrol();
  123. }
  124.  
  125. void opening(){ // OPENING
  126.   float cas = 0.0;
  127.   lcd.clear();    
  128.   lcd.setCursor(0,0);
  129.   lcd.print("OTVIRANI - ");  
  130.   lcd.print(cas,1); lcd.print("s");
  131.   beeping(3);  
  132.   digitalWrite(MOTOR_OPEN,LOW); // motor start  
  133.   do{
  134.      if(millis()%100==0)
  135.      {
  136.       cas+=0.1;  
  137.       lcd.setCursor(11,0);
  138.       lcd.print(cas,1);
  139.       lcd.print("s  ");
  140.       LEDcontrol();
  141.      }
  142.     }
  143.   while ( (!isOpenSensor1()) || (!isOpenSensor2()) );    
  144.   digitalWrite(MOTOR_OPEN,HIGH); // motor stop  
  145.   otevrene = true; //
  146.   LEDcontrol();
  147.   beeping(1);
  148.   delay(800);
  149.   lcd.clear();
  150.   lcd.setCursor(0,0);
  151.   lcd.print("OTEVRENE - ");
  152.   printTimeNow();
  153. }
  154.  
  155. void closing(){ // CLOSING
  156.   float cas = 0.0;
  157.   lcd.clear();    
  158.   lcd.setCursor(0,0);
  159.   lcd.print("ZAVIRANI - ");  
  160.   lcd.print(cas/1000.0,1);
  161.   lcd.print("s");  
  162.   beeping(3);    
  163.   digitalWrite(MOTOR_CLOSE,LOW); // motor start  
  164.   do{
  165.      if(millis()%100==0)
  166.      {
  167.       cas+=0.1;  
  168.       lcd.setCursor(11,0);
  169.       lcd.print(cas,1);
  170.       lcd.print("s  ");
  171.       LEDcontrol();
  172.      }
  173.     }
  174.   while ( (isOpenSensor1()) || (isOpenSensor2()) );    
  175.   digitalWrite(MOTOR_CLOSE,HIGH); // motor stop  
  176.   otevrene = false; //
  177.   LEDcontrol();
  178.   beeping(1);
  179.   delay(800);
  180.   lcd.clear();
  181.   lcd.setCursor(0,0);
  182.   lcd.print("ZAVRENE - ");
  183.   printTimeNow();
  184. }
  185.  
  186. bool watchOpeningTime(){
  187.   if (muzeSeOtvirat)
  188.   { // je otviraci cas ?
  189.     if (isTimeBetween(otvirani_OD[0], otvirani_OD[1], otvirani_DO[0], otvirani_DO[1]) )    
  190.     {
  191.       int lux = lightMeter.readLightLevel();
  192.       if (lux >= lux_otevreni) counterLuxOtvirani++;
  193.       else { counterLuxOtvirani = 0; return false; }
  194.  
  195.       if (counterLuxOtvirani == 3) // po 3. po sobe jdoucich minutach - otvira se
  196.       {
  197.         counterLuxOtvirani = 0;
  198.         muzeSeOtvirat = false;
  199.         muzeSeZavirat = true;
  200.         return true;
  201.       }
  202.       else return false;
  203.     }
  204.     // pokud neni otevrene 2min po casu otvirani - automaticky otevrit
  205.     else if (isTimeBetween(otvirani_DO[0], otvirani_DO[1]+1, otvirani_DO[0], otvirani_DO[1]+2) )
  206.     {
  207.       counterLuxOtvirani = 0;
  208.       muzeSeOtvirat = false;
  209.       muzeSeZavirat = true;
  210.       return true;
  211.     }
  212.     else return false;
  213.   }
  214. }
  215.  
  216. bool watchClosingTime(){
  217.   if (muzeSeZavirat)
  218.   { // je zaviraci cas ?
  219.     if (isTimeBetween(zavirani_OD[0], zavirani_OD[1], zavirani_DO[0], zavirani_DO[1]) )
  220.     {
  221.       int lux = lightMeter.readLightLevel();
  222.       if (lux >= lux_zavreni) counterLuxZavirani++;
  223.       else { counterLuxZavirani = 0; return false; }
  224.  
  225.       if (counterLuxZavirani == 3) // po 3. po sobe jdoucich minutach - zavira se
  226.       {
  227.         counterLuxZavirani = 0;
  228.         muzeSeOtvirat = true;
  229.         muzeSeZavirat = false;
  230.         return true;
  231.       }  
  232.       else return false;      
  233.     }  
  234.     // pokud neni otevrene 2min po casu zavirani - automaticky zavrit
  235.     else if (isTimeBetween(zavirani_DO[0], zavirani_DO[1]+1, zavirani_DO[0], zavirani_DO[1]+2) )
  236.     {
  237.       counterLuxZavirani = 0;
  238.       muzeSeOtvirat = true;
  239.       muzeSeZavirat = false;
  240.       return true;
  241.     }
  242.     else return false;  
  243.   }
  244. }
  245.  
  246. void firstStartCheckDoor(){
  247.   // ze zacatku ZAVRENE
  248.   if( (!isOpenSensor1()) && (!isOpenSensor2()) )
  249.   {
  250.     otevrene = false; //  
  251.     muzeSeOtvirat = true;
  252.     muzeSeZavirat = false;
  253.     lcd.clear();  lcd.setCursor(0,0);  
  254.     lcd.print("ZAVRENE - "); printTimeNow();
  255.     beeping(1);
  256.   }
  257.   // ze zacatku OTEVRENE
  258.   else
  259.   {
  260.     if( (isOpenSensor1()) && (isOpenSensor2()) )
  261.     {
  262.       otevrene = true; //      
  263.       muzeSeOtvirat = false;
  264.       muzeSeZavirat = true;
  265.       lcd.clear();  lcd.setCursor(0,0);  
  266.       lcd.print("OTEVRENE - "); printTimeNow();
  267.       beeping(1);
  268.     }
  269.     // je POOTEVRENE (neni zavrene ani otevrene)
  270.     else
  271.     {            
  272.       if(isTimeBetween(otvirani_OD[0], otvirani_OD[1], zavirani_DO[0], zavirani_DO[1]) )    
  273.       { // je den - otevri
  274.         otevrene = true; //      
  275.         muzeSeOtvirat = false;
  276.         muzeSeZavirat = true;
  277.         opening();  
  278.       }
  279.       else
  280.       { // je noc - zavri
  281.         otevrene = false; //  
  282.         muzeSeOtvirat = true;
  283.         muzeSeZavirat = false;
  284.         closing();
  285.       }
  286.     }
  287.   }  
  288. }
  289.  
  290. int checkOpenButton() {  
  291.   int event = 0;
  292.   // Read the state of the button
  293.   buttonVal1 = !digitalRead(BUTTON_OPEN);  
  294.   // Button pressed down
  295.   if (buttonVal1 == LOW && buttonLast1 == HIGH && (millis() - upTime1) > debounce)
  296.   {
  297.     downTime1 = millis();
  298.     ignoreUp1 = false;
  299.     waitForUp1 = false;
  300.     holdEventPast1 = false;
  301.   }  
  302.  
  303.   // Button released
  304.   else if (buttonVal1 == HIGH && buttonLast1 == LOW && (millis() - downTime1) > debounce && (not ignoreUp1))
  305.   {
  306.     event = 1; // press
  307.     upTime1 = millis();
  308.   }
  309.      
  310.   // Test for hold
  311.   if (buttonVal1 == LOW && (millis() - downTime1) >= holdTime && (not holdEventPast1))
  312.   {
  313.     event = 2; // hold
  314.     waitForUp1 = true;
  315.     ignoreUp1 = true;
  316.     holdEventPast1 = true;
  317.   }
  318.   buttonLast1 = buttonVal1;
  319.   return event;  
  320. }
  321.  
  322. int checkCloseButton() {  
  323.   int event = 0;
  324.   // Read the state of the button
  325.   buttonVal2 = !digitalRead(BUTTON_CLOSE);  
  326.   // Button pressed down
  327.   if (buttonVal2 == LOW && buttonLast2 == HIGH && (millis() - upTime2) > debounce)
  328.   {
  329.     downTime2 = millis();
  330.     ignoreUp2 = false;
  331.     waitForUp2 = false;
  332.     holdEventPast2 = false;
  333.   }  
  334.  
  335.   // Button released
  336.   else if (buttonVal2 == HIGH && buttonLast2 == LOW && (millis() - downTime2) > debounce && (not ignoreUp2))
  337.   {
  338.     event = 1; // press
  339.     upTime2 = millis();
  340.   }
  341.      
  342.   // Test for hold
  343.   if (buttonVal2 == LOW && (millis() - downTime2) >= holdTime && (not holdEventPast2))
  344.   {
  345.     event = 2; // hold
  346.     waitForUp2 = true;
  347.     ignoreUp2 = true;
  348.     holdEventPast2 = true;
  349.   }
  350.   buttonLast2 = buttonVal2;
  351.   return event;  
  352. }
  353.  
  354. void setOpeningLux(){  
  355.   long timer = millis();
  356.   int nove_lux_otevreni = lux_otevreni;
  357.   lcd.clear();
  358.   lcd.setCursor(0,0);
  359.   lcd.print("Nastav otevreni!");
  360.   lcd.setCursor(0,1);
  361.   lcd.print("Hodnota: ");    
  362.   lcd.print(nove_lux_otevreni);
  363.   lcd.print("Lx");
  364.   lcd.setCursor(15,1); lcd.blink();
  365.    
  366.   while (millis() - timer < 6000) // po 6s bez zmacknuti se ulozi
  367.   {    
  368.     if (checkOpenButton() == 1) // minus
  369.     {  
  370.       if (nove_lux_otevreni > 0)
  371.         nove_lux_otevreni--;
  372.       lcd.setCursor(9,1);
  373.       lcd.print(nove_lux_otevreni);
  374.       lcd.print("Lx     ");
  375.       lcd.setCursor(15,1);  
  376.       timer = millis();
  377.       delay(50);
  378.     }
  379.     if (checkCloseButton() == 1) // plus
  380.     {    
  381.       nove_lux_otevreni++;
  382.       lcd.setCursor(9,1);
  383.       lcd.print(nove_lux_otevreni);
  384.       lcd.print("Lx     ");
  385.       lcd.setCursor(15,1);  
  386.       timer = millis();
  387.       delay(50);
  388.     }  
  389.   }  
  390.  
  391.   lux_otevreni = nove_lux_otevreni;    //
  392.   EEPROM.write(10, nove_lux_otevreni); //
  393.   lcd.noBlink();
  394.   lcd.clear(); lcd.setCursor(0,0);
  395.   lcd.print("Hodnota otevreni");
  396.   lcd.setCursor(0,1);
  397.   lcd.print(nove_lux_otevreni);
  398.   lcd.print("Lx ulozena!");
  399.   delay(2500);
  400.   lcd.clear(); lcd.setCursor(0,0);
  401.   if (otevrene) lcd.print("OTEVRENE - ");
  402.   else lcd.print("ZAVRENE - ");
  403.   lcd.print(ulozenyCas);  
  404. }
  405.  
  406. void setClosingLux(){
  407.   long timer = millis();
  408.   int nove_lux_zavreni = lux_zavreni;
  409.   lcd.clear();
  410.   lcd.setCursor(0,0);
  411.   lcd.print("Nastav zavreni!");
  412.   lcd.setCursor(0,1);
  413.   lcd.print("Hodnota: ");    
  414.   lcd.print(nove_lux_zavreni);
  415.   lcd.print("Lx");
  416.   lcd.setCursor(15,1); lcd.blink();
  417.    
  418.   while (millis() - timer < 6000) // po 6s bez zmacknuti se ulozi
  419.   {    
  420.     if (checkOpenButton() == 1) // minus
  421.     {  
  422.       if (nove_lux_zavreni > 0)
  423.         nove_lux_zavreni--;
  424.       lcd.setCursor(9,1);
  425.       lcd.print(nove_lux_zavreni);
  426.       lcd.print("Lx     ");
  427.       lcd.setCursor(15,1);  
  428.       timer = millis();
  429.       delay(50);
  430.     }
  431.     if (checkCloseButton() == 1) // plus
  432.     {    
  433.       nove_lux_zavreni++;
  434.       lcd.setCursor(9,1);
  435.       lcd.print(nove_lux_zavreni);
  436.       lcd.print("Lx     ");
  437.       lcd.setCursor(15,1);  
  438.       timer = millis();
  439.       delay(50);
  440.     }  
  441.   }  
  442.  
  443.   lux_zavreni = nove_lux_zavreni;     //
  444.   EEPROM.write(20, nove_lux_zavreni); //
  445.   lcd.noBlink();
  446.   lcd.clear(); lcd.setCursor(0,0);
  447.   lcd.print("Hodnota zavreni");
  448.   lcd.setCursor(0,1);
  449.   lcd.print(nove_lux_zavreni);
  450.   lcd.print("Lx ulozena!");
  451.   delay(2500);
  452.   lcd.clear(); lcd.setCursor(0,0);
  453.   if (otevrene) lcd.print("OTEVRENE - ");
  454.   else lcd.print("ZAVRENE - ");
  455.   lcd.print(ulozenyCas);
  456. }
  457.  
  458. void beeping(int count) {
  459.   int pipnuti = 200; int mezera = 50;
  460.   for(int i=0; i<count; i++)
  461.   {
  462.     analogWrite(BEEP, 128);
  463.     delay(pipnuti);
  464.     analogWrite(BEEP, 0);
  465.     delay(mezera);
  466.   }
  467. }
  468.  
  469. void showLuxTempHumid(){
  470.   float tep = mojeDHT.readTemperature();
  471.   float vlh = mojeDHT.readHumidity();
  472.   int lux = lightMeter.readLightLevel();
  473.  
  474.   //lux = (lux/10)*10; // zaokrohleni na 10tky
  475.  
  476.   if (isnan(tep) || isnan(vlh))
  477.   { // pokud senzor vraci blbosti tak nuly
  478.     tep = 0;  vlh = 0;
  479.   }  
  480.   else
  481.   { // zobraz hodnoty..
  482.     lcd.setCursor(0,1);
  483.     lcd.print(lux);    
  484.     lcd.print("Lx");
  485.     if(lux<10) { lcd.setCursor(3,1); lcd.print("    ");}  
  486.     if(lux>=10 && lux<100) { lcd.setCursor(4,1); lcd.print("   ");}
  487.     if(lux>=100 && lux<1000) { lcd.setCursor(5,1); lcd.print("  ");}
  488.     if(lux>=1000) { lcd.setCursor(6,1); lcd.print(" ");}
  489.     lcd.setCursor(7, 1);
  490.     lcd.print(tep,0);    
  491.     lcd.print((char)223); lcd.print("C");
  492.     lcd.setCursor(13, 1);
  493.     lcd.print(vlh,0);
  494.     lcd.print("%");
  495.   }
  496. }
  497.  
  498. void printDigits(int digits){
  499.   lcd.print(':');
  500.   ulozenyCas += ":";
  501.   if(digits < 10)
  502.   {
  503.     lcd.print('0');
  504.     ulozenyCas += "0";
  505.   }
  506.   lcd.print(digits);
  507.   ulozenyCas += digits;
  508. }
  509.  
  510. void printTimeNow(){
  511.   time_t myTime;
  512.   myTime = RTC.get();
  513.   lcd.print(hour(myTime));
  514.   ulozenyCas = hour(myTime);
  515.   printDigits(minute(myTime));  
  516.   //printDigits(second(myTime));
  517. }
  518.  
  519. bool isOpenSensor1(){
  520.   return digitalRead(SENSOR_1);
  521. }
  522.  
  523. bool isOpenSensor2(){
  524.   return digitalRead(SENSOR_2);
  525. }
  526.  
  527. bool isTimeBetween(int startHour, int startMinute, int endHour, int endMinute){
  528.   //get current timestamp
  529.   time_t now_ = RTC.get();
  530.   // make current date and time structure
  531.   breakTime(now_, tm);
  532.   // make auxiliary structures to be more human editable and friendly
  533.   memcpy(&tm_hour_start, &tm, sizeof(tm));
  534.   memcpy(&tm_hour_end, &tm, sizeof(tm));
  535.   // change auxiliary structures to meet your start and end schedule
  536.   tm_hour_start.Hour = startHour;
  537.   tm_hour_start.Minute = startMinute;
  538.   tm_hour_start.Second = 0;
  539.   tm_hour_end.Hour = endHour;
  540.   tm_hour_end.Minute = endMinute;
  541.   tm_hour_end.Second = 0;
  542.   // reverse process to get timestamps
  543.   t_hour_start = makeTime(tm_hour_start);
  544.   t_hour_end = makeTime(tm_hour_end);
  545.   // check if end time is past midnight and correct if needed
  546.   if (startHour > endHour) //past midnight correction
  547.     t_hour_end = t_hour_end + SECS_PER_DAY;
  548.    
  549.   //final part  
  550.   if ((t_hour_start <= now_) && (now_ <= t_hour_end))
  551.     return true;
  552.   else
  553.     return false;  
  554. }
  555.  
  556. void LEDcontrol(){
  557.    if ((isOpenSensor1() && !isOpenSensor2()) || (!isOpenSensor1() && isOpenSensor2()) )
  558.    {
  559.      digitalWrite(LED_G,HIGH);
  560.      digitalWrite(LED_R,HIGH);
  561.    }
  562.    else
  563.    {
  564.      if (isOpenSensor1() && isOpenSensor2())
  565.        digitalWrite(LED_G,HIGH);
  566.      else digitalWrite(LED_G,LOW);  
  567.      if (!isOpenSensor1() && !isOpenSensor2())
  568.        digitalWrite(LED_R,HIGH);
  569.      else digitalWrite(LED_R,LOW);
  570.    }
  571. }
  572.  
  573.  
  574. //  lcd.print("STISK. TLACITKO ");
  575. //  while ( (isOpenSensor()) ||
  576. //        ( (!buttonOpenPressed()) && (!buttonClosePressed()) ));
  577.  
  578. /*
  579.   if (digitalRead(BUTTON_CLOSE)) {
  580.     delay(20);
  581.     if (digitalRead(BUTTON_CLOSE)) {
  582.       return true;   }
  583.   } return false;  
  584.   */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement