#include <HX711.h>
#include <TM1637Display.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
#define CALIBRATION_FACTOR 352.83 //this number is taken from HX_kitchen_scale_cal
LiquidCrystal_I2C lcd(0x27, 2, 16);
byte pinDt = 4; // DOUT
byte pinSck = 3; // SCK
HX711 scale;
/* Keypad setup */
const byte KEYPAD_ROWS = 4;
const byte KEYPAD_COLS = 4;
byte rowPins[KEYPAD_ROWS] = {12, 11, 10, 9}; // pin numbers of each input button
byte colPins[KEYPAD_COLS] = {8, 7, 6, 5}; // pin numbers of each input button
char keys[KEYPAD_ROWS][KEYPAD_COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, KEYPAD_ROWS, KEYPAD_COLS);
uint64_t value = 0;
char Tare = 'A'; // clear lcd, reset to 0
char Enter = 'B'; // enter
char Clear = 'C'; // clear keypad input
char Delete = 'D'; // delete last number inputted
int count = 0;
unsigned long savedNumber=0;
TM1637Display display(A0, A1);
void setup()
{
lcd.begin(16, 2);
lcd.init();
lcd.backlight();
lcd.print("ENGINEERING");
lcd.setCursor(0, 1);
lcd.print("GOOD");
delay(2000);
lcd.clear();
Serial.begin(57600);
scale.begin(pinDt, pinSck);
scale.set_scale(CALIBRATION_FACTOR); // this value is obtained by calibrating the scale with known weights;
scale.tare();
display.setBrightness(7);//for 4digit
}
void loop()
{
lcd.setCursor(0, 1);
char key = keypad.getKey();
lcd.print(key);
//Serial.print(scale.get_units(2),0);
if (key >= '0' && key <= '9')
{
lcd.setCursor (count,0);
lcd.print (key);
savedNumber = savedNumber * 10 + (key - '0'); // Convert the pressed key to a number
Serial.print("Entered number: ");
Serial.println(savedNumber);
lcd.setCursor (0,0);
count ++;
}
else if (key == Clear)
{
lcd.clear();
savedNumber = 0;
count = 0;
}
else if (key == Delete)
{
int count1 = 0;
count1=count--;
lcd.setCursor (count1,0);
lcd.clear();
savedNumber = (savedNumber / 10);
Serial.print("Deleted Last Digit. New Number: ");
Serial.println(savedNumber);
lcd.println (savedNumber);
if(count1 <= 0){count = 0;}//to ensure the lcd cursor will not go negative
}
else if (key == Tare)
{
lcd.clear();
savedNumber = 0;
count = 0;
scale.tare();
lcd.print ("Taring Complete");
delay(1000);
lcd.clear();
}
else if (key == Enter)
{
Serial.println("Entering while loop...");
Serial.print (key);
while (true)
{
// Your code inside the while loop goes here
// This code will keep executing until the "C" button is pressed
Serial.print(savedNumber);
lcd.setCursor(0, 1);
lcd.print(scale.get_units(2),0);
lcd.print(" Gram");
lcd.print(" ");
// Check if the "C" button is pressed to exit the loop
if (keypad.getKey() == 'C')
{
Serial.println("Exiting while loop...");
break;
}
}
}
display.showNumberDec(savedNumber-scale.get_units(1));
//delay(2000);
}