50kg Load Cells with Hx711 ( fluctuations when removing the weight) Automated beer Tap

Hi, I’m building an automated beer tap. I’m using a 4 load cell, an hx711 amplifier and two servos. Basically, when I put my glass the sensor reads the weight and the first servo tilts the glass 35 degrees and the second servo reads the 35 degree angle and opens the tap. The sensor keeps reading while the glass is getting full, tilting the glass at 25, 15 and 5 degrees, depending on the weight. Once the weight gets between 719 grams and 1 kilo, the tilting servo turns to 5 degrees and the second servo detects this angle and closes the tap.

My problem is that sometimes when I take out the glass, it looks that detects some kind of change in the weight and the servos moves, tilting the device and opening for a second and spilling the beer.

I have the feeling that a longer delay could help with the problem. But I don’t know where I should put it. Or maybe there’s a way to ignore the weight readings when i'm taking the glass. I appreciate if someone can point me in the right direction.

This is my code.

/*
 Example using the SparkFun HX711 breakout board with a scale
 By: Nathan Seidle
 SparkFun Electronics
 Date: November 19th, 2014
 License: This code is public domain but you buy me a beer if you use this and we meet someday (Beerware license).

 This example demonstrates basic scale output. See the calibration sketch to get the calibration_factor for your
 specific load cell setup.

 This example code uses bogde's excellent library:"https://github.com/bogde/HX711"
 bogde's library is released under a GNU GENERAL PUBLIC LICENSE

 The HX711 does one thing well: read load cells. The breakout board is compatible with any wheat-stone bridge
 based load cell which should allow a user to measure everything from a few grams to tens of tons.
 Arduino pin 2 -> HX711 CLK
 3 -> DAT
 5V -> VCC
 GND -> GND

 The HX711 board can be powered from 2.7V to 5V so the Arduino 5V power should be fine.

*/
//Scale Prep
#include "HX711.h"
#define calibration_factor -26040 //This value is obtained using the SparkFun_HX711_Calibration sketch
#define LOADCELL_DOUT_PIN  2
#define LOADCELL_SCK_PIN  3
HX711 scale;

//Servo Tilt Prep
#include <Servo.h>
Servo ServoTilt;
Servo ServoTap;

int ServoTiltPos;

//Oled Prep

#include <Wire.h>
#include <Adafruit_GFX.h>   
#include <Adafruit_SSD1306.h> 

#define ANCHO 128     
#define ALTO 64       
#define OLED_RESET 4      
Adafruit_SSD1306 oled(ANCHO, ALTO, &Wire, OLED_RESET);  


void setup() {
  
  //Setup Scale
  Serial.begin(9600);
  Serial.println("HX711 scale demo");

  scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
  scale.set_scale(calibration_factor); //This value is obtained by using the SparkFun_HX711_Calibration sketch
  scale.tare(); //Assuming there is no weight on the scale at start up, reset the scale to 0

  Serial.println("Readings:");

  //Setup Servo Tilt
  ServoTilt.attach(9);
  ServoTilt.write(0); //Tilt Servo in initial position
  
  //Setup Servo Tap

  ServoTap.attach(5);
  ServoTap.write(120);// Tap Servo in close position

  //Setup Oled
  Wire.begin ();
  oled.begin (SSD1306_SWITCHCAPVCC, 0x3C);
}

void loop() {
  Serial.print("Reading: ");
  Serial.print(scale.get_units(), 3); //scale.get_units() returns a float
  Serial.print(" Kg"); //You can change this to kg but you'll need to refactor the calibration_factor
  Serial.println();

  oled.clearDisplay();      
  oled.setTextColor(WHITE);   
  oled.setTextSize(2);
  oled.setCursor(10, 16);     
  oled.print("Reading: ");
  oled.print(scale.get_units(), 3);
  oled.print(" Kg");
  oled.display();      

   //Condition Open tap 
  
     ServoTiltPos=ServoTilt.read();
     
  if (ServoTiltPos <= 35 && ServoTiltPos > 5 ) {
     ServoTiltPos=ServoTilt.read();
     delay (300);
     ServoTap.write(0); // Tap Servo opens position
     }
     else {
      delay (50);
     ServoTap.write(120);//Tap Servo Close position
     
     }
   
  //Condition Glass Stay at 0
  if (scale.get_units() >= -0.1 && scale.get_units() < 0.375) {
     ServoTilt.write(0);
     }

   //Condition Glass go to 35
  if (scale.get_units() >= 0.375 && scale.get_units() < 0.585) {
     ServoTilt.write(35);
     }

  //Condition Glass go to 25
  if (scale.get_units() >= 0.585 && scale.get_units() < 0.619) {
     ServoTilt.write(25);
     }
     
  //Condition Glass go to 15
  if (scale.get_units() >= 0.619 && scale.get_units() < 0.719) {
     ServoTilt.write(15);
     }

  //Condition Glass go to 5
  if (scale.get_units() >= 0.719 && scale.get_units() < 1.00) {
     ServoTilt.write(5);
     }

  delay(50);
}

This is a link of the prototype.

And this is the link of the prototype malfunctioning.

Prototype malfunction - YouTube

Think about refactoring your code into a state machine (or finite state machine) so that each change you move to a different state. Upon finishing up the final pour state, you close the tap and then move into a "wait" state that simply waits for a certain amount of time and then transactions back to the "ready" state to start the entire process again.

That way, you can always ready the scale but you can ignore it based on what state you are in.