SHARE
TWEET

Untitled

a guest Apr 18th, 2019 88 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <Wire.h>
  2. #include <LiquidCrystal_I2C.h>
  3. #include <virtuabotixRTC.h>
  4.  
  5. #define BUTTON_PIN A1
  6. #define delayBetweenMessages 1000 // millis
  7. #define PONETIOMETER_PIN A0
  8.  
  9. #define _3_HOURS 3600000 // millis
  10. #define DIVIER_FROM_ANALOG_TO_PERCENT 10.23
  11. #define WINTER_DECREASE_COEFF 1.5
  12.  
  13. #define FEBRUARY 1
  14. #define JANUARY 0
  15. #define DECEMBER 11
  16.  
  17.  
  18. virtuabotixRTC RTC(/*CLK*/ 8,
  19. /*DAT*/ 9,
  20. /*RESET*/ 10);
  21.  
  22. LiquidCrystal_I2C lcd(0x27,
  23. /*columns*/ 16,
  24. /*rows*/ 2);
  25.  
  26. bool ItsMorningNow(){
  27.   return 5 <= RTC.hours && RTC.hours <= 13;
  28. };
  29.  
  30. bool ItsWinterNow(){
  31.   return RTC.month == FEBRUARY || RTC.month == JANUARY || RTC.month == DECEMBER;
  32. };
  33.  
  34. class SoilMoistureSensor{
  35. private:
  36.   byte port;
  37. public:
  38.   SoilMoistureSensor(const byte port):port(port){};
  39.  
  40.   byte getMoisture()const { return analogRead(port) / DIVIER_FROM_ANALOG_TO_PERCENT;}
  41. };
  42.  
  43. class Pump{
  44. private:
  45.   byte pin;
  46. public:
  47.   Pump(const byte pin):pin(pin){pinMode(pin,OUTPUT);};
  48.  
  49.   void pumpLiquid(const word delayMillis){
  50.     digitalWrite(pin,HIGH);
  51.     delay(delayMillis);
  52.     digitalWrite(pin,LOW);
  53.   };
  54. };
  55.  
  56. class Pot{
  57. private:
  58.   SoilMoistureSensor itsSoilMoistureSensor;
  59.   Pump itsPump;
  60.  
  61.   byte currMoisture;
  62.   byte requiredMoisture;
  63.  
  64.   static constexpr float deviationFromRequredMoistureCoeff = 0.2;
  65.  
  66.   byte calcMinSatisfactoryMoisture()const {
  67.     float currDeviationFromRequredMoistureCoeff = deviationFromRequredMoistureCoeff;
  68.     RTC.updateTime();
  69.     if(ItsWinterNow()){
  70.        currDeviationFromRequredMoistureCoeff = deviationFromRequredMoistureCoeff * WINTER_DECREASE_COEFF;
  71.     }
  72.     return requiredMoisture*(1 - currDeviationFromRequredMoistureCoeff);};
  73.  
  74. public:
  75.   Pot(const byte requiredMoisture,
  76.   const byte soilMoistureSensorPin,
  77.   const byte pumpPin)
  78.   : requiredMoisture(requiredMoisture),
  79.   itsSoilMoistureSensor(soilMoistureSensorPin),
  80.   itsPump(pumpPin){};
  81.  
  82.   void setRequiredMoisture(const byte moisture){requiredMoisture = moisture;};
  83.  
  84.   bool needsWatering()const{ return currMoisture < calcMinSatisfactoryMoisture();};
  85.  
  86.   byte determineMoisture(){
  87.     currMoisture = itsSoilMoistureSensor.getMoisture();
  88.     return currMoisture;
  89.   };
  90.  
  91.   void raiseMoistureToRequiredLevel(){
  92.     byte currRequiredMoisture = calcMinSatisfactoryMoisture();
  93.     while(currMoisture < currRequiredMoisture){
  94.         itsPump.pumpLiquid(5000);
  95.         delay(1000);
  96.         Serial.print(determineMoisture());
  97.     }
  98.   };
  99. };
  100.  
  101. Pot pot(/* required moisture level (in %) */50,
  102. /*soil moisture sensor pin*/ 0xA0,
  103. /*pump pin*/ 7);
  104.  
  105. void printMessagesOnLCD(const char* str1, const char* str2, const word theirTimeOnScreen){
  106.   lcd.clear();
  107.   lcd.setCursor(0,0);
  108.   lcd.print(str1);
  109.   lcd.setCursor(0,1);
  110.   lcd.print(str2);
  111.   delay(theirTimeOnScreen);
  112.   lcd.clear();
  113. }
  114.  
  115.  
  116. void setup()
  117. {
  118.  
  119.   Serial.begin(9600);
  120.   lcd.init();
  121.   lcd.backlight();
  122.  
  123.   RTC.setDS1302Time(00, 52, 14, 5, 18, 4, 2019);
  124.   pinMode(BUTTON_PIN,INPUT);
  125.  
  126.   while(!digitalRead(BUTTON_PIN)){
  127.     printMessagesOnLCD("What level of", "moisture should", delayBetweenMessages);
  128.     printMessagesOnLCD("be maintained", "in the pot?", delayBetweenMessages);
  129.     printMessagesOnLCD("Press button", "to continue", delayBetweenMessages);
  130.   }
  131.      
  132.   byte moistureLevel;
  133.  
  134.   pinMode(PONETIOMETER_PIN, INPUT);
  135.   delay(2000);
  136.   while (!digitalRead(BUTTON_PIN)){
  137.     moistureLevel = analogRead(PONETIOMETER_PIN) / DIVIER_FROM_ANALOG_TO_PERCENT;
  138.     lcd.setCursor(0,0);
  139.     lcd.print(moistureLevel);
  140.     lcd.print(" %");
  141.     delay(1000);
  142.     printMessagesOnLCD("Press button", "to continue", delayBetweenMessages);
  143.   }
  144.   pot.setRequiredMoisture(moistureLevel);
  145. }
  146.  
  147. void loop()
  148. {
  149.   RTC.updateTime();
  150.  
  151.  // if(ItsMorningNow()){
  152.  Serial.println(pot.determineMoisture());
  153.       if(pot.needsWatering()){
  154.         pot.raiseMoistureToRequiredLevel();
  155.       }
  156.   //}
  157.    
  158.  //delay(_3_HOURS);
  159. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top