#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#include <HX711.h>
#include <RTClib.h>
#include <SPI.h>
// Define pins for the components
#define HX711_DOUT 2
#define HX711_SCK 3
#define ILI9341_CS 53
#define ILI9341_DC 50
#define ILI9341_RST 49
// Define button pins
#define BUTTON_START 8
#define BUTTON_RESET 12
#define BUTTON_SAVE 9
// Define instances for the components
HX711 scale;
Adafruit_ILI9341 tft = Adafruit_ILI9341(ILI9341_CS, ILI9341_DC, ILI9341_RST);
RTC_DS1307 rtc;
// State variables
bool isMeasuring = false;
void setup() {
// Initialize serial communication
Serial.begin(9600);
// Initialize ILI9341 screen
tft.begin();
tft.setRotation(1);
tft.fillScreen(ILI9341_BLACK);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(2);
tft.setCursor(10, 10);
tft.println("Digital Scale");
delay(2000);
// Initialize HX711
scale.begin(HX711_DOUT, HX711_SCK);
scale.set_scale(425); // Set calibration factor
scale.tare(); // Reset the scale
// Initialize RTC
if (!rtc.begin()) {
tft.setTextColor(ILI9341_RED);
tft.setCursor(10, 40);
tft.println("RTC not found!");
while (1);
}
// Check if RTC is running
if (!rtc.isrunning()) {
tft.setCursor(10, 70);
tft.println("RTC is NOT running!");
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
// Initialize buttons
pinMode(BUTTON_START, INPUT_PULLUP);
pinMode(BUTTON_RESET, INPUT_PULLUP);
pinMode(BUTTON_SAVE, INPUT_PULLUP);
}
void loop() {
// Check if start button is pressed
if (digitalRead(BUTTON_START) == LOW) {
isMeasuring = true;
tft.setCursor(10, 160);
tft.setTextColor(ILI9341_GREEN);
tft.println("Measurement started!");
delay(1000);
}
if (isMeasuring) {
// Get weight data from HX711
float weight = scale.get_units();
if (weight < 0) {
weight = 0;
}
// Display weight on screen
tft.fillScreen(ILI9341_BLACK);
tft.setCursor(10, 10);
tft.println("Weight:");
tft.setTextSize(3);
tft.setCursor(10, 50);
tft.setTextColor(ILI9341_GREEN);
tft.print(weight, 2);
tft.println(" kg");
// Display RTC time
DateTime now = rtc.now();
tft.setTextSize(2);
tft.setTextColor(ILI9341_WHITE);
tft.setCursor(10, 100);
tft.print("Time: ");
tft.print(now.hour(), DEC);
tft.print(":");
tft.print(now.minute(), DEC);
tft.print(":");
tft.print(now.second(), DEC);
tft.setCursor(10, 130);
tft.print("Date: ");
tft.print(now.day(), DEC);
tft.print("/");
tft.print(now.month(), DEC);
tft.print("/");
tft.print(now.year(), DEC);
}
// Check for reset button press
if (digitalRead(BUTTON_RESET) == LOW) {
tft.setCursor(10, 160);
tft.setTextColor(ILI9341_RED);
tft.println("Resetting scale...");
scale.tare();
isMeasuring = false;
delay(2000);
}
// Check for save button press
if (digitalRead(BUTTON_SAVE) == LOW) {
tft.setCursor(10, 190);
tft.setTextColor(ILI9341_BLUE);
tft.println("Saving data...");
float weight = scale.get_units();
DateTime now = rtc.now();
Serial.print("Weight: ");
Serial.print(weight, 2);
Serial.print(" kg, Time: ");
Serial.print(now.hour(), DEC);
Serial.print(":");
Serial.print(now.minute(), DEC);
Serial.print(":");
Serial.print(now.second(), DEC);
Serial.print(", Date: ");
Serial.print(now.day(), DEC);
Serial.print("/");
Serial.print(now.month(), DEC);
Serial.print("/");
Serial.println(now.year(), DEC);
delay(2000);
}
delay(500);
}