Hey guys, I am trying to do a different kinda a digital scale. The things that I have are:
-load cell (10kg)
-HX711
-buzzer
-led
-LCD screen
-button
-Arduino mega
You see what I want is when I put a weight bigger than 100gr I want my led and my buzzer to work. Moreover, while the buzzer is still working I am gonna press the button for 5 seconds, this way the buzzer will stop working BUT the led will keep working. ( If the weight is smaller than 100gr led and the buzzer wont work )
The problems I have:
-
-when I put a weight bigger than 100 my led and buzzer work fine. But I can't seem to find a way to work my button.
-
I do have issues with the calibration, the LCD screen do prints negative numbers. I am pretty sure it's not giving me the correct outcomes. BUT my priority right now is to get the button to work.
And if you have any ideas for me to improve my digital scale please let me know
Here is my code
#include <HX711_ADC.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
HX711_ADC LoadCell(45, 43);
LiquidCrystal_I2C lcd(0x27, 16,2);
const int buzzerPin = 41;
const int buttonPin = 39;
bool buzzerState = false; // Track the state of the buzzer (on/off)
bool buttonPressed = false;
unsigned long buttonPressStartTime = 0;
void setup()
{
LoadCell.begin();
LoadCell.start(2000); // load cells gets 2000ms of time to stabilize
LoadCell.setCalFactor(1000.0); // calibration factor for load cell
lcd.begin();
lcd.backlight();
pinMode(53,OUTPUT);
pinMode(buzzerPin, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP);
}
void loop() {
LoadCell.update();
float i = LoadCell.getData();
lcd.setCursor(0, 0);
lcd.print("Weight[g]:");
lcd.setCursor(0, 1);
lcd.print(i);
if(i>100){
digitalWrite(53,HIGH);
digitalWrite(41,HIGH);
buzzerState = true;
} else {
// Turn off LED and buzzer
digitalWrite(53, LOW);
digitalWrite(buzzerPin, LOW);
buzzerState = false;
}
// Button functionality
if (digitalRead(buttonPin) == LOW) {
// Button is pressed
if (!buttonPressed) {
buttonPressed = true;
buttonPressStartTime = millis();
}
// If the button has been pressed for 5 seconds, turn off the buzzer
if (buttonPressed && (millis() - buttonPressStartTime >= 5000) && buzzerState) {
digitalWrite(buzzerPin, LOW);
buzzerState = false;
}
} else {
// Button is released
buttonPressed = false;
}
}