Advertisement
pleasedontcode

Water Level rev_01

Jan 1st, 2024
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: Water Level
  13.     - Source Code compiled for: Arduino Uno
  14.     - Source Code created on: 2024-01-01 12:45:27
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Measure distance If distance is greater than 30 cm */
  21.     /* print "water overflow " and turn buzzer on If */
  22.     /* distance is less than 15 cm print "water underflow */
  23.     /* " and turn buzzer on create this in a function */
  24.     /* called water_level */
  25. /****** END SYSTEM REQUIREMENTS *****/
  26.  
  27. /****** DEFINITION OF LIBRARIES *****/
  28. #include <Arduino.h>
  29. #include <Ultrasonic.h>
  30.  
  31. /***** GLOBAL VARIABLES *****/
  32. const uint8_t waterFlowPin = 6; // Pin to control the water flow
  33. const uint8_t buzzerPin = 7; // Pin to control the buzzer
  34.  
  35. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  36. const uint8_t myUS_HC_SR04_ECHO_PIN_D3 = 3;
  37. const uint8_t myUS_HC_SR04_ECHO_PIN_D5 = 5;
  38.  
  39. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  40. const uint8_t myUS_HC_SR04_TRIGGER_PIN_D2 = 2;
  41. const uint8_t myUS_HC_SR04_TRIGGER_PIN_D4 = 4;
  42.  
  43. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  44. Ultrasonic ultrasonic_sensor_D3(myUS_HC_SR04_TRIGGER_PIN_D2, myUS_HC_SR04_ECHO_PIN_D3);
  45. Ultrasonic ultrasonic_sensor_D5(myUS_HC_SR04_TRIGGER_PIN_D4, myUS_HC_SR04_ECHO_PIN_D5);
  46.  
  47. /****** FUNCTION PROTOTYPES *****/
  48. void setup(void);
  49. void loop(void);
  50. void water_level(int distance);
  51.  
  52. void setup(void)
  53. {
  54.     // put your setup code here, to run once:
  55.     pinMode(waterFlowPin, OUTPUT);
  56.     pinMode(buzzerPin, OUTPUT);
  57.  
  58.     pinMode(myUS_HC_SR04_ECHO_PIN_D3, INPUT);
  59.     pinMode(myUS_HC_SR04_ECHO_PIN_D5, INPUT);
  60.  
  61.     pinMode(myUS_HC_SR04_TRIGGER_PIN_D2, OUTPUT);
  62.     pinMode(myUS_HC_SR04_TRIGGER_PIN_D4, OUTPUT);
  63.  
  64.     Serial.begin(9600);
  65. }
  66.  
  67. void loop(void)
  68. {
  69.     // put your main code here, to run repeatedly:
  70.     unsigned int distance_D3 = ultrasonic_sensor_D3.read();
  71.     unsigned int distance_D5 = ultrasonic_sensor_D5.read();
  72.  
  73.     // Readings from D3 sensor
  74.     Serial.print("Distance using sensor D3: ");
  75.     Serial.print(distance_D3);
  76.     Serial.println(" cm");
  77.  
  78.     // Readings from D5 sensor
  79.     Serial.print("Distance using sensor D5: ");
  80.     Serial.print(distance_D5);
  81.     Serial.println(" cm");
  82.  
  83.     // Check the water level
  84.     water_level(distance_D3);
  85.    
  86.     delay(1000);
  87. }
  88.  
  89. void water_level(int distance)
  90. {
  91.     constexpr int waterOverflowDistance = 30; // Threshold for water overflow condition
  92.     constexpr int waterUnderflowDistance = 15; // Threshold for water underflow condition
  93.    
  94.     if(distance > waterOverflowDistance) // If distance is greater than 30 cm
  95.     {
  96.         digitalWrite(waterFlowPin, HIGH); // Turn the water flow pin ON
  97.         digitalWrite(buzzerPin, HIGH); // Turn the buzzer ON
  98.         Serial.println("Water Overflow");
  99.     }
  100.     else if (distance < waterUnderflowDistance) // If distance is less than 15 cm
  101.     {
  102.         digitalWrite(waterFlowPin, HIGH); // Turn the water flow pin ON
  103.         digitalWrite(buzzerPin, HIGH); // Turn the buzzer ON
  104.         Serial.println("Water Underflow");
  105.     }
  106.     else
  107.     {
  108.         digitalWrite(waterFlowPin, LOW); // Turn the water flow pin OFF
  109.         digitalWrite(buzzerPin, LOW); // Turn the buzzer OFF
  110.     }
  111. }
  112.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement