#include <ESP32Servo.h>
#include "HX711.h"
const int buttonPin = 15;
const int servoPin = 18;
const int dout = 21;
const int sck = 22;
const float weightThreshold = 25.0;
const unsigned long fourHours = 4 * 60 * 60 * 1000; // 4 hours
const unsigned long twelveHours = 12 * 60 * 60 * 1000; // 12 hours
Servo myServo;
HX711 scale;
bool processActive = false;
int cycleCount = 0;
void setup() {
Serial.begin(9600);
pinMode(buttonPin, INPUT_PULLUP);
myServo.attach(servoPin);
myServo.write(0);
scale.begin(dout, sck);
scale.set_scale();
scale.tare();
Serial.println("System initialized. Press the button to start.");
}
void loop() {
if (digitalRead(buttonPin) == LOW && !processActive) {
processActive = true;
cycleCount = 0;
startProcess();
}
}
void startProcess() {
for (cycleCount = 0; cycleCount < 3; cycleCount++) {
Serial.print("Cycle ");
Serial.println(cycleCount + 1);
while (true) {
float weight = scale.get_units();
Serial.print("Current weight: ");
Serial.println(weight);
if (weight < weightThreshold) {
Serial.println("Weight is below 25g, opening the servo to 90 degrees.");
myServo.write(90);
}
if (weight >= weightThreshold) {
Serial.println("Weight is 25g or more, closing the servo to 0 degrees.");
myServo.write(0);
break;
}
delay(1000);
}
if (cycleCount < 2) {
Serial.println("4-hour delay before the next cycle.");
delay(fourHours);
} else {
Serial.println("12-hour delay before the next cycle.");
delay(twelveHours);
}
}
Serial.println("Process completed.");
processActive = false;
}