Advertisement
pleasedontcode

Ultrasonic Indicator rev_01

Apr 17th, 2024
66
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: Ultrasonic Indicator
  13.     - Source Code compiled for: Arduino Uno
  14.     - Source Code created on: 2024-04-17 14:09:14
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* water level indicator */
  21. /****** END SYSTEM REQUIREMENTS *****/
  22.  
  23. /****** DEFINITION OF LIBRARIES *****/
  24. #include <Ultrasonic.h> // https://github.com/ErickSimoes/Ultrasonic
  25.  
  26. /****** FUNCTION PROTOTYPES *****/
  27. void setup(void);
  28. void loop(void);
  29. void updateOutputs(void);
  30. void checkWaterLevel(unsigned int distance);
  31.  
  32. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  33. const uint8_t US_HC_SR04_Echo_PIN_D3 = 3;
  34.  
  35. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  36. const uint8_t US_HC_SR04_Trigger_PIN_D2 = 2;
  37. const uint8_t WATER_LEVEL_INDICATOR_PIN = 4;
  38.  
  39. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  40. /***** used to store raw data *****/
  41. bool US_HC_SR04_Trigger_PIN_D2_rawData = 0;
  42.  
  43. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  44. /***** used to store data after characteristic curve transformation *****/
  45. float US_HC_SR04_Trigger_PIN_D2_phyData = 0.0;
  46.  
  47. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  48. Ultrasonic ultrasonic(US_HC_SR04_Trigger_PIN_D2, US_HC_SR04_Echo_PIN_D3);
  49.  
  50. void setup(void)
  51. {
  52.   // put your setup code here, to run once:
  53.   pinMode(US_HC_SR04_Echo_PIN_D3, INPUT);
  54.   pinMode(US_HC_SR04_Trigger_PIN_D2, OUTPUT);
  55.   pinMode(WATER_LEVEL_INDICATOR_PIN, OUTPUT);
  56.   Serial.begin(9600); // Initialize serial communication
  57. }
  58.  
  59. void loop(void)
  60. {
  61.   // put your main code here, to run repeatedly:
  62.   updateOutputs(); // Refresh output data
  63.  
  64.   // Read distance from ultrasonic sensor
  65.   unsigned int distance = ultrasonic.read();
  66.  
  67.   // Print distance in centimeters
  68.   Serial.print("Distance in CM: ");
  69.   Serial.println(distance);
  70.  
  71.   // Check water level and update the indicator
  72.   checkWaterLevel(distance);
  73.  
  74.   delay(1000);
  75. }
  76.  
  77. void updateOutputs(void)
  78. {
  79.   digitalWrite(US_HC_SR04_Trigger_PIN_D2, US_HC_SR04_Trigger_PIN_D2_rawData);
  80. }
  81.  
  82. void checkWaterLevel(unsigned int distance)
  83. {
  84.   // Define the threshold distance for water level
  85.   const unsigned int WATER_LEVEL_THRESHOLD = 10;
  86.  
  87.   if (distance <= WATER_LEVEL_THRESHOLD)
  88.   {
  89.     // Water level is low, turn on the indicator
  90.     digitalWrite(WATER_LEVEL_INDICATOR_PIN, HIGH);
  91.   }
  92.   else
  93.   {
  94.     // Water level is normal, turn off the indicator
  95.     digitalWrite(WATER_LEVEL_INDICATOR_PIN, LOW);
  96.   }
  97. }
  98.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement