#include "HX711.h"
#include <LiquidCrystal.h>
HX711 scale;
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // LCD connections
const int printButtonPin = 7; // Print button pin
const int backButtonPin = 8; // Back button pin
const int endButtonPin = 9; // End button pin
volatile bool printRequested = false; // Flag to indicate a print request
volatile bool backRequested = false; // Flag to indicate a back request
volatile bool endRequested = false; // Flag to indicate an end request
float calibrationFactor = 234.0; // Initialize calibration factor (you can change this)
void setup() {
Serial.begin(9600);
scale.begin(6, 7);
lcd.begin(16, 2);
lcd.clear();
pinMode(printButtonPin, INPUT_PULLUP); // Print button pin as input with internal pull-up resistor
pinMode(backButtonPin, INPUT_PULLUP); // Back button pin as input with internal pull-up resistor
pinMode(endButtonPin, INPUT_PULLUP); // End button pin as input with internal pull-up resistor
attachInterrupt(digitalPinToInterrupt(printButtonPin), printButtonPressed, FALLING);
attachInterrupt(digitalPinToInterrupt(backButtonPin), backButtonPressed, FALLING);
attachInterrupt(digitalPinToInterrupt(endButtonPin), endButtonPressed, FALLING);
Serial.println("Enter calibration factor (default 1.0): ");
while (!Serial.available()) {
// Wait for input from the serial monitor
}
calibrationFactor = Serial.parseFloat(); // Read the calibration factor from the serial monitor
}
int gradeCount[6] = {0}; // Initialize an array to count items for each grade
void loop() {
float weight = scale.get_units(10) * calibrationFactor; // Apply the calibration factor
int grade;
if (weight >= 350) {
grade = 1;
} else if (weight >= 300) {
grade = 2;
} else if (weight >= 250) {
grade = 3;
} else if (weight >= 200) {
grade = 4;
} else if (weight >= 150) {
grade = 5;
} else {
grade = 6;
}
if (printRequested) {
printRequested = false;
printSlip(gradeCount);
}
if (backRequested) {
backRequested = false;
if (gradeCount[grade - 1] > 0) {
gradeCount[grade - 1]--;
}
}
if (endRequested) {
endRequested = false;
for (int i = 0; i < 6; i++) {
gradeCount[i] = 0;
}
}
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Grade Counts:");
lcd.setCursor(0, 1);
for (int i = 0; i < 6; i++) {
lcd.print("Grade ");
lcd.print(i + 1);
lcd.print("=");
lcd.print(gradeCount[i]);
lcd.print(" ");
}
Serial.print("Weight: ");
Serial.print(weight);
Serial.print(" kg, Grade: ");
Serial.println(grade);
delay(1000);
}
void printButtonPressed() {
printRequested = true;
}
void backButtonPressed() {
backRequested = true;
}
void endButtonPressed() {
endRequested = true;
}
void printSlip(int gradeCount[6]) {
Serial.println("Printing Grade Counts:");
for (int i = 0; i < 6; i++) {
Serial.print("Grade ");
Serial.print(i + 1);
Serial.print(": ");
Serial.println(gradeCount[i]);
}
}
Loading
epaper-2in9
epaper-2in9