Automatic draft tap (Problem with order of actions)

Hi,
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:"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;

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


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);
  
  //Setup Servo Tap

  ServoTap.attach(5);
  ServoTap.write(180);
}

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();

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

   //condition 2
  if (scale.get_units() >= 0.375 && scale.get_units() < 0.485) {
     ServoTilt.write(35);
     }

  //condition 3
  if (scale.get_units() >= 0.485 && scale.get_units() < 0.519) {
     ServoTilt.write(25);
     }
     
  //condition 4
  if (scale.get_units() >= 0.519 && scale.get_units() < 0.619) {
     ServoTilt.write(15);
     }

  //condition 4
  if (scale.get_units() >= 0.619 && scale.get_units() < 1.00) {
     ServoTilt.write(5);
     }

  delay(200);
}

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())
        ...
    else 
        ...

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.