#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <HX711.h>
#define VBAT_PIN 3
#define DOUT_PIN 4
#define SCK_PIN 5
#define SDA_PIN 6
#define SCL_PIN 7
// Calibration factor will be the (Reading)/(known weight)
#define CALIBRATION_FACTOR 420
HX711 scale;
Adafruit_SSD1306 oled(128, 64, &Wire, -1);
void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  pinMode(VBAT_PIN, INPUT);
  Wire.begin(SDA_PIN, SCL_PIN);
  // initialize OLED display with I2C address 0x3C
  if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    Serial.println(F("failed to start SSD1306 OLED"));
    while (1);
  }
  
  oled.clearDisplay();
  oled.setTextSize(4);
  oled.setTextColor(WHITE);
  Serial.println("Initializing the scale");
  scale.begin(DOUT_PIN, SCK_PIN);
  scale.set_scale(CALIBRATION_FACTOR);   
  //scale.tare();
}
void loop() {
  delay(1000);
  float vbat = analogRead(VBAT_PIN) / 4095.0 * 3.3 * 2;
  Serial.println("Battery Voltage : " + String(vbat) + " V");
  
  oled.clearDisplay();
  Serial.print("\nReading :");
  Serial.println(scale.read()); 
  Serial.print("Weight :");
  float weight = scale.get_units();
  Serial.print(weight);
  Serial.println("kg");
  oled.setCursor(15, 15);
  oled.println(weight);
  oled.display();
}