#include <HX711.h>
#include <Servo.h>
Servo my_servo1; //servo for weight sensor
Servo my_servo2; //servo for water sensor
int pos = 0;
const int ini_weight = 100; //100grams weight variable
const int water_level = 100; //100 water level variable
int x = 1; //change according to calibration
int y = 0; //change according to calibration
// HX711 circuit wiring
const int LOADCELL_DOUT_PIN = 2;
const int LOADCELL_SCK_PIN = 3;
// water level sensor circuit wiring
const int sensorPower = 7;
const int sensorPin = A0;
int val = 0;
HX711 scale;
int calculate_weight();
int calculate_water();
void setup() {
Serial.begin(57600);
my_servo1.attach(8);
my_servo2.attach(9);
pinMode(sensorPower, OUTPUT);
digitalWrite(sensorPower, LOW);
scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
}
void loop() {
int weight = calculate_weight();
Serial.print("Weight: ");
Serial.print(weight);
Serial.println(" g");
if (weight >= ini_weight) {
my_servo1.write(90);
}
else if (weight < ini_weight) {
my_servo1.write(pos);
}
int level = calculate_water();
Serial.print("Water level: ");
Serial.print(level);
if (level >= water_level) {
my_servo2.write(90);
}
else if (level < water_level) {
my_servo2.write(pos);
}
}
int calculate_weight() {
if (scale.is_ready()) {
long reading = scale.read() * x;
float grams = (float)reading / 100000.0;
grams = grams - y;
return grams;
} else {
Serial.println("HX711 not found.");
}
delay(1000);
}
int calculate_water() {
digitalWrite(sensorPower, HIGH);
delay(10);
val = analogRead(sensorPin);
digitalWrite(sensorPower, LOW);
return val;
delay(1000);
}
Comments