Skip to content

Liquid Filling Machine – Liquid Filler Part 4

The code at this point is complete.
Just modify the GetCurrentWeight() using the calculated from SciDAVis conversion formula and modify the IsAutofilling case
WeightTared = (Weight-Tare)-4; where 4 was the difference between normal and autofill modes.
The complete sketch of liquid filler is located below :

// include the library http://arduino.cc/en/Reference/LiquidCrystal
#include <LiquidCrystal.h>
#include <EEPROM.h>

const int StartStopBtnPin = 8; // the pin that the Start Stop Button is attached to
const int ActionBtnPin = 9;    // the pin that the Action Button is attached to
const int SetBtnPin = 10;      // the pin that the Set Button is attached to
const int RelayPin = 11;
const int LoadCellAnalogPin = 0; //The analog pin for the loadcell

int StartStopBtnCounter = 0;   // counter for the number of button presses
int StartStopBtnState = 0;     // current state of the button
int StartStopBtnLastState = 0; // previous state of the button

int ActionBtnCounter = 0;
int ActionBtnState = 0;
int ActionBtnLastState = 0;
boolean IsActionBtnActive = true;

int SetBtnCounter = 0;
int SetBtnState = 0;
int SetBtnLastState = 0;
boolean IsSetBtnActive = true;

int Weight = 0;
int Tare = 0;
int WeightTared = 0; //WeightTared = Weight - Tare
int WeightLimit = 1000;
boolean IsWeighting = false;
boolean IsAutofilling = false;

String TempString = "";

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);

void WriteToLCD(String line1, String line2){
  if (line1 != ""){
    lcd.clear();
    lcd.setCursor(0, 0);//col,row
    lcd.print(line1);
    if (line2 != ""){
      lcd.setCursor(0, 1);//col,row
      lcd.print(line2);
    }
  } else { // empty first line
    lcd.setCursor(0, 1);//col,row
    lcd.print("                ");
    lcd.setCursor(0, 1);//col,row
    lcd.print(line2);
  }
}

String StampWeightLimit() {
  TempString="Cur.limit ";
  TempString=TempString + WeightLimit;
  return TempString+"gr";
}

void ModifyWeightLimit(int increment){
  int TempWeight;
  TempWeight=WeightLimit;
  TempWeight=TempWeight+increment;
  if (TempWeight<0 || TempWeight>9990){
    lcd.noDisplay();
    delay(500);
    lcd.display();
  } else {
    WeightLimit=TempWeight;
  }
}

String StampTare() {
  TempString="Cur. tare ";
  TempString=TempString + Tare;
  return TempString+"gr";
}

int GetCurrentWeight() {

  const int max = 60;
  unsigned int AnalogValueTotal = 0;
  unsigned int AnalogValueAverange = 0;

  analogRead(LoadCellAnalogPin); //The first analogRead will switch the pin to the ADC.
  delay(10); //The delay will allow the voltage at the ADC to stabilize and the second analogRead should get a stable value.

  for (int i=0; i < max; i++){
    AnalogValueTotal = AnalogValueTotal + analogRead(LoadCellAnalogPin);
    delay(1);
  }

  AnalogValueAverange = (AnalogValueTotal)/max; //The division is conducted using the data type of the operands, so 9(int) / 4(int) gives 2(int)
                                                //In integer division, any fractional part is truncated.

  //return AnalogValueAverange;
  return ( (AnalogValueAverange*1.2134) + 133.9982 );

}

void SaveToEEPROM(unsigned int LimitInGr){

  unsigned int wl;
  wl=LimitInGr>>8; //8 zeros at beggining

  unsigned int wr;
  wr=LimitInGr<<8; //8 zeros at end
  wr=wr>>8;

  EEPROM.write(0,wl);
  EEPROM.write(1,wr);

  TempString="SAVED ";
  TempString=TempString + LimitInGr;
  TempString=TempString+"gr";
  WriteToLCD("",TempString);
}

void ReadFromEEPROM(){
  unsigned int wl;
  wl=EEPROM.read(0);
  wl=wl<<8;
  unsigned int wr;
  wr=EEPROM.read(1);

  unsigned int w;
  w=wl+wr;

  if (w==0 || w>9999) {
    WeightLimit=1000;
  } else {
    WeightLimit=w;
  }

}

void setup() {

  // initialize the StartStopBtnPin pin as a input:
  pinMode(StartStopBtnPin, INPUT);
  pinMode(ActionBtnPin, INPUT);
  pinMode(SetBtnPin, INPUT);
  pinMode(RelayPin, OUTPUT);

  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);

  // Print a message to the LCD.
  //lcd.setCursor(0, 0);//col,row
  WriteToLCD(">Stopped","Made By Hw2Sw");

  //Important
  analogReference(INTERNAL);

  //Read the eeprom and find the latest weight limit
  ReadFromEEPROM();

}

void loop() {

  // read the start stop input pin:
  StartStopBtnState = digitalRead(StartStopBtnPin);
  ActionBtnState = digitalRead(ActionBtnPin);
  SetBtnState = digitalRead(SetBtnPin);

  //////////////////////////////////////////////////
  //////////////////////////////////////////////////
  //THE START STOP BUTTON
  if (StartStopBtnState != StartStopBtnLastState) {

    if (StartStopBtnState == HIGH) {
      StartStopBtnCounter++;
    } 

    IsWeighting=false;
    IsAutofilling=false;

    if (StartStopBtnCounter == 0) {
      IsActionBtnActive = true;
      IsSetBtnActive = true;
      digitalWrite(RelayPin, LOW);
      WriteToLCD(">Stopped","");
    } else if (StartStopBtnCounter == 1) {
      IsActionBtnActive = false;
      IsSetBtnActive = false;
      digitalWrite(RelayPin, HIGH);
      WriteToLCD(">STARTED","");
    } else {
      StartStopBtnCounter=0;
    } 

  }

  StartStopBtnLastState = StartStopBtnState;

  //////////////////////////////////////////////////
  //////////////////////////////////////////////////
  //THE ACTION BUTTON
  if (ActionBtnState != ActionBtnLastState) {

    if (IsActionBtnActive==true) {

      if (ActionBtnState == HIGH) {
        ActionBtnCounter++;
      } 

      IsWeighting=false;
      IsAutofilling=false;

      if (ActionBtnCounter == 0) {
        WriteToLCD(">Stopped","");
      } else if (ActionBtnCounter == 1) {
        TempString=StampWeightLimit();
        WriteToLCD(">AUTOFILL",TempString);
      } else if (ActionBtnCounter == 2) {
        //Get Weight from the sensor
        WriteToLCD(">Weight","");
        IsWeighting=true;
      } else if (ActionBtnCounter == 3) {
        //Get Weight from the sensor
        WriteToLCD(">Tare","");
      } else if (ActionBtnCounter == 4) {
        //Get Weight from the sensor
        WriteToLCD(">Untare","");
      } else if (ActionBtnCounter == 5) {
        TempString=StampWeightLimit();
        WriteToLCD(">Change -10gr",TempString);
      } else if (ActionBtnCounter == 6) {
        TempString=StampWeightLimit();
        WriteToLCD(">Change -100gr",TempString);
      } else if (ActionBtnCounter == 7) {
        TempString=StampWeightLimit();
        WriteToLCD(">Change -1000gr",TempString);
      } else if (ActionBtnCounter == 8 ) {
        TempString=StampWeightLimit();
        WriteToLCD(">Change +10gr",TempString);
      } else if (ActionBtnCounter == 9) {
        TempString=StampWeightLimit();
        WriteToLCD(">Change +100gr",TempString);
      } else if (ActionBtnCounter == 10) {
        TempString=StampWeightLimit();
        WriteToLCD(">Change +1000gr",TempString);
      } else if (ActionBtnCounter == 11) {
        TempString=StampWeightLimit();
        WriteToLCD(">Save cur. lim.",TempString);
      } else if (ActionBtnCounter == 12) {
        TempString=StampWeightLimit();
        WriteToLCD(">RESET limit",TempString);
      } else {
        ActionBtnCounter=0;
      } 

    } else { //IsActionBtnActive==false
      WriteToLCD("","Not allowed");
      ActionBtnCounter=0;
    }

  }

  ActionBtnLastState = ActionBtnState;

  //////////////////////////////////////////////////
  //////////////////////////////////////////////////
  //THE SET BUTTON
  if (SetBtnState != SetBtnLastState) {

    if (IsSetBtnActive==true) {

      if (SetBtnState == HIGH) {
        SetBtnCounter++;
      }

      if (SetBtnCounter == 0) {
        if (ActionBtnCounter == 0) { //stopped
          WriteToLCD("","Not allowed");
        } else if (ActionBtnCounter == 1) { //autofill
          IsAutofilling=not IsAutofilling;
        } else if (ActionBtnCounter == 2) { //get weight
          //Nothing to do...
        } else if (ActionBtnCounter == 3) { //tare
          Tare = GetCurrentWeight();
          TempString=StampTare();
          WriteToLCD("",TempString);
        } else if (ActionBtnCounter == 4) { //untare
          Tare=0;
          TempString=StampTare();
          WriteToLCD("",TempString);
        } else if (ActionBtnCounter == 5) { //-10gr
          ModifyWeightLimit(-10);
          TempString=StampWeightLimit();
          WriteToLCD("",TempString);
        } else if (ActionBtnCounter == 6) { //-100gr
          ModifyWeightLimit(-100);
          TempString=StampWeightLimit();
          WriteToLCD("",TempString);
        } else if (ActionBtnCounter == 7) { //-1000gr
          ModifyWeightLimit(-1000);
          TempString=StampWeightLimit();
          WriteToLCD("",TempString);
        } else if (ActionBtnCounter == 8 ) { //+10gr
          ModifyWeightLimit(10);
          TempString=StampWeightLimit();
          WriteToLCD("",TempString);
        } else if (ActionBtnCounter == 9) { //+100gr
          ModifyWeightLimit(100);
          TempString=StampWeightLimit();
          WriteToLCD("",TempString);
        } else if (ActionBtnCounter == 10) { //+1000gr
          ModifyWeightLimit(1000);
          TempString=StampWeightLimit();
          WriteToLCD("",TempString);
        } else if (ActionBtnCounter == 11) { //Save weight limit to the eeprom
          SaveToEEPROM(WeightLimit);
        } else if (ActionBtnCounter == 12) { //Save weight limit to the eeprom
          SaveToEEPROM(1000);
        } 

      } else {
        SetBtnCounter=0;
      }       

    } else { //IsSetBtnActive==false
      WriteToLCD("","Not allowed");
      SetBtnCounter=0;
    }

  }

  SetBtnLastState = SetBtnState;

  if (IsAutofilling == true){ //Autofill

    Weight = GetCurrentWeight();
    WeightTared = (Weight-Tare)-4; //-4 the voltage regulator erogates more voltage when the relay is open so gives 60gr more (constant error)
    TempString = WeightTared;
    TempString=TempString + "/";
    TempString=TempString + WeightLimit;
    TempString=TempString + "gr";
    WriteToLCD("",TempString);    

    if (WeightTared<WeightLimit){
      digitalWrite(RelayPin, HIGH);
    } else {
      digitalWrite(RelayPin, LOW);
      IsAutofilling = false;
    }  

  } else if (IsWeighting == true){ //Show Weight and tared weight

    Weight = GetCurrentWeight();
    WeightTared = Weight - Tare;
    TempString = Weight;
    TempString = TempString + "/tared";
    TempString = TempString + WeightTared;
    TempString = TempString + "gr";
    WriteToLCD("",TempString);

  }

}

With the help of the 28pin socket of our liquid filler we are able to remove the ATMEGA chip.
We place the chip on the arduino board and we connect arduino into our computer.
Now we load the sketch using arduino IDE. Press compile and then upload.
Now with the formula updated we can put again the chip on our liquid filler board.

Liquid filler complete sketch can be downloaded from here : LiquidFillerPart4

It is now showtime ! We are ready to test our home made cheap liquid filler.

Below you can see live demonstration of the final result.

1Kg load cell weight test

1Kg load cell testing auto filling (at 500gr & 700 gr)

20Kg load cell testing auto filling (at 930gr & 1480gr)

20Kg load cell weight and auto filling test at 1480gr

 

Thats all for now !