Advertisement
The_Intoxicator

Smart Water Bottle

Apr 1st, 2020
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.59 KB | None | 0 0
  1. #include <NewPing.h>
  2. #include "SSD1306.h"
  3.  
  4. #define TRIGGER_PIN  26  // Arduino pin tied to trigger pin on the ultrasonic sensor.
  5. #define ECHO_PIN     25  // Arduino pin tied to echo pin on the ultrasonic sensor.
  6. #define MAX_DISTANCE 25 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
  7.  
  8. float old_amount = 0;
  9. float water_amount_in_ml = 0;
  10.  
  11. NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
  12.  
  13. SSD1306 display(0x3c, 4, 15);
  14.  
  15. void setup() {
  16.   Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results.
  17.  
  18.   display.init(); // initialise the OLED
  19.   display.setFont(ArialMT_Plain_24);
  20.   display.setTextAlignment(TEXT_ALIGN_CENTER_BOTH);
  21. }
  22.  
  23. void waterCaclulation() {
  24.   float water_level = 0;
  25.   float read_value = 0;
  26.   for (int i = 0; i < 5; i++) { // Take five reading to calculate an average
  27.  
  28.     read_value = sonar.ping_cm();
  29.     if (read_value <= 21 && read_value >= 1) { // Indicatiing a valid value
  30.       water_level = water_level + read_value;
  31.     } else {
  32.       return;
  33.     }
  34.     delay(50);
  35.   }
  36.  
  37.   float average_water_level = 21 - water_level / 5; //find average from five reading, 21 = bottle height
  38.   water_amount_in_ml = float(average_water_level * 37.5); //1 cm water level = 37.5 mL
  39.   Serial.print(water_amount_in_ml);
  40.   Serial.print(" ML Remaining\r\n");
  41.   Serial.println(read_value);
  42.  
  43.   display.clear();
  44.   display.drawString(63, 20, String(water_amount_in_ml) + " mL");
  45.   display.display();
  46.  
  47. }
  48.  
  49. void loop() {
  50.   waterCaclulation();
  51.   delay(1000);
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement