Automatic draft tap (Problem with order of actions)

I’m building an automated kegerator 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 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 to 700 grams, the second servo closes the tap.
I used IF statements to do all the actions. But when I put my glass, the action of tilting the glass and opening the tap are made at the same time. I would like to tilt the glass first and open the tap a couple of seconds later. When I tried to put a delay on the IF statement for before the command to open the tap but it also delays my readings for the weight. I don’t know if is the way I wrote the code than i can't put a delay before the servo starts moving. Maybe there's a better way to do it. 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:""
 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

 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
HX711 scale;

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

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

  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


  //Setup Servo Tilt
  //Setup Servo Tap


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

  //Condition to Open the tap
  if (scale.get_units() >= 0.375 && scale.get_units() < 0.700) {
     delay (200);
     else {
  //condition 1
  if (scale.get_units() >= 0&& scale.get_units() < 0.375) {

   //condition 2
  if (scale.get_units() >= 0.375 && scale.get_units() < 0.485) {

  //condition 3
  if (scale.get_units() >= 0.485 && scale.get_units() < 0.519) {
  //condition 4
  if (scale.get_units() >= 0.519 && scale.get_units() < 0.619) {

  //condition 4
  if (scale.get_units() >= 0.619 && scale.get_units() < 1.00) {


consider this structure

    if (0.700 < scale.get_units())
    else if (0.619 < scale.get_units())
    else if (0.519 < scale.get_units())
    else if (0.485 < scale.get_units())
    else if (0.375 < scale.get_units())

Yes, also consider getting the units just once per pass, store it in some variable for testing, e.g.:

float units = scale.get_units();

if (0.700 <  units)
    else if (0.619 < units)

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.