CCCPaste

Moisture Control rev_02

/********* Pleasedontcode.com **********

	Pleasedontcode thanks you for automatic code generation! Enjoy your code!

	- Terms and Conditions:
	You have a non-exclusive, revocable, worldwide, royalty-free license
	for personal and commercial use. Attribution is optional; modifications
	are allowed, but you're responsible for code maintenance. We're not
	liable for any loss or damage. For full terms,
	please visit pleasedontcode.com/termsandconditions.

	- Project: Moisture Control
	- Source Code compiled for: Arduino Uno
	- Source Code created on: 2024-03-01 04:58:40

********* Pleasedontcode.com **********/

/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
	/* If moisture content below 40% turn on the pump for */
	/* 5 minutes. Only allow pump to run once a day */
/****** SYSTEM REQUIREMENT 2 *****/
	/* if moisture is above 40% turn off pump */
/****** END SYSTEM REQUIREMENTS *****/

/****** DEFINITION OF LIBRARIES *****/
#include 
#include  //https://github.com/marcoschwartz/LiquidCrystal_I2C
#include           //https://github.com/matmunk/DS18B20

/****** FUNCTION PROTOTYPES *****/
void setup(void);
void loop(void);
void turnOnPump(void);
void turnOffPump(void);

/***** DEFINITION OF DIGITAL INPUT PINS *****/
const uint8_t Moisture_Sensor_PIN_D3 = 3;

/***** DEFINITION OF I2C PINS *****/
const uint8_t LCD_LCD1602I2C_I2C_PIN_SDA_A4 = A4;
const uint8_t LCD_LCD1602I2C_I2C_PIN_SCL_A5 = A5;
const uint8_t LCD_LCD1602I2C_I2C_SLAVE_ADDRESS = 0x27; // Update the I2C slave address

/****** DEFINITION OF LIBRARIES CLASS INSTANCES *****/
LiquidCrystal_I2C lcd(LCD_LCD1602I2C_I2C_SLAVE_ADDRESS, 16, 2); // Update the LCD dimensions

DS18B20 sensor(2); // Initialize the DS18B20 object with the pin number where the sensor is connected

bool isPumpOn = false; // Variable to track the pump state
unsigned long previousPumpOnTime = 0; // Variable to store the timestamp of the previous pump activation

void setup(void)
{
  // put your setup code here, to run once:
  pinMode(Moisture_Sensor_PIN_D3, INPUT);

  lcd.begin(16, 2);      // Set the LCD dimensions (16 columns and 2 rows)
  lcd.setBacklight(LOW); // Turn off the backlight initially
}

void loop(void)
{
  // put your main code here, to run repeatedly:
  
  // Read the moisture level from the sensor
  int moistureLevel = analogRead(Moisture_Sensor_PIN_D3);

  // Check if moisture level is below 40%
  if (moistureLevel < 400)
  {
    // Check if the pump is allowed to run (once a day)
    unsigned long currentMillis = millis();
    if (!isPumpOn && currentMillis - previousPumpOnTime >= 24 * 60 * 60 * 1000)
    {
      // Turn on the pump for 5 minutes
      turnOnPump();
      previousPumpOnTime = currentMillis;
    }
  }
  else
  {
    // Turn off the pump if it is currently on
    if (isPumpOn)
    {
      turnOffPump();
    }
  }
  
  // Update the LCD display with the moisture level
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Moisture level:");
  lcd.setCursor(0, 1);
  lcd.print(moistureLevel);
  
  delay(1000); // Delay for 1 second
}

void turnOnPump()
{
  // Code to turn on the pump
  // Replace this with your actual code to control the pump
  isPumpOn = true;

  // Display pump status on LCD
  lcd.setCursor(0, 2);
  lcd.print("Pump: ON");
}

void turnOffPump()
{
  // Code to turn off the pump
  // Replace this with your actual code to control the pump
  isPumpOn = false;

  // Display pump status on LCD
  lcd.setCursor(0, 2);
  lcd.print("Pump: OFF");
}