CCCPaste

v1 rev_01

/********* 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: v1
	- Source Code compiled for: Arduino Uno
	- Source Code created on: 2023-12-05 02:22:08
	- Source Code generated by: Pham

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

/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
	/* Use hc-sr04 ultrasonic sensor and yf-s401 flow */
	/* sensor to make a water pumping system with a relay */
	/* connected to the pump when the water volume is */
	/* below 20% and stop the pump when the water volume */
	/* is over 80% in a 16cm high tank, The flow sensor */
	/* will c */
/****** END SYSTEM REQUIREMENTS *****/

/****** DEFINITION OF LIBRARIES *****/
#include 
#include 
#include 

const int triggerPin = 2;
const int echoPin = 3;
const int flowSensorPin = 4;
const int relayPin = 5;

long duration;
int distance;
int flowRate;
float waterVolume;
float tankHeight = 16;
float tankCapacity = 100;
float thresholdLow = tankCapacity * 0.2;
float thresholdHigh = tankCapacity * 0.8;

LiquidCrystal_I2C lcd(0x27, 20, 4);

void setup()
{
  pinMode(triggerPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(flowSensorPin, INPUT);
  pinMode(relayPin, OUTPUT);

  lcd.begin(20, 4);
  lcd.backlight();
  lcd.clear();
}

void loop()
{
  digitalWrite(triggerPin, LOW);
  delayMicroseconds(2);
  digitalWrite(triggerPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(triggerPin, LOW);

  duration = pulseIn(echoPin, HIGH);
  distance = duration * 0.034 / 2;

  flowRate = pulseIn(flowSensorPin, HIGH);

  waterVolume = tankHeight * (tankCapacity - distance) / tankCapacity;

  if (waterVolume < thresholdLow) {
    digitalWrite(relayPin, HIGH);
  } else if (waterVolume > thresholdHigh) {
    digitalWrite(relayPin, LOW);
  }

  lcd.setCursor(0, 0);
  lcd.print("Distance: ");
  lcd.print(distance);
  lcd.print(" cm");

  lcd.setCursor(0, 1);
  lcd.print("Flow Rate: ");
  lcd.print(flowRate);
  lcd.print(" ml/s");

  lcd.setCursor(0, 2);
  lcd.print("Water Volume: ");
  lcd.print(waterVolume);
  lcd.print(" L");

  delay(1000);
}