#include <HX711.h> //This library can be obtained here http://librarymanager/All#Avia_HX711
#include <Wire.h>
//#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
//OLED
#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);
#define calibration_factor 375 //This value is obtained using the SparkFun_HX711_Calibration sketch for 44lbs
//46050.0
#define LOADCELL_DOUT_PIN 33
#define LOADCELL_SCK_PIN 32
const int buttonPin = 26;
int buttonState = 0;
//float loadcellread ;
float loadcellread, max_load;
HX711 scale;
void setup() {
Serial.begin(9600);
pinMode(10, OUTPUT);
pinMode(buttonPin, INPUT);
// Serial.println("HX711 scale demo");
scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
scale.set_scale(calibration_factor); //This value is obtained by using the SparkFun_HX711_Calibration sketch
//Assuming there is no weight on the scale at start up, reset the scale to 0
// Serial.println("Readings:");
Wire.begin();
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
max_load = -999999.0; // set initial max load value to a very small value
}
void displayloadcell() {
buttonState = digitalRead(buttonPin);
loadcellread = scale.get_units() ;
//Print the weight value to the serial monitor
// Serial.print("Weight: ");
//Serial.println(loadcellread); //0.453592
// Serial.println(" kg");
if (loadcellread > max_load) {
max_load = loadcellread;
}
/*
display.clearDisplay();
display.setTextColor(WHITE);
display.setTextSize(1);
display.setCursor(0, 0);
display.print(" PULL FORCE");
display.setCursor(10, 9);
display.setTextSize(3);
display.print(loadcellread);
display.setTextSize(1.7);
display.print (" kg");
*/
// OLED Display
display.clearDisplay();
display.setTextColor(WHITE);
display.setTextSize(2);
display.setCursor(0, 0);
display.print("W: ");
display.print(loadcellread, 2);
Serial.print("W: ");
Serial.println(loadcellread, 2);
display.setTextSize(1.2);
display.setCursor(0, 30);
display.print("Max: ");
display.print(max_load, 2);
Serial.print("Max: ");
Serial.print(max_load, 2);
}
void reset (){
if (buttonState == LOW) {
scale.tare();
Serial.print("button_pressed");
}
}
void loop() {
if (buttonState == 0){
display.clearDisplay();
display.setTextSize(2);
display.setCursor(0, 0);
display.print("Reseting");
reset();
}
displayloadcell();
display.display();
delay(50);
}