#include "HX711.
h"
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 10, 9, 8, 7);
#define DOUT 3
#define CLK 2
float weight=0;
const int cal=6;
const int up=5;
const int down=4;
const int x100=1;
int readingup,readingdown,readingcal,readingx100,cf=0,a[2]={100,1000},i=0;
float calibration_factor =-204100; //this value is calculated according to
instructions in report
HX711 scale(DOUT, CLK); //using the Hx711 in 128 gain mode by default
void setup()
{
[Link](16,2);
pinMode(cal,INPUT);
pinMode(up,INPUT);
pinMode(down,INPUT);
pinMode(x100,INPUT);
scale.set_scale(); // initially calibrating the scale with zero value
[Link](); //resets the scale to 0
long zero_factor = scale.read_average(10); //stores the average of 10 readings
from the ADC when there is no weight on pan
}
void loop()
{
readingcal=digitalRead(cal);
if(readingcal==HIGH) //this section gets activated when CAL button is ON for
calibration
{
if(cf==0)//in this section we will give some instructions for calibration once
{
[Link]();
[Link](2,0);
[Link]("Calibration ");
[Link](4,1);
[Link]("Starts! ");
delay(3000);
[Link]();
[Link](0,0);
[Link]("Place 1kg weight");
[Link](0,1);
[Link]("or known weight.");
delay(4000);
[Link]();
[Link](0,0);
[Link]("Press '+' when ");
[Link](0,1);
[Link]("weight is small.");
delay(2500);
[Link]();
[Link](0,0);
[Link]("Press '-' when ");
[Link](0,1);
[Link]("weight is big. ");
delay(2500);
cf=1; // this variable helps us to read the instructions once and for the
rest of time calibration() function is called
}
calibration();
}
else //otherwise to measure the weights
{
display2();
cf=0; //this variable helps us to read the instructions ONCE when the CAL
button is switched ON
}
}
void calibration() //this is the general function of Calibration
{
readingup=digitalRead(up); //reads the logic at pin 5 to increase the calibration
factor
readingdown=digitalRead(down); //reads the logic at pin 4 to decrease the
calibration factor
readingx100=digitalRead(x100); //reads the logic at pin 1 to inc/dec the
calibration factor by 100 times
if(readingx100==HIGH)
{
i=1; //i is the index of array used at top that contains only two values 10 and
100,
//it provides the factor by which we are inc/dec the calibration factor
}
else {i=0;} //if x100 is off then by default we increase or decrease the
calibration factor by 10 times
if(readingup==HIGH)
{
calibration_factor+=a[i];//increasing the calibration factor by 10/100 times
delay(80);
}
if(readingdown==HIGH)
{
calibration_factor-=a[i];//decreasing the calibration factor by 10/100 times
delay(80);
}
display1();//as we used two different displays for two modes. NOrmal mode and
Calibration mode..
//this is for calibration mode
}
void display1()//calibration mode display
{
scale.set_scale(calibration_factor); //the value in calibration factor is
obtained by calibration it with known weights
weight=scale.get_units(); //stores the average of weight readings from the ADC
according to the calibration factor
if(weight<0)
{weight=0;}
[Link](0,0);
[Link]("Weight: ");
[Link](8,0);
[Link](weight,4); //displays the weight in 4 decimal places only for
calibration
[Link](14,0);
[Link]("kg");
[Link](0,1);
[Link]("Value: "); //display the calibration factor so that it is easy to
inc/dec it and reading it at the same time
[Link](7,1);
[Link](calibration_factor);
}
void display2()//display of normal mode
{
scale.set_scale(calibration_factor);
weight=scale.get_units();
if(weight<0)
{weight=0;}//if the weight is negative then show it zero
[Link](0,0);
[Link]("**Weight Scale:*");
[Link](0,1);
[Link]("Weight: ");
[Link](8,1);
[Link](weight,2);//displaying the weight in two decimal places. As 0.01
accuracy is required
[Link](12,1);
[Link]("kg ");
}