#include <Arduino.h>
#include "HX711.h"
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 32

// HX711 circuit wiring
const int LOADCELL_DOUT_PIN = 11;
const int LOADCELL_SCK_PIN = 10;

#define OLED_RESET     -1
#define SCREEN_ADDRESS 0x3C
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

HX711 scale;

void setup() {
  Serial.begin(9600);
  if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
    Serial.println(F("SSD1306 allocation failed"));
    for(;;); // Don't proceed, loop forever
  }
  scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);


  display.display();
  delay(2000);
  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(SSD1306_WHITE);
}

void loop() {

  if (scale.is_ready()) {
    display.clearDisplay();
    display.setCursor(0,0);
    display.display();
    scale.set_scale();    
    display.println("Tare... remove any weights from the scale.");
    display.display();
    delay(1000);
    scale.tare();
    display.println("Tare done...");
    display.print("Place a known weight on the scale...");
    display.display();
    delay(1000);
    display.clearDisplay();
    display.setCursor(0,0);
    display.display();
    long reading = scale.get_units(10);
    display.print("Result: ");
    display.println(reading);
    display.display();
  }
  else {
    display.println("HX711 not found.");
  }
  delay(5000);
}