#include "HX711.h"
#include <SD.h>

const int DT_pin = 2;
const int SCK_pin = 3;
const int loadCellDoutPin = 12;
const int chipSelect = 10;

HX711 scale;

File dataFile;

void setup() {
  Serial.begin(9600);
  scale.begin(DT_pin, SCK_pin);
  scale.set_scale(0.42); // Calibration factor for load cell

  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    return;
  }

  Serial.println("Card initialized.");
}

void loop() {
  if (scale.is_ready()) {
    long weight = scale.get_units(10); // Get weight (change 10 to adjust average)
    Serial.print("Weight: ");
    Serial.print(weight);
    Serial.println(" g");
    
    dataFile = SD.open("data.txt", FILE_WRITE);
    if (dataFile) {
      dataFile.print("Weight: ");
      dataFile.print(weight);
      dataFile.println(" g");
      dataFile.close();
    } else {
      Serial.println("Error opening data.txt");
    }

    delay(1000); // Adjust delay as needed
  }
}