Skip to content

Liquid Filling Machine – Liquid Filler Part 2

Here we will present the updated code of liquid filler

// 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;
}

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

  }

}

As you can see at pin 11 is located our relay and at Analog 0 our load cell sensor.

Notice that WeightTared = Weight - Tare.

We have added the int GetCurrentWeight() function in order to get the average analog reading.
At this pin the reading will be an integer from 0 to 1023.

This procedure works like this :
Reads from analog pin 0 using : analogRead(LoadCellAnalogPin);
Then delays 10ms .
This delay is needed in order to allow the voltage at the ADC to stabilize and the second
analogRead should get a stable value and not the first one made before.

Now after the 10ms delay we read from the analog0 pin 60 times.
We are summing all the 60 readings in AnalogValueTotal.

Notice that the worst case for AnalogValueTotal is 60*1023=61380
which value is covered with an unsigned int .
A simple int can not cover this value.

At this point we are doing the average like this :
AnalogValueAverange = (AnalogValueTotal)/max;
This division gives an integer since AnalogValueTotal and max are integers.
Notice that in arduino in an integer division, any fractional part is truncated.

The function returns AnalogValueAverange which should be an integer value from 0 to 1023.
In the third liquid filler tutorial we will see how from this AnalogValueAverange
we will get grams.

We are going to use SciDAVis so for now we leave it as is.

1 thought on “Liquid Filling Machine – Liquid Filler Part 2

  1. Pingback: How To Connect Load Cell To Arduino? | Click & Find Answer !

Comments are closed.