// Start By The Name Of Allah
// Digital Scale Machine with Unit Change
// By: Imran Sharif
// Technical Minds
//=================================================================
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <HX711_ADC.h>
#include <EEPROM.h>
#if defined(ESP8266)|| defined(ESP32) || defined(AVR)
#endif
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Declaration for SSD1306 display connected using I2C
#define OLED_RESET 4 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
HX711_ADC LoadCell(18, 5); // parameters: dt pin, sck pin
const int calVal_eepromAdress = 0;
unsigned long t = 0;
const int tare_pin = 14; // Tare Switch Pin
const int cal_pin = 13; // Calibration Pin
boolean bUnit_g = true;
//=================================================================
void setup()
{
Serial.begin(57600); delay(10);
Serial.println();
Serial.println("Starting...");
pinMode(tare_pin, INPUT_PULLUP);
// initialize the OLED object
if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;)
; // Don't proceed, loop forever
}
display.display(); // Used To Clear The Display.
delay(2000); // Used To Set A 1 Second Delay.
display.clearDisplay(); // Used To Clear The Display (Clear The Buffer).
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(0, 8);
display.println("ENGSOLEX");
display.display();
delay(1000);
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(0,24);
display.println("Digital ");
display.display();
delay(1000);
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(0,40);
display.println("Scale 50KG");
display.display();
delay(2000);
display.clearDisplay();
LoadCell.begin();
//LoadCell.setReverseOutput(); //uncomment to turn a negative output value to positive
unsigned long stabilizingtime = 2000; // preciscion right after power-up can be improved by adding a few seconds of stabilizing time
boolean _tare = true; //set this to false if you don't want tare to be performed in the next step
LoadCell.start(stabilizingtime, _tare);
if (LoadCell.getTareTimeoutFlag() || LoadCell.getSignalTimeoutFlag())
{
Serial.println("Timeout, check MCU>HX711 wiring and pin designations");
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0, 28);
display.println("Check Load Cell");
display.display();
delay(2000);
display.clearDisplay();
while (1);
}
else
{
LoadCell.setCalFactor(1.0); // 420.0 user set calibration value (float), initial value 1.0 may be used for this sketch
Serial.println("Startup is complete");
}
}
//=================================================================
void loop()
{
(LoadCell.update()); // check for new data/start next conversion:
float i = LoadCell.getData();
Serial.print("Load_cell output val: ");
Serial.println(i);
float s = i / 1000;
display.clearDisplay(); // Clear the buffer.
display.setTextSize(3); // Display Text
display.setTextColor(WHITE);
display.setCursor(0, 24);
display.print(s);
display.println("KG");
display.display();
if (digitalRead(tare_pin) == LOW)
{
LoadCell.tareNoDelay(); //tare
display.clearDisplay(); // Clear the buffer.
display.setTextSize(1); // Display Text
display.setTextColor(WHITE);
display.setCursor(0, 28);
display.println("Taring.....");
display.display();
LoadCell.start(1000);
}
// receive command from serial terminal
if (Serial.available() > 0) {
char inByte = Serial.read();
if (inByte == 't') LoadCell.tareNoDelay(); //tare
else if (inByte == 'c') calibrate(); //calibrate
else if (inByte == 'e') changeSavedCalFactor(); //edit calibration value manually
}
}
void calibrate()
{
Serial.println("***");
Serial.println("Start calibration:");
Serial.println("Place the load cell an a level stable surface.");
Serial.println("Remove any load applied to the load cell.");
Serial.println("Send 't' from serial monitor to set the tare offset.");
boolean _resume = false;
while (_resume == false) {
LoadCell.update();
if (Serial.available() > 0) {
if (Serial.available() > 0) {
char inByte = Serial.read();
if (inByte == 't') LoadCell.tareNoDelay();
}
}
if (LoadCell.getTareStatus() == true) {
Serial.println("Tare complete");
_resume = true;
}
}
Serial.println("Now, place your known mass on the loadcell.");
Serial.println("Then send the weight of this mass (i.e. 100.0) from serial monitor.");
float known_mass = 0;
_resume = false;
while (_resume == false) {
LoadCell.update();
if (Serial.available() > 0) {
known_mass = Serial.parseFloat();
if (known_mass != 0) {
Serial.print("Known mass is: ");
Serial.println(known_mass);
_resume = true;
}
}
}
LoadCell.refreshDataSet(); //refresh the dataset to be sure that the known mass is measured correct
float newCalibrationValue = LoadCell.getNewCalibration(known_mass); //get the new calibration value
Serial.print("New calibration value has been set to: ");
Serial.print(newCalibrationValue);
Serial.println(", use this as calibration value (calFactor) in your project sketch.");
Serial.print("Save this value to EEPROM adress ");
Serial.print(calVal_eepromAdress);
Serial.println("? y/n");
_resume = false;
while (_resume == false) {
if (Serial.available() > 0) {
char inByte = Serial.read();
if (inByte == 'y') {
#if defined(ESP8266)|| defined(ESP32)
EEPROM.begin(512);
#endif
EEPROM.put(calVal_eepromAdress, newCalibrationValue);
#if defined(ESP8266)|| defined(ESP32)
EEPROM.commit();
#endif
EEPROM.get(calVal_eepromAdress, newCalibrationValue);
Serial.print("Value ");
Serial.print(newCalibrationValue);
Serial.print(" saved to EEPROM address: ");
Serial.println(calVal_eepromAdress);
_resume = true;
}
else if (inByte == 'n') {
Serial.println("Value not saved to EEPROM");
_resume = true;
}
}
}
Serial.println("End calibration");
Serial.println("***");
Serial.println("To re-calibrate, send 'r' from serial monitor.");
Serial.println("For manual edit of the calibration value, send 'c' from serial monitor.");
Serial.println("***");
}
void changeSavedCalFactor() {
float oldCalibrationValue = LoadCell.getCalFactor();
boolean _resume = false;
Serial.println("***");
Serial.print("Current value is: ");
Serial.println(oldCalibrationValue);
Serial.println("Now, send the new value from serial monitor, i.e. 696.0");
float newCalibrationValue;
while (_resume == false) {
if (Serial.available() > 0) {
newCalibrationValue = Serial.parseFloat();
if (newCalibrationValue != 0) {
Serial.print("New calibration value is: ");
Serial.println(newCalibrationValue);
LoadCell.setCalFactor(newCalibrationValue);
_resume = true;
}
}
}
_resume = false;
Serial.print("Save this value to EEPROM adress ");
Serial.print(calVal_eepromAdress);
Serial.println("? y/n");
while (_resume == false) {
if (Serial.available() > 0) {
char inByte = Serial.read();
if (inByte == 'y') {
#if defined(ESP8266)|| defined(ESP32)
EEPROM.begin(512);
#endif
EEPROM.put(calVal_eepromAdress, newCalibrationValue);
#if defined(ESP8266)|| defined(ESP32)
EEPROM.commit();
#endif
EEPROM.get(calVal_eepromAdress, newCalibrationValue);
Serial.print("Value ");
Serial.print(newCalibrationValue);
Serial.print(" saved to EEPROM address: ");
Serial.println(calVal_eepromAdress);
_resume = true;
}
else if (inByte == 'n') {
Serial.println("Value not saved to EEPROM");
_resume = true;
}
}
}
Serial.println("End change calibration value");
Serial.println("***");
}