Skip to content

Liquid Filling Machine – Liquid Filler Part 1

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

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

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

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

  //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;
      WriteToLCD(">Stopped","");
    } else if (StartStopBtnCounter == 1) {
      IsActionBtnActive = false;
      IsSetBtnActive = false;
      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
          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;

}

As you may notice the LCD is connected at arduino pins 2,3,4,5,6,7 .

The three menu buttons are connected at pins 8,9,10.

The first button (pin 8) is the START/STOP button. This button will open and close the gravity feed valve.

The second button (pin9) is the MENU (action) button. It loops between the various options that liquid filler offers.

The third button is the SET button.  Sets/confirms the selected MENU voice.

The void WriteToLCD(String line1, String line2) function help us to print the various options on our LCD.

String StampWeightLimit() prints the weight limit on the screen

void ModifyWeightLimit(int increment) modifies the weight limit of the machine

String StampTare() prints the tare (WeightTared = Weight - Tare)

void SaveToEEPROM(unsigned int LimitInGr) saves the weightlimit

void ReadFromEEPROM() reads and restores the saved weightlimit

The menu is similar to the circular menu presented at the simple circular menu article.