Advertisement
Tywais

Ultrasonic Water Level

Mar 24th, 2025
343
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.79 KB | Science | 0 0
  1. #include <SPI.h>
  2. #include <NewPing.h>
  3. #include "SSD1306Ascii.h"
  4. #include "SSD1306AsciiWire.h"
  5. // 0X3C+SA0 - 0x3C or 0x3D
  6. #define I2C_ADDRESS 0x3C
  7. SSD1306AsciiWire oled;
  8.  
  9. #define TRIGGER_PIN  9  // Arduino pin tied to trigger pin on the ultrasonic sensor.
  10. #define ECHO_PIN     10  // Arduino pin tied to echo pin on the ultrasonic sensor.
  11. #define MAX_DISTANCE 400 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
  12.  
  13. NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
  14.  
  15. #include "OneButton.h"    
  16. int ButtonPin = 12;                          //we need the OneButton library
  17. OneButton button(ButtonPin, true);                         //attach a button on pin D12 to the library
  18.  
  19. int temperature = 0;
  20. long distance = 0;
  21. float pingtime = 0;
  22. float pi = 3.1415926;
  23. int base = 0;  //distance to bottom of container (offset)
  24. int height = 0;  //actual height of liquid.
  25. float diameter = 93.11;  //diameter in mm of container  
  26. float radius = diameter/2;  //Area = pi * r^2
  27. float volume = 0;           //volume of liquid. π * radius² * height
  28. float liters = 0;
  29.  
  30. void setup() {
  31.   // Start the Serial communication
  32.   Serial.begin(115200);
  33.   pinMode(ButtonPin, INPUT_PULLUP);
  34.   button.attachClick(singleclick);                  // link the function to be called on a singleclick event.
  35.  
  36.   Wire.begin();
  37.   Wire.setClock(400000L);
  38.   oled.begin(&Adafruit128x32, I2C_ADDRESS);
  39.   oled.setFont(TimesNewRoman16_italic);   //(Adafruit5x7);
  40.   oled.clear();
  41.  
  42.   oled.setCursor(0,0); //(col,row)
  43.   //oled.set2X();
  44.   oled.setInvertMode(2);
  45.   oled.println("      CAL     ");
  46.  
  47.   delay(2000);   //delay to calibrate base
  48.   pingtime = sonar.ping();
  49.   pingtime = pingtime + (.6 * temperature);
  50.   pingtime = pingtime/2;
  51.   distance = pingtime * (0.343);  
  52.   base = distance;
  53.   delay(1000);
  54.   oled.setCursor(0,0); //(col,row)
  55.   //oled.set2X();
  56.   oled.setInvertMode(2);
  57.   oled.println("      LITERS     ");
  58.  
  59. }
  60.  
  61. void loop() {
  62.   delay(250);
  63.  
  64.   pingtime = sonar.ping();
  65.   pingtime = pingtime + (.6 * temperature);
  66.   pingtime = pingtime/2;
  67.   distance = pingtime * (0.343);  
  68.   height = base - distance;
  69.  //Volume = π * radius² * height
  70.   volume = pi * sq(radius) * height;  //cubic mm
  71.   liters = volume * 1.0E-6;           //convert to liters
  72.   // Display the distance
  73.   oled.setFont(TimesNewRoman16);   //(Adafruit5x7);
  74.   oled.clearToEOL();
  75.   oled.setCursor(40,10); //(col,row)
  76.   oled.setInvertMode(0);
  77.   oled.println(liters);
  78.  
  79.   Serial.print("$");    //send to process app
  80.   Serial.print(liters);
  81.   Serial.print(";");
  82.  
  83. //  Serial.println(liters);
  84. //  Serial.println(distance);
  85. //  Serial.println();
  86. }
  87.  
  88. void singleclick(){  
  89.   base = distance;    //set the base in mm Bottom of container
  90. }
  91.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement