#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Arduino.h>
#include "HX711.h"
#include "soc/rtc.h"
#include "HX711.h"
#include <Pushbutton.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
const int trigPin = 5;
const int echoPin = 18;
const float tankHeight = 71.12; // Height of the liquid tank in centimeters
const float tankRadius = 17.78; // Radius of the liquid tank in centimeters
const float emptyThreshold = 1.0; // Distance threshold below which the tank is considered empty
const float maxDistance = 65.0;
const float pi = 3.14159265359; // Maximum distance to measure in centimeters
//define sound speed in cm/uS
#define SOUND_SPEED 0.034
#define CM_TO_INCH 0.393701
const int LOADCELL_DOUT_PIN = 16;
const int LOADCELL_SCK_PIN = 4;
HX711 scale;
int reading;
int lastReading;
//REPLACE WITH YOUR CALIBRATION FACTOR
#define CALIBRATION_FACTOR -471.497
//OLED Display
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
//Button
#define BUTTON_PIN 19
Pushbutton button(BUTTON_PIN);
long duration;
float distanceCm;
float distanceInch;
void setup() {
Serial.begin(115200);
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
delay(500);
display.clearDisplay();
//rtc_clk_cpu_freq_set(RTC_CPU_FREQ_80M);
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
delay(2000);
display.clearDisplay();
display.setTextColor(WHITE);
Serial.println("Initializing the scale");
scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
scale.set_scale(CALIBRATION_FACTOR); // this value is obtained by calibrating the scale with known weights; see the README for details
scale.tare(); // reset the scale to 0
display.setTextSize(2);
display.setTextColor(WHITE);
Serial.println("Setup done");
}
void loop() {
// Wait a bit before scanning again
delay(5000);
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculate the distance
distanceCm = duration * SOUND_SPEED/2;
if (distanceCm > 0 && distanceCm < tankHeight) { // Check if the distance is out of range
float volume = pi * tankRadius * tankRadius * (100 - distanceCm);
Serial.print(distanceCm);
Serial.println(" cm");
Serial.print(volume);
Serial.println(" liters");
delay(1000); // Wait for one second before taking the next measurement
return; // Exit the loop without calculating volume
} else {
Serial.println("Out of range");
}
// Prints the distance in the Serial Monitor
Serial.print("Distance (cm): ");
Serial.println(distanceCm);
display.clearDisplay();
display.setCursor(0, 5);
//Display distance in cm
display.print(distanceCm);
display.print(" cm");
float volume = pi * tankRadius * tankRadius * (100 - distanceCm);
display.print(volume);
display.println(" liters");
delay(1000); // Wait for one second before taking the next measurement
//return; // Exit the loop without calculating volume
display.print("Out of range");
//display.print(distanceCm);
//display.print(" cm");
// Display distance in inches
/* display.print(distanceInch);
display.print(" in");*/
if (button.getSingleDebouncedPress()){
Serial.print("tare...");
scale.tare();
}
if (scale.wait_ready_timeout(200)) {
reading = round(scale.get_units());
Serial.print("Weight: ");
Serial.println(reading);
if (reading != lastReading){
display.print(reading);
}
lastReading = reading;
}
else {
Serial.println("HX711 not found.");
}
display.display();
delay(500);
}