CCCPaste

Water Level 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: Water Level
	- Source Code compiled for: Arduino Uno
	- Source Code created on: 2024-01-01 12:45:27

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

/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
	/* Measure distance If distance is greater than 30 cm */
	/* print "water overflow " and turn buzzer on If */
	/* distance is less than 15 cm print "water underflow */
	/* " and turn buzzer on create this in a function */
	/* called water_level */
/****** END SYSTEM REQUIREMENTS *****/

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

/***** GLOBAL VARIABLES *****/
const uint8_t waterFlowPin = 6; // Pin to control the water flow
const uint8_t buzzerPin = 7; // Pin to control the buzzer

/***** DEFINITION OF DIGITAL INPUT PINS *****/
const uint8_t myUS_HC_SR04_ECHO_PIN_D3 = 3;
const uint8_t myUS_HC_SR04_ECHO_PIN_D5 = 5;

/***** DEFINITION OF DIGITAL OUTPUT PINS *****/
const uint8_t myUS_HC_SR04_TRIGGER_PIN_D2 = 2;
const uint8_t myUS_HC_SR04_TRIGGER_PIN_D4 = 4;

/****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
Ultrasonic ultrasonic_sensor_D3(myUS_HC_SR04_TRIGGER_PIN_D2, myUS_HC_SR04_ECHO_PIN_D3);
Ultrasonic ultrasonic_sensor_D5(myUS_HC_SR04_TRIGGER_PIN_D4, myUS_HC_SR04_ECHO_PIN_D5);

/****** FUNCTION PROTOTYPES *****/
void setup(void);
void loop(void);
void water_level(int distance);

void setup(void)
{
    // put your setup code here, to run once:
    pinMode(waterFlowPin, OUTPUT);
    pinMode(buzzerPin, OUTPUT);

    pinMode(myUS_HC_SR04_ECHO_PIN_D3, INPUT);
    pinMode(myUS_HC_SR04_ECHO_PIN_D5, INPUT);

    pinMode(myUS_HC_SR04_TRIGGER_PIN_D2, OUTPUT);
    pinMode(myUS_HC_SR04_TRIGGER_PIN_D4, OUTPUT);

    Serial.begin(9600);
}

void loop(void)
{
    // put your main code here, to run repeatedly:
    unsigned int distance_D3 = ultrasonic_sensor_D3.read();
    unsigned int distance_D5 = ultrasonic_sensor_D5.read();

    // Readings from D3 sensor
    Serial.print("Distance using sensor D3: ");
    Serial.print(distance_D3);
    Serial.println(" cm");

    // Readings from D5 sensor
    Serial.print("Distance using sensor D5: ");
    Serial.print(distance_D5);
    Serial.println(" cm");

    // Check the water level
    water_level(distance_D3);
    
    delay(1000);
}
 
void water_level(int distance)
{
    constexpr int waterOverflowDistance = 30; // Threshold for water overflow condition
    constexpr int waterUnderflowDistance = 15; // Threshold for water underflow condition
    
    if(distance > waterOverflowDistance) // If distance is greater than 30 cm
    {
        digitalWrite(waterFlowPin, HIGH); // Turn the water flow pin ON
        digitalWrite(buzzerPin, HIGH); // Turn the buzzer ON
        Serial.println("Water Overflow");
    }
    else if (distance < waterUnderflowDistance) // If distance is less than 15 cm
    {
        digitalWrite(waterFlowPin, HIGH); // Turn the water flow pin ON
        digitalWrite(buzzerPin, HIGH); // Turn the buzzer ON
        Serial.println("Water Underflow");
    }
    else
    {
        digitalWrite(waterFlowPin, LOW); // Turn the water flow pin OFF
        digitalWrite(buzzerPin, LOW); // Turn the buzzer OFF
    }
}