#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include "HX711.h" // Include the HX711 library
// HX711 circuit wiring
const int dataPin = 26;
const int clockPin = 18;
HX711 scale;
LiquidCrystal_I2C lcd(0x27, 16, 2); // Set the LCD address to 0x27 for a 16 chars and 2 line display
int ButtonTaree = 25;
int LedPin = 32; // Define the pin for the LED
float gramvalue;
float calibration_factor = 420; // This calibration factor is adjusted according to my load cell
float units;
void setup()
{
scale.begin(dataPin, clockPin);
Serial.begin(115200);
lcd.begin(16, 2);
lcd.init();
// Print a message to the LCD.
lcd.backlight();
scale.set_scale(calibration_factor); // Adjust to this calibration factor
scale.tare();
pinMode(ButtonTaree, INPUT_PULLUP);
pinMode(LedPin, OUTPUT); // Set the LED pin as an output
digitalWrite(LedPin, LOW); // Ensure the LED is off initially
xTaskCreatePinnedToCore(Task1, "Task1", 4000, NULL, 1, NULL, 1);
xTaskCreatePinnedToCore(Task2, "Task2", 4000, NULL, 1, NULL, 1);
xTaskCreatePinnedToCore(Task3, "Task3", 4000, NULL, 1, NULL, 1); // Create Task3 for LED control
}
void loop()
{
// Empty loop as tasks handle the functionality
}
void Task1(void* pt)
{
while (1)
{
long Timestart = millis();
units = scale.get_units();
// Round units to two decimal places
units = round(units * 100.0) / 100.0;
if (digitalRead(ButtonTaree) == LOW)
{
lcd.setCursor(0, 1); // Set cursor to the second row
lcd.print(" Taring... ");
scale.tare();
lcd.clear();
}
long task1_time = millis()-Timestart;
Serial.print("Execute_time1 =");
Serial.println(task1_time);
delay(500); // Adjust delay to allow for task scheduling
}
}
void Task2(void* pt)
{
while (1)
{
long Timestart2 = millis();
lcd.setCursor(0, 0);
lcd.print("Weight: ");
lcd.setCursor(8, 0);
lcd.print(units, 2); // Displays the weight in 2 decimal places
lcd.print("kg");
long task2_time = millis()-Timestart2;
Serial.print("Execute_time2 =");
Serial.println(task2_time);
delay(1000); // Update the display every 1000 milliseconds
}
}
void Task3(void* pt)
{
while (1)
{
long Timestart3 = millis();
// Check if weight is greater than 3kg and turn on the LED if it is
if (units > 3.0)
{
digitalWrite(LedPin, HIGH); // Turn on the LED
lcd.setCursor(0, 1); // Set cursor to the second row
lcd.print("Overload Weight!");
}
else
{
digitalWrite(LedPin, LOW); // Turn off the LED
lcd.clear();
}
long task3_time = millis()-Timestart3;
Serial.print("Execute_time3 =");
Serial.println(task3_time);
delay(500); // Adjust delay to allow for task scheduling
}
}