Advertisement
pleasedontcode

Moisture Control rev_02

Feb 29th, 2024
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: Moisture Control
  13.     - Source Code compiled for: Arduino Uno
  14.     - Source Code created on: 2024-03-01 04:58:40
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* If moisture content below 40% turn on the pump for */
  21.     /* 5 minutes. Only allow pump to run once a day */
  22. /****** SYSTEM REQUIREMENT 2 *****/
  23.     /* if moisture is above 40% turn off pump */
  24. /****** END SYSTEM REQUIREMENTS *****/
  25.  
  26. /****** DEFINITION OF LIBRARIES *****/
  27. #include <Wire.h>
  28. #include <LiquidCrystal_I2C.h> //https://github.com/marcoschwartz/LiquidCrystal_I2C
  29. #include <DS18B20.h>          //https://github.com/matmunk/DS18B20
  30.  
  31. /****** FUNCTION PROTOTYPES *****/
  32. void setup(void);
  33. void loop(void);
  34. void turnOnPump(void);
  35. void turnOffPump(void);
  36.  
  37. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  38. const uint8_t Moisture_Sensor_PIN_D3 = 3;
  39.  
  40. /***** DEFINITION OF I2C PINS *****/
  41. const uint8_t LCD_LCD1602I2C_I2C_PIN_SDA_A4 = A4;
  42. const uint8_t LCD_LCD1602I2C_I2C_PIN_SCL_A5 = A5;
  43. const uint8_t LCD_LCD1602I2C_I2C_SLAVE_ADDRESS = 0x27; // Update the I2C slave address
  44.  
  45. /****** DEFINITION OF LIBRARIES CLASS INSTANCES *****/
  46. LiquidCrystal_I2C lcd(LCD_LCD1602I2C_I2C_SLAVE_ADDRESS, 16, 2); // Update the LCD dimensions
  47.  
  48. DS18B20 sensor(2); // Initialize the DS18B20 object with the pin number where the sensor is connected
  49.  
  50. bool isPumpOn = false; // Variable to track the pump state
  51. unsigned long previousPumpOnTime = 0; // Variable to store the timestamp of the previous pump activation
  52.  
  53. void setup(void)
  54. {
  55.   // put your setup code here, to run once:
  56.   pinMode(Moisture_Sensor_PIN_D3, INPUT);
  57.  
  58.   lcd.begin(16, 2);      // Set the LCD dimensions (16 columns and 2 rows)
  59.   lcd.setBacklight(LOW); // Turn off the backlight initially
  60. }
  61.  
  62. void loop(void)
  63. {
  64.   // put your main code here, to run repeatedly:
  65.  
  66.   // Read the moisture level from the sensor
  67.   int moistureLevel = analogRead(Moisture_Sensor_PIN_D3);
  68.  
  69.   // Check if moisture level is below 40%
  70.   if (moistureLevel < 400)
  71.   {
  72.     // Check if the pump is allowed to run (once a day)
  73.     unsigned long currentMillis = millis();
  74.     if (!isPumpOn && currentMillis - previousPumpOnTime >= 24 * 60 * 60 * 1000)
  75.     {
  76.       // Turn on the pump for 5 minutes
  77.       turnOnPump();
  78.       previousPumpOnTime = currentMillis;
  79.     }
  80.   }
  81.   else
  82.   {
  83.     // Turn off the pump if it is currently on
  84.     if (isPumpOn)
  85.     {
  86.       turnOffPump();
  87.     }
  88.   }
  89.  
  90.   // Update the LCD display with the moisture level
  91.   lcd.clear();
  92.   lcd.setCursor(0, 0);
  93.   lcd.print("Moisture level:");
  94.   lcd.setCursor(0, 1);
  95.   lcd.print(moistureLevel);
  96.  
  97.   delay(1000); // Delay for 1 second
  98. }
  99.  
  100. void turnOnPump()
  101. {
  102.   // Code to turn on the pump
  103.   // Replace this with your actual code to control the pump
  104.   isPumpOn = true;
  105.  
  106.   // Display pump status on LCD
  107.   lcd.setCursor(0, 2);
  108.   lcd.print("Pump: ON");
  109. }
  110.  
  111. void turnOffPump()
  112. {
  113.   // Code to turn off the pump
  114.   // Replace this with your actual code to control the pump
  115.   isPumpOn = false;
  116.  
  117.   // Display pump status on LCD
  118.   lcd.setCursor(0, 2);
  119.   lcd.print("Pump: OFF");
  120. }
  121.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement