//Start By The Name Of Allaha
//This Code is written for Weighing scale by Technical minds
#include <HX711_ADC.h>
#include <LiquidCrystal_I2C.h>
#if defined(ESP8266)|| defined(ESP32) || defined(AVR)
#include <EEPROM.h>
#endif
//pins:
const int HX711_dout = 4; //mcu > HX711 dout pin
const int HX711_sck = 5; //mcu > HX711 sck pin
const int tpin = 3; // the pin that the tare pushbutton is attached to
const int Up_buttonPin = 9; // the pin that the up pushbutton is attached to
const int Down_buttonPin = 8; // the pin that the down pushbutton is attached to
float buttonPushCounter = 0; // counter for the number of button presses
float up_buttonState = 0; // current state of the up button
float up_lastButtonState = 0; // previous state of the up button
float down_buttonState = 0; // current state of the up button
float down_lastButtonState = 0; // previous state of the up button
bool bPress = false;
//HX711 constructor:
HX711_ADC LoadCell(HX711_dout, HX711_sck);
//LCD_12C:
LiquidCrystal_I2C lcd(0x27, 2, 16);
//EEPROM:
const int calVal_eepromAdress = 0;
unsigned long t = 0;
// Setup:
void setup() {
Serial.begin(57600); delay(10);
//Serial.println();
//Serial.println("Starting...");
pinMode(tpin, INPUT_PULLUP);
pinMode( Up_buttonPin , INPUT_PULLUP);
pinMode( Down_buttonPin , INPUT_PULLUP);
pinMode(6, OUTPUT);
pinMode(12, OUTPUT);
lcd.init();
lcd.backlight();
lcd.setCursor(2, 0);
lcd.print("MERIT BREAD");
lcd.setCursor(1, 1);
lcd.print("WEIGHING SCALE");
delay(2000);
lcd.clear();
//Start weighing:
LoadCell.begin();
//LoadCell.setReverseOutput(); //uncomment to turn a negative output value to positive
unsigned long stabilizingtime = 2000; // preciscion right after power-up can be improved by adding a few seconds of stabilizing time
boolean _tare = true; //set this to false if you don't want tare to be performed in the next step
LoadCell.start(stabilizingtime, _tare);
if (LoadCell.getTareTimeoutFlag() || LoadCell.getSignalTimeoutFlag()) {
//Serial.println("Timeout, check MCU>HX711 wiring and pin designations");
while (1);
}
else {
LoadCell.setCalFactor(420.0); // user set calibration value (float), initial value 1.0 may be used for this sketch
//Serial.println("Startup is complete");
}
while (!LoadCell.update());
}
void loop() {
static boolean newDataReady = 0;
const int serialPrintInterval = 0; //increase value to slow down serial print activity
// check for new data/start next conversion:
if (LoadCell.update()) newDataReady = true;
// get smoothed value from the dataset:
if (newDataReady) {
if (millis() > t + serialPrintInterval) {
float i = LoadCell.getData();
//Serial.print("Load_cell output val: ");
//Serial.println(i);
newDataReady = 0;
t = millis();
lcd.setCursor(0, 0);
lcd.print("SET WEI:");
lcd.setCursor(9, 0);
lcd.print(buttonPushCounter);
lcd.setCursor(14, 0);
lcd.print("KG");
lcd.setCursor(0, 1);
lcd.print("WEIGHT :");
lcd.setCursor(9, 1);
lcd.print(i);
lcd.setCursor(14, 1);
lcd.print("KG");
}
}
checkUp();
checkDown();
if (digitalRead(tpin) == LOW) {
LoadCell.tareNoDelay();
}
// check if last tare operation is complete:
if (LoadCell.getTareStatus() == true) {
lcd.clear();
lcd.print("Tare complete");
delay(1000);
lcd.clear();
}
float i = LoadCell.getData();
float k = buttonPushCounter - i;
if ( k < 0.05 && k > 0.01 )
{
digitalWrite (6, HIGH);
delay(100);
digitalWrite (6, LOW);
delay(200);
}
if ( k >= 0.05 )
{
digitalWrite (6, HIGH);
delay(200);
digitalWrite (6, LOW);
delay(200);
}
if (i >= buttonPushCounter)
{
digitalWrite (6, LOW);
digitalWrite (12, HIGH);
}
else
{
digitalWrite(12, LOW);
}
}
void checkUp()
{
up_buttonState = digitalRead(Up_buttonPin);
// compare the buttonState to its previous state
if (up_buttonState != up_lastButtonState)
{
// if the state has changed, increment the counter
if (up_buttonState == LOW)
{
bPress = true;
// if the current state is HIGH then the button went from off to on:
buttonPushCounter = buttonPushCounter + 0.10;
}
}
// save the current state as the last state, for next time through the loop
up_lastButtonState = up_buttonState;
}
void checkDown()
{
down_buttonState = digitalRead(Down_buttonPin);
// compare the buttonState to its previous state
if (down_buttonState != down_lastButtonState)
{
// if the state has changed, increment the counter
if (down_buttonState == LOW)
{
bPress = true;
buttonPushCounter = buttonPushCounter - 0.10;
}
}
// save the current state as the last state, for next time through the loop
down_lastButtonState = down_buttonState;
}