I don’t know what’s going on but my lcd doesn’t show text but only rectangular boxes. I can change this and make it disappear.
#include <LiquidCrystal.h> // LCD Library
#include "HX711.h" // Load cell library
#define DOUT 5 // Arduino Pin connection data
#define CLK 4 // Arduino Pin connection data
#define SW2 2 // Right switch (unit swap)
#define SW1 3 // Left switch (tare)
HX711 loadCell; // Creates the Scale Object
long int rawValue, tareValue; // Long variables
double calibrationFactor = 0.002230622; // [grams/ADC value]
const int rs = 8, en = 6, d4 = 7, d5 = 11, d6 = 12, d7 = 13; // Pins for the LCD
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
const int MAX_UNITS = 4; // Update this value as you add more units
int selectedUnit = 1; // Default unit (Grams)
const double MAX_WEIGHT = 5000.0; // Load cell max capacity relative to grams
void setup() {
pinMode(SW1, INPUT_PULLUP);
pinMode(SW2, INPUT_PULLUP);
lcd.begin(16, 2);
lcd.setCursor(3, 0);
lcd.print("Warming up");
delay(5000);
lcd.clear();
lcd.print("...Almost there");
delay(5000);
Serial.begin(9600);
loadCell.begin(DOUT, CLK, 128);
lcd.clear();
lcd.setCursor(2, 0);
lcd.print("Please clear");
lcd.setCursor(3, 1);
lcd.print("load tray");
delay(5000);
lcd.clear();
lcd.setCursor(3, 0);
lcd.print("Commencing");
lcd.setCursor(2, 1);
lcd.print("TARE function");
tareValue = loadCell.read_average(150);
lcd.clear();
lcd.setCursor(1, 0);
lcd.print("TARE function");
lcd.setCursor(3, 1);
lcd.print("complete");
delay(2000);
lcd.clear();
lcd.setCursor(4, 0);
lcd.print("All set!");
}
void loop() {
static unsigned long lastSW2Press = 0;
static unsigned long lastSW1Press = 0;
const unsigned long debounceDelay = 100;
bool sw2State = digitalRead(SW2);
bool sw1State = digitalRead(SW1);
if (sw2State == LOW && millis() - lastSW2Press > debounceDelay) {
selectedUnit = (selectedUnit % MAX_UNITS) + 1;
lastSW2Press = millis();
}
if (sw1State == LOW && millis() - lastSW1Press > debounceDelay) {
lcd.clear();
lcd.setCursor(3, 0);
lcd.print("Commencing");
lcd.setCursor(2, 1);
lcd.print("TARE function");
tareValue = loadCell.read_average(150);
lcd.clear();
lcd.setCursor(1, 0);
lcd.print("TARE function");
lcd.setCursor(3, 1);
lcd.print("complete");
delay(2000);
lcd.clear();
lastSW1Press = millis();
}
rawValue = loadCell.read_average(40) - tareValue;
if (abs(rawValue) < 150) {
rawValue = 0;
}
double Grams = rawValue * calibrationFactor;
double Kilograms = Grams / 1000.0;
double Pounds = Grams * 0.00220462;
double Ounces = Grams * 0.035274;
char buffer[16]; // Buffer for formatted text
lcd.setCursor(0, 1);
lcd.print("TARE");
lcd.setCursor(11, 1);
lcd.print("units");
lcd.setCursor(0, 0);
if (Grams > MAX_WEIGHT) {
lcd.print("Max weight!");
} else {
switch (selectedUnit) {
case 1:
snprintf(buffer, 16, "%.2f Grams", Grams);
break;
case 2:
snprintf(buffer, 16, "%.2f Kg", Kilograms);
break;
case 3:
snprintf(buffer, 16, "%.2f lbs", Pounds);
break;
case 4:
snprintf(buffer, 16, "%.2f Oz", Ounces);
break;
}
lcd.print(buffer);
}
}
```
Here is my code and my wiring.
