CCCPaste

Flow Management 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: Flow Management
	- Source Code NOT compiled for: Arduino Uno
	- Source Code created on: 2024-08-22 21:58:47

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

/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
	/* detect leakage between two flowmeters and if */
	/* detected stop the pump */
/****** END SYSTEM REQUIREMENTS *****/

/****** DEFINITION OF LIBRARIES *****/
#include 	// https://github.com/hafidhh/FlowSensor-Arduino
#include 	// https://github.com/honvl/Seeed-Xiao-NRF52840-Battery

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

/***** DEFINITION OF DIGITAL INPUT PINS *****/
const uint8_t Sam_YF_S401_OUT_PIN_D2 = 2; // Corrected pin naming convention
const uint8_t Sam_YF_S401_OUT_PIN_D3 = 3; // Pin for second flow sensor (for leakage detection)
const uint8_t Pump_PIN = 4; // Pin to control the pump

/****** DEFINITION OF LIBRARIES CLASS INSTANCES *****/
#define type YFS401 // Define the sensor type
FlowSensor Sensor(type, Sam_YF_S401_OUT_PIN_D2); // Initialize FlowSensor object for the first sensor
FlowSensor Sensor2(type, Sam_YF_S401_OUT_PIN_D3); // Initialize FlowSensor object for the second sensor

unsigned long timebefore = 0; // Variable to track time for flow rate reading
unsigned long reset = 0; // Variable to track time for volume reset

/****** FUNCTION DEFINITIONS *****/
void count() {
    Sensor.count(); // Count pulses from the first sensor
    Sensor2.count(); // Count pulses from the second sensor
}

void setup(void) {
    Serial.begin(115200); // Start serial communication at 115200 baud rate
    Sensor.begin(count); // Initialize the first sensor with the count function
    Sensor2.begin(count); // Initialize the second sensor with the count function
    pinMode(Sam_YF_S401_OUT_PIN_D2, INPUT); // Set the first sensor pin as input
    pinMode(Sam_YF_S401_OUT_PIN_D3, INPUT); // Set the second sensor pin as input
    pinMode(Pump_PIN, OUTPUT); // Set the pump control pin as output
    digitalWrite(Pump_PIN, HIGH); // Start with the pump turned on
}

void loop(void) {
    // Read flow rate and volume every second
    if (millis() - timebefore >= 1000) {
        Sensor.read(); // Read the first sensor data
        Sensor2.read(); // Read the second sensor data
        Serial.print("Flow rate (L/minute) : ");
        Serial.println(Sensor.getFlowRate_m()); // Print flow rate of the first sensor
        Serial.print("Volume (L) : ");
        Serial.println(Sensor.getVolume()); // Print volume of the first sensor

        // Check for leakage between the two flowmeters
        if (Sensor.getFlowRate_m() != Sensor2.getFlowRate_m()) {
            Serial.println("Leak detected! Stopping the pump.");
            digitalWrite(Pump_PIN, LOW); // Stop the pump if a leak is detected
        }

        timebefore = millis(); // Update time tracker
    }

    // Reset volume every 60 seconds
    if (millis() - reset >= 60000) {
        Sensor.resetVolume(); // Reset the volume of the first sensor
        Sensor2.resetVolume(); // Reset the volume of the second sensor
        reset = millis(); // Update reset time tracker
    }
}

/* END CODE */