Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <NewPing.h>
- #include "SSD1306.h"
- #define TRIGGER_PIN 26 // Arduino pin tied to trigger pin on the ultrasonic sensor.
- #define ECHO_PIN 25 // Arduino pin tied to echo pin on the ultrasonic sensor.
- #define MAX_DISTANCE 25 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
- float old_amount = 0;
- float water_amount_in_ml = 0;
- NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
- SSD1306 display(0x3c, 4, 15);
- void setup() {
- Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results.
- display.init(); // initialise the OLED
- display.setFont(ArialMT_Plain_24);
- display.setTextAlignment(TEXT_ALIGN_CENTER_BOTH);
- }
- void waterCaclulation() {
- float water_level = 0;
- float read_value = 0;
- for (int i = 0; i < 5; i++) { // Take five reading to calculate an average
- read_value = sonar.ping_cm();
- if (read_value <= 21 && read_value >= 1) { // Indicatiing a valid value
- water_level = water_level + read_value;
- } else {
- return;
- }
- delay(50);
- }
- float average_water_level = 21 - water_level / 5; //find average from five reading, 21 = bottle height
- water_amount_in_ml = float(average_water_level * 37.5); //1 cm water level = 37.5 mL
- Serial.print(water_amount_in_ml);
- Serial.print(" ML Remaining\r\n");
- Serial.println(read_value);
- display.clear();
- display.drawString(63, 20, String(water_amount_in_ml) + " mL");
- display.display();
- }
- void loop() {
- waterCaclulation();
- delay(1000);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement