#include <Arduino.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
#include <HX711.h>
#define NUM_KEYS 4
char button_pressed[NUM_KEYS];
int k = 1;
#define buzzer 2
#define DT 3
#define SCK 4
HX711 scale;
float scale_calibration = 0;
float weight_units;
float weight_gr;
float delta;
LiquidCrystal_I2C lcd(0x27, 20, 4);
const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] =
{
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = {14, 15, 16, 17};
byte colPins[COLS] = {18, 19, 20, 21};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
void setup()
{
Serial.begin(9600);
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Enter weight:");
lcd.setCursor(0, 2);
lcd.print("Weight:");
scale.begin(DT, SCK);
scale.set_scale();
scale.tare();
}
void loop()
{
char key = keypad.getKey();
if (key != NO_KEY)
{
button_pressed [k] = key;
Serial.print(key);
lcd.setCursor(0, 1);
lcd.print(key);
scale.set_scale(scale_calibration);
}
Serial.print("Read data: ");
weight_units = scale.get_units(), 10;
if (weight_units < 0){
weight_units = 0.00;
}
weight_gr = weight_units * 0.035274;
Serial.print(weight_gr);
Serial.println(" grams");
lcd.setCursor(0, 3);
lcd.print(weight_gr);
delta = key - weight_gr;
if(delta + 1 > 0)
{
tone(2, 1000, 3000);
}
else if(delta - 1 < 0)
{
tone(2, 500, 3000);
}
else
{
noTone(2);
}
}