#include <Wire.h> // 如果使用的是I2C接口的压力传感器
// 假设传感器连接到A0和A1
const int sensorPin1 = A0;
const int sensorPin2 = A1;
// 假定传感器的位置
const float x1 = 0.0;
const float y1 = 0.0;
const float x2 = 10.0; // 假设传感器间隔10cm
const float y2 = 0.0;
void setup() {
Serial.begin(9600);
}
void loop() {
// 读取传感器值
int sensorValue1 = analogRead(sensorPin1);
int sensorValue2 = analogRead(sensorPin2);
// 转换为电压值,Arduino的A0-A5口的参考电压为5V
float voltage1 = sensorValue1 / 1023.0 * 5.0;
float voltage2 = sensorValue2 / 1023.0 * 5.0;
// 假设电压与压力成正比,压力与质量也成正比
// 这里简化处理,不考虑重力和面积
float mass1 = voltage1;
float mass2 = voltage2;
// 计算重心
float cx = (mass1 * x1 + mass2 * x2) / (mass1 + mass2);
float cy = (mass1 * y1 + mass2 * y2) / (mass1 + mass2);
// 输出结果
Serial.print("Center of Mass: ");
Serial.print("X: ");
Serial.print(cx);
Serial.print(", Y: ");
Serial.println(cy);
// 等待一段时间再读取,防止过于频繁
delay(1000);
}