#include <Wire.h>
#include <Keypad.h>
#include <LiquidCrystal_I2C.h>
#include <HX711.h>
//LCD FUNCTION
LiquidCrystal_I2C lcd(0x27,16,2);
//KEYPAD FUNCTION
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
//define the cymbols on the buttons of the keypads
char hexaKeys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'.','0','#','D'}
};
byte rowPins[ROWS] = {9, 8, 7, 6}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {5, 4, 3, 2}; //connect to the column pinouts of the keypad
//initialize an instance of class NewKeypad
Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
//LOAD CELL FUNCTION
const int LOADCELL_DOUT_PIN = 11;
const int LOADCELL_SCK_PIN = 12;
HX711 scale;
//GLOBAL VARIABLE
static String Action = "nothing";
static float CalibrationFactor;
static String keypadinput = "";
static float TargetWeight = 0.00;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
lcd.init();
lcd.backlight();
lcd.setCursor(0,0);
lcd.println("AUTOMATIC WEIGHT");
lcd.setCursor(0,1);
lcd.println(" DISPENSER");
delay(2000);
lcd.clear();
scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
scale.set_scale(CalibrationFactor);
scale.tare();
}
void calibrate(){
if (Action == "nothing")
{
Action = "calibrate_1";
keypadinput = "";
lcd.clear();
lcd.setCursor(0,0);
lcd.println("==CALIBRATION==");
delay(3000);
lcd.clear();
lcd.setCursor(0,0);
lcd.println("Remove weight");
lcd.setCursor(0,1);
lcd.println("then press Enter");
return;
}
if (Action == "calibrate_1")
{
if (scale.is_ready())
{
lcd.clear();
lcd.setCursor(0,0);
lcd.println("Calculating");
lcd.setCursor(0,1);
lcd.println("Please wait..");
delay(2000);
scale.tare();
scale.set_scale(0);
CalibrationFactor = 0;
CalibrationFactor = scale.get_units(10);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Weight:0");
lcd.setCursor(0,1);
lcd.print("CF:");
lcd.print(String(CalibrationFactor));
delay(2000);
lcd.clear();
lcd.setCursor(0,0);
lcd.println("Place weight");
lcd.setCursor(0,1);
lcd.println("then press Enter");
Action = "calibrate_2";
}
else
{
lcd.clear();
lcd.setCursor(0,0);
lcd.println("Scale not found");
lcd.setCursor(0,1);
lcd.println("Calibrate failed");
Action == "false";
delay(3000);
}
return;
}
if (Action == "calibrate_2")
{
if (scale.is_ready())
{
lcd.clear();
lcd.setCursor(0,0);
lcd.println("Weight (gr):");
lcd.setCursor(0,1);
Action = "calibrate_3";
}
else
{
lcd.clear();
lcd.setCursor(0,0);
lcd.println("Scale not found");
lcd.setCursor(0,1);
lcd.println("Calibrate failed");
Action == "nothing";
delay(3000);
}
return;
}
if(Action == "calibrate_3")
{
lcd.clear();
CalibrationFactor = CalibrationFactor + scale.get_units(10);
CalibrationFactor = CalibrationFactor / keypadinput.toFloat();
scale.set_scale(CalibrationFactor);
lcd.setCursor(0,0);
lcd.print("Weight:");
lcd.print(keypadinput);
lcd.setCursor(0,1);
lcd.print("CF:");
lcd.print(String(CalibrationFactor));
delay(2000);
lcd.clear();
lcd.setCursor(0,0);
lcd.println("Calibration");
lcd.setCursor(0,1);
lcd.println("Success!");
Action = "nothing";
delay(2000);
lcd.clear();
return;
}
}
void scale_func(){
if(Action == "nothing"){
float weight = scale.get_units(10);
lcd.setCursor(0,0);
lcd.print("Weight:");
lcd.print(weight,1);
lcd.println("gr");
lcd.setCursor(0,1);
lcd.print("Set to:");
delay(100);
}
}
void setTargetWeight(){
if(Action == "nothing"){
keypadinput = "";
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Set Weight (gr):");
lcd.setCursor(0,1);
Action = "set_target";
return;
}
if(Action == "set_target"){
TargetWeight = keypadinput.toFloat();
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Done");
lcd.setCursor(0,1);
lcd.print("Set to:");
lcd.print(TargetWeight,1);
lcd.print("gr");
Action = "nothing";
delay(1000);
return;
}
}
void loop() {
// put your main code here, to run repeatedly:
char customKey = customKeypad.getKey();
if(customKey != NO_KEY)
{
if (isdigit(customKey)){
lcd.print(String(customKey));
keypadinput = keypadinput + String(customKey);
}
if(customKey == 'A'){
Action = "nothing";
calibrate();
}
if(customKey == 'B'){
Action = "nothing";
scale.tare();
lcd.clear();
lcd.setCursor(0,0);
lcd.println("Weight:0.0gr");
}
if(customKey == 'C'){
setTargetWeight();
}
if(customKey == 'D'){
if(Action == "calibrate_1" or Action == "calibrate_2" or Action == "calibrate_3"){
calibrate();
}
if(Action == "set_target"){
setTargetWeight();
}
}
if(customKey == '.'){
}
if(customKey == '#'){
}
} else
{
scale_func();
}
}