Advertisement
pleasedontcode

Flow Management rev_01

Aug 22nd, 2024
132
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: Flow Management
  13.     - Source Code NOT compiled for: Arduino Uno
  14.     - Source Code created on: 2024-08-22 21:58:47
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* detect leakage between two flowmeters and if */
  21.     /* detected stop the pump */
  22. /****** END SYSTEM REQUIREMENTS *****/
  23.  
  24. /****** DEFINITION OF LIBRARIES *****/
  25. #include <FlowSensor.h> // https://github.com/hafidhh/FlowSensor-Arduino
  26. #include <xiaobattery.h>    // https://github.com/honvl/Seeed-Xiao-NRF52840-Battery
  27.  
  28. /****** FUNCTION PROTOTYPES *****/
  29. void setup(void);
  30. void loop(void);
  31.  
  32. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  33. const uint8_t Sam_YF_S401_OUT_PIN_D2 = 2; // Corrected pin naming convention
  34. const uint8_t Sam_YF_S401_OUT_PIN_D3 = 3; // Pin for second flow sensor (for leakage detection)
  35. const uint8_t Pump_PIN = 4; // Pin to control the pump
  36.  
  37. /****** DEFINITION OF LIBRARIES CLASS INSTANCES *****/
  38. #define type YFS401 // Define the sensor type
  39. FlowSensor Sensor(type, Sam_YF_S401_OUT_PIN_D2); // Initialize FlowSensor object for the first sensor
  40. FlowSensor Sensor2(type, Sam_YF_S401_OUT_PIN_D3); // Initialize FlowSensor object for the second sensor
  41.  
  42. unsigned long timebefore = 0; // Variable to track time for flow rate reading
  43. unsigned long reset = 0; // Variable to track time for volume reset
  44.  
  45. /****** FUNCTION DEFINITIONS *****/
  46. void count() {
  47.     Sensor.count(); // Count pulses from the first sensor
  48.     Sensor2.count(); // Count pulses from the second sensor
  49. }
  50.  
  51. void setup(void) {
  52.     Serial.begin(115200); // Start serial communication at 115200 baud rate
  53.     Sensor.begin(count); // Initialize the first sensor with the count function
  54.     Sensor2.begin(count); // Initialize the second sensor with the count function
  55.     pinMode(Sam_YF_S401_OUT_PIN_D2, INPUT); // Set the first sensor pin as input
  56.     pinMode(Sam_YF_S401_OUT_PIN_D3, INPUT); // Set the second sensor pin as input
  57.     pinMode(Pump_PIN, OUTPUT); // Set the pump control pin as output
  58.     digitalWrite(Pump_PIN, HIGH); // Start with the pump turned on
  59. }
  60.  
  61. void loop(void) {
  62.     // Read flow rate and volume every second
  63.     if (millis() - timebefore >= 1000) {
  64.         Sensor.read(); // Read the first sensor data
  65.         Sensor2.read(); // Read the second sensor data
  66.         Serial.print("Flow rate (L/minute) : ");
  67.         Serial.println(Sensor.getFlowRate_m()); // Print flow rate of the first sensor
  68.         Serial.print("Volume (L) : ");
  69.         Serial.println(Sensor.getVolume()); // Print volume of the first sensor
  70.  
  71.         // Check for leakage between the two flowmeters
  72.         if (Sensor.getFlowRate_m() != Sensor2.getFlowRate_m()) {
  73.             Serial.println("Leak detected! Stopping the pump.");
  74.             digitalWrite(Pump_PIN, LOW); // Stop the pump if a leak is detected
  75.         }
  76.  
  77.         timebefore = millis(); // Update time tracker
  78.     }
  79.  
  80.     // Reset volume every 60 seconds
  81.     if (millis() - reset >= 60000) {
  82.         Sensor.resetVolume(); // Reset the volume of the first sensor
  83.         Sensor2.resetVolume(); // Reset the volume of the second sensor
  84.         reset = millis(); // Update reset time tracker
  85.     }
  86. }
  87.  
  88. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement