here is my current code:
#include <HX711_ADC.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
HX711_ADC LoadCell(45, 43);
const int buzzerPin = 41;
const int buttonPin = 39;
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
bool buzzerState = false;
bool buttonPressed = false;
unsigned long buttonPressStartTime = 0;
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
void setup()
{
LoadCell.begin();
LoadCell.start(1000);
LoadCell.setCalFactor(-230); setup
pinMode(53,OUTPUT);
pinMode(buzzerPin, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP);
Serial.begin(115200);
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
delay(2000);
display.clearDisplay();
}
void loop() {
// put your main code here, to run repeatedly:
LoadCell.update(); // retrieves data from the load cell
float i = LoadCell.getData(); // get output value
float displayWeight = max(0, i);
display.clearDisplay();
display.setTextColor(WHITE);
display.setTextSize(3.2);
display.setCursor(15,21);
display.print(displayWeight);
display.display();
if(i>10){
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 >= 2000) && buzzerState) {
digitalWrite(buzzerPin, LOW);
buzzerState = false;
}
} else {
// Button is released
buttonPressed = false;
}
}