#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <HX711.h>
#include <Servo.h>
LiquidCrystal_I2C lcd(0x3F, 16, 2); // Address may vary; use an I2C scanner to find the correct address.
HX711 scale;
Servo barrierServo;
const int pirStartPin = 2; // PIR motion sensor for starting
const int pirStopPin = 3; // PIR motion sensor for stopping
const int servoPin = 9; // Servo control pin
const int ledPin = 13; // Built-in LED for status indication
const int LOADCELL_DOUT_PIN = 11; // Pin for HX711 data output
const int LOADCELL_SCK_PIN = 12; // Pin for HX711 serial clock
const long minimumWeight = 1000; // Minimum weight in grams (1kg)
const long maximumWeight = 5000; // Maximum weight in grams (5kg)
bool barrierOpen = false;
void setup() {
pinMode(pirStartPin, INPUT);
pinMode(pirStopPin, INPUT);
pinMode(ledPin, OUTPUT);
lcd.init();
lcd.backlight(); // Keep the backlight on initially
// Add contrast adjustment instructions
Serial.println("Adjust the LCD contrast manually using the potentiometer on the LCD module.");
Serial.println("Turn the potentiometer until the text is clearly visible.");
scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
scale.set_scale(); // Set this to your own calibration factor.
barrierServo.attach(servoPin);
closeBarrier();
Serial.begin(9600);
}
void loop() {
bool carDetected = false;
if (digitalRead(pirStartPin) == HIGH) {
carDetected = true;
openBarrier();
}
if (digitalRead(pirStopPin) == HIGH) {
carDetected = false;
closeBarrier();
}
// Read the potentiometer value to control LCD backlight
int potValue = analogRead(A0); // Read the potentiometer value
int backlightValue = map(potValue, 0, 1023, 0, 255); // Map it to LCD backlight range (0-255)
lcd.setBacklight(backlightValue); // Set the LCD backlight based on potentiometer value
if (carDetected) {
long weight = getWeight();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Weight: ");
lcd.print(weight);
lcd.print("g");
if (weight > maximumWeight) {
lcd.setCursor(0, 2);
lcd.print("Turn back or");
lcd.setCursor(0, 3);
lcd.print("Reduce weight");
closeBarrier();
}
} else {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Weightbridge");
}
}
void openBarrier() {
barrierServo.write(0); // Open the barrier
barrierOpen = true;
digitalWrite(ledPin, HIGH);
}
void closeBarrier() {
barrierServo.write(90); // Close the barrier
barrierOpen = false;
digitalWrite(ledPin, LOW);
}
long getWeight() {
return scale.get_units(10); // Read the weight from the load cell
}