#include <LiquidCrystal.h>
#include "HX711.h"
// Define LCD pin configuration
LiquidCrystal lcd(8, 9, 7, 6, 5, 4);
// Define buzzer and LED pins
const int buzzerPin = 3; // Connect to Bz1:1
const int ledPin = 13; // Connect to Digital Pin 13
// Define HX711 Load Cell Amplifier configuration
HX711 scale;
// Threshold to determine when the load is considered present
const long loadThreshold = 100;
void setup() {
lcd.begin(16, 2);
lcd.print("Smart Bed");
delay(2000); // Wait for 2 seconds
pinMode(buzzerPin, OUTPUT);
pinMode(ledPin, OUTPUT);
// Initialize the HX711
scale.begin(2, 3); // Connect DT to D2 and SCK to D3
}
void loop() {
lcd.setCursor(0, 1); // Move cursor to the second line
lcd.print("Reading Load");
// Read the load from the HX711
long load = scale.read();
lcd.setCursor(0, 0); // Move cursor to the first line
lcd.print("Smart Bed "); // Add spaces to clear the previous content
lcd.setCursor(0, 1);
lcd.print("Load: ");
lcd.print(load);
// Check if the load is present
if (load > loadThreshold) {
// Turn on the buzzer and LED simultaneously
tone(buzzerPin, 1000); // Adjust the frequency as needed
digitalWrite(ledPin, HIGH);
// Inner loop to count seconds while load is present
unsigned long startTime = millis();
while (millis() - startTime <= 5000) {
delay(1000); // Wait for 1 second
lcd.setCursor(9, 0); // Move cursor to the first line, 10th position
lcd.print((millis() - startTime) / 1000);
// Read the load from the HX711 during the inner loop
load = scale.read();
lcd.setCursor(6, 1); // Move cursor to the second line, 7th position
lcd.print(" "); // Clear previous load value
lcd.setCursor(6, 1);
lcd.print("Load: ");
lcd.print(load);
// If load is removed, break out of the inner loop
if (load <= loadThreshold) {
noTone(buzzerPin);
digitalWrite(ledPin, LOW);
break;
}
}
}
delay(5000); // Wait for 5 seconds before the next loop
}