Advertisement
pleasedontcode

v1 rev_01

Dec 4th, 2023
94
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: v1
  13.     - Source Code compiled for: Arduino Uno
  14.     - Source Code created on: 2023-12-05 02:22:08
  15.     - Source Code generated by: Pham
  16.  
  17. ********* Pleasedontcode.com **********/
  18.  
  19. /****** SYSTEM REQUIREMENTS *****/
  20. /****** SYSTEM REQUIREMENT 1 *****/
  21.     /* Use hc-sr04 ultrasonic sensor and yf-s401 flow */
  22.     /* sensor to make a water pumping system with a relay */
  23.     /* connected to the pump when the water volume is */
  24.     /* below 20% and stop the pump when the water volume */
  25.     /* is over 80% in a 16cm high tank, The flow sensor */
  26.     /* will c */
  27. /****** END SYSTEM REQUIREMENTS *****/
  28.  
  29. /****** DEFINITION OF LIBRARIES *****/
  30. #include <Arduino.h>
  31. #include <Wire.h>
  32. #include <LiquidCrystal_I2C.h>
  33.  
  34. const int triggerPin = 2;
  35. const int echoPin = 3;
  36. const int flowSensorPin = 4;
  37. const int relayPin = 5;
  38.  
  39. long duration;
  40. int distance;
  41. int flowRate;
  42. float waterVolume;
  43. float tankHeight = 16;
  44. float tankCapacity = 100;
  45. float thresholdLow = tankCapacity * 0.2;
  46. float thresholdHigh = tankCapacity * 0.8;
  47.  
  48. LiquidCrystal_I2C lcd(0x27, 20, 4);
  49.  
  50. void setup()
  51. {
  52.   pinMode(triggerPin, OUTPUT);
  53.   pinMode(echoPin, INPUT);
  54.   pinMode(flowSensorPin, INPUT);
  55.   pinMode(relayPin, OUTPUT);
  56.  
  57.   lcd.begin(20, 4);
  58.   lcd.backlight();
  59.   lcd.clear();
  60. }
  61.  
  62. void loop()
  63. {
  64.   digitalWrite(triggerPin, LOW);
  65.   delayMicroseconds(2);
  66.   digitalWrite(triggerPin, HIGH);
  67.   delayMicroseconds(10);
  68.   digitalWrite(triggerPin, LOW);
  69.  
  70.   duration = pulseIn(echoPin, HIGH);
  71.   distance = duration * 0.034 / 2;
  72.  
  73.   flowRate = pulseIn(flowSensorPin, HIGH);
  74.  
  75.   waterVolume = tankHeight * (tankCapacity - distance) / tankCapacity;
  76.  
  77.   if (waterVolume < thresholdLow) {
  78.     digitalWrite(relayPin, HIGH);
  79.   } else if (waterVolume > thresholdHigh) {
  80.     digitalWrite(relayPin, LOW);
  81.   }
  82.  
  83.   lcd.setCursor(0, 0);
  84.   lcd.print("Distance: ");
  85.   lcd.print(distance);
  86.   lcd.print(" cm");
  87.  
  88.   lcd.setCursor(0, 1);
  89.   lcd.print("Flow Rate: ");
  90.   lcd.print(flowRate);
  91.   lcd.print(" ml/s");
  92.  
  93.   lcd.setCursor(0, 2);
  94.   lcd.print("Water Volume: ");
  95.   lcd.print(waterVolume);
  96.   lcd.print(" L");
  97.  
  98.   delay(1000);
  99. }
  100.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement