#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include "HX711.h"
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
#define DOUT 12
#define CLK 13
HX711 scale;
float calibration_factor = 409; // ปรับค่านี้ตามการสอบเทียบของคุณ
float length; // ค่าความยาวที่คำนวณได้
void setup() {
Serial.begin(115200);
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0, 0);
display.println("Initializing...");
display.display();
scale.begin(DOUT, CLK);
scale.set_scale(calibration_factor);
scale.tare(); // รีเซ็ตค่าเป็นศูนย์
}
void loop() {
float weight = scale.get_units(); // อ่านค่าน้ำหนัก
length = calculateLength(weight); // คำนวณความยาวจากน้ำหนัก
display.clearDisplay();
display.setCursor(0, 0);
display.print("Weight: ");
display.print(weight, 2);
display.println(" kg");
display.setCursor(0, 20);
display.print("Length: ");
display.print(length, 2);
display.println(" m");
display.display();
if (Serial.available()) {
char temp = Serial.read();
if (temp == 't' || temp == 'T') {
scale.tare(); // รีเซ็ตค่าเป็นศูนย์
}
}
delay(500);
}
float calculateLength(float weight) {
// กำหนดสมการหรือความสัมพันธ์ระหว่างน้ำหนักและความยาวที่นี่
// ตัวอย่าง: length = weight * 0.1
return weight * 0.1;
}
LAB XIII-III
Load cell