#include <stdio.h>
#include <HX711.h>
#include <LiquidCrystal.h>
/* Button Pin Connection */
const int BUTTON_1 = 34; // Button Red
const int BUTTON_2 = 35; // Button Blue
/* LCD Pin Connection */
const int LCD_PIN_RS = 32;
const int LCD_PIN_EN = 33;
const int LCD_PIN_D4 = 25;
const int LCD_PIN_D5 = 27;
const int LCD_PIN_D6 = 23;
const int LCD_PIN_D7 = 22;
/* HX711 Pin Connections */
const int LOADCELL_PIN_DT = 19;
const int LOADCELL_PIN_SCK = 18;
/* Initialize the library by associating any needed LCD interface */
LiquidCrystal lcd(LCD_PIN_RS, LCD_PIN_EN, LCD_PIN_D4, LCD_PIN_D5, LCD_PIN_D6, LCD_PIN_D7);
/* Defeinitions */
HX711 scale;
float known_weight = 100.0;
float raw_weight = 0.0;
float calibration_value = 0.f;
float last_weight;
float last_weight_change = 5001.0;
char buffer[17];
void setup() {
Serial.begin(115200);
/* Set buttons as input */
pinMode(BUTTON_1, INPUT);
pinMode(BUTTON_2, INPUT);
/* Set up the LCD's number of columns and rows */
lcd.begin(16, 2);
Serial.begin(115200);
/* Display project name. */
lcd.print("LOAD CELL");
lcd.setCursor(0,1);
lcd.print("CALIBRATION");
while(digitalRead(BUTTON_1) == LOW); // if push BUTTON_1, end of the loop.
delay(300);
/* Clear second line. */
lcd.setCursor(0,1);
lcd.print(" ");
lcd.setCursor(0,0);
lcd.print("LOAD CELL C.");
scale.begin(LOADCELL_PIN_DT, LOADCELL_PIN_SCK);
/* This value is obtained by calibrating the scale with known weights. */
scale.set_scale(); // ??? why it is empty
/* Reset the scale to 0. */
lcd.setCursor(0,1);
lcd.print("Tare... ");
while(digitalRead(BUTTON_1) == LOW); /* if push BUTTON_1, end of the loop. */
delay(300);
scale.tare();
/* Clear second line. */
lcd.setCursor(0,1);
lcd.print(" ");
/* Print known_weight first value */
lcd.setCursor(0,1);
sprintf(buffer, "Weight: %.1f", known_weight);
lcd.print(buffer);
while(digitalRead(BUTTON_1) == LOW){ /* if push BUTTON_1, end of the loop. */
if(digitalRead(BUTTON_2) == HIGH){
/* Increase known_weight 100gr. */
known_weight += 100.0;
/* Max value 5000.0 gr. */
if(known_weight >= 5000.0){
known_weight = 100.0;
}
/* Print known_weight value */
lcd.setCursor(0,1);
lcd.print(" ");
lcd.setCursor(0,1);
sprintf(buffer, "Weight: %.1f", known_weight);
lcd.print(buffer);
delay(300);
}
}
delay(300);
/* Place known weight to cell. */
lcd.setCursor(0,1);
sprintf(buffer, "Place %.1f gr", known_weight);
lcd.print(buffer);
while(digitalRead(BUTTON_1) == LOW); /* if push BUTTON_1, end of the loop. */
delay(300);
/* Find calibration_value */
calibration_value = scale.get_units(10) / known_weight;
lcd.setCursor(0,1);
sprintf(buffer, "Calib. V: %.5f", calibration_value);
lcd.print(buffer);
/* This value is obtained by calibrating the scale with known weights */
scale.set_scale(calibration_value);
while(digitalRead(BUTTON_1) == LOW); /* if push BUTTON_1, end of the loop. */
delay(300);
/* reset the scale to 0 */
lcd.setCursor(0,1);
lcd.print(" ");
lcd.setCursor(0,1);
lcd.print("Tare...");
while(digitalRead(BUTTON_1) == LOW); /* if push BUTTON_1, end of the loop. */
delay(300);
scale.tare();
}
void loop() {
last_weight = scale.get_units(1);
if(last_weight != last_weight_change){
lcd.setCursor(0,1);
lcd.print(" ");
lcd.setCursor(0,1);
sprintf(buffer, "Weight: %.1fgr", last_weight);
lcd.print(buffer);
last_weight_change = last_weight;
}
}