您好我正在使用带有arduino uno的力传感器进行项目。我遇到的问题是当我在传感器上施加力时它会显示值但是当我停止施加力时它仍然显示相同的值而不是0.当我用不同的力施加它时,值会相应地改变但它仍然当我停止施力时不会为0。请帮忙!
int iForceSensorReading; // the analog reading from the FSR resistor divider
int iForceSensorReading1;
int iForceSensorReading2;
int iForceSensorReading3;
int iForceSensorReading4;
int iForceSensorVoltage; // the analog reading converted to voltage
int iForceSensorVoltage1;
int iForceSensorVoltage2;
int iForceSensorVoltage3;
int iForceSensorVoltage4;
unsigned long ulForceSensorResistance;// The voltage converted to resistance, can be very big so make "long"
unsigned long ulForceSensorResistance1;
unsigned long ulForceSensorResistance2;
unsigned long ulForceSensorResistance3;
unsigned long ulForceSensorResistance4;
unsigned long ulForceSensorConductance;
unsigned long ulForceSensorConductance1;
unsigned long ulForceSensorConductance2;
unsigned long ulForceSensorConductance3;
unsigned long ulForceSensorConductance4;
float FsrForce = 0; // Resistance converted to force
float FsrForce1 = 0;
float FsrForce2 = 0;
float FsrForce3 = 0;
float FsrForce4 = 0;
void setup(void){
Serial.begin(9600); // send debugging information via the Serial monitor
}
void loop(void){
iForceSensorReading = analogRead(A0);//read index finger pressure
delay(30);
// analog voltage reading ranges from about 0 to 1023 which maps to 0V to 5V (= 5000mV)
iForceSensorVoltage = map(iForceSensorReading, 0, 1023, 0, 5000);
if (iForceSensorVoltage == 0){
Serial.println("No pressure at index finger");
}
else{
ulForceSensorConductance = conductanceFunction(ulForceSensorResistance, iForceSensorVoltage);
delay(30);
}
if (ulForceSensorConductance <= 1000){
FsrForce = ulForceSensorConductance / 80;
Serial.print("Force applied by index finger in Newtons: ");
Serial.println(FsrForce, 4);
}
else{
FsrForce = ulForceSensorConductance - 1000;
FsrForce /= 30;
Serial.print("Force applied by index finger in Newtons: ");
Serial.println(FsrForce, 4);
delay(30);
}
Serial.println("-------------------------------------------------------");
iForceSensorReading1 = analogRead(A1);//read middle finger pressure
delay(30);
iForceSensorVoltage1 = map(iForceSensorReading1, 0, 1023, 0, 5000);
if (iForceSensorVoltage1 == 0){
Serial.println("No pressure at middle finger");
}
else{
ulForceSensorConductance1 = conductanceFunction(ulForceSensorResistance1, iForceSensorVoltage1);
delay(30);
}
if (ulForceSensorConductance1 <= 1000){
FsrForce1 = ulForceSensorConductance1 / 80;
Serial.print("Force applied by middle finger in Newtons: ");
Serial.println(FsrForce1, 4);
}
else{
FsrForce1 = ulForceSensorConductance1 - 1000;
FsrForce1 /= 30;
Serial.print("Force applied by middle finger in Newtons: ");
Serial.println(FsrForce1, 4);
delay(30);
}
Serial.println("-------------------------------------------------------");
iForceSensorReading2 = analogRead(A2);// read ring finger pressure
delay(30);
iForceSensorVoltage2 = map(iForceSensorReading2, 0, 1023, 0, 5000);
if (iForceSensorVoltage2 == 0){
Serial.println("No pressure at ring finger");
}
else{
ulForceSensorConductance2 = conductanceFunction(ulForceSensorResistance2, iForceSensorVoltage2);
delay(30);
}
if (ulForceSensorConductance2 <= 1000){
FsrForce2 = ulForceSensorConductance2 / 80;
Serial.print("Force applied by ring finger in Newtons: ");
Serial.println(FsrForce2, 4);
}
else{
FsrForce2 = ulForceSensorConductance2 - 1000;
FsrForce2 /= 30;
Serial.print("Force applied by ring finger in Newtons: ");
Serial.println(FsrForce2, 4);
delay(30);
}
Serial.println("-------------------------------------------------------");
iForceSensorReading3 = analogRead(A3);//read little finger pressure
delay(30);
iForceSensorVoltage3 = map(iForceSensorReading3, 0, 1023, 0, 5000);
if (iForceSensorVoltage3 == 0)
{Serial.println("No pressure at little finger");
}else {
ulForceSensorConductance3 = conductanceFunction(ulForceSensorResistance3, iForceSensorVoltage3);
delay(30);
}
if (ulForceSensorConductance3 <= 1000)
{FsrForce3 = ulForceSensorConductance3 / 80;
Serial.print("Force applied by little finger in Newtons: ");
Serial.println(FsrForce3, 4);
}else
{
FsrForce3 = ulForceSensorConductance3 - 1000;
FsrForce3 /= 30;
Serial.print("Force applied by little finger in Newtons: ");
Serial.println(FsrForce3, 4);
delay(30);
}
Serial.println("-------------------------------------------------------");
iForceSensorReading4 = analogRead(A4);//read thumb pressure
delay(30);
iForceSensorVoltage4 = map(iForceSensorReading4, 0, 1023, 0, 5000);
if (iForceSensorVoltage4 == 0)
{Serial.println("No pressure at thumb");
}
else
{
ulForceSensorConductance4 = conductanceFunction(ulForceSensorResistance4, iForceSensorVoltage4);
delay(30);
}
if (ulForceSensorConductance4 <= 1000)
{FsrForce4 = ulForceSensorConductance4 / 80;
Serial.print("Force applied by thumb in Newtons: ");
Serial.println(FsrForce4, 4);
}
else
{
FsrForce4 = ulForceSensorConductance4 - 1000;
FsrForce4 /= 30;
Serial.print("Force applied by thumb in Newtons: ");
Serial.println(FsrForce4, 4);
delay(30);
}
Serial.println("-------------------------------------------------------");
float totalForce = FsrForce + FsrForce1 + FsrForce2 + FsrForce3;// total force applied by hand
Serial.print("Total force applied by hand: ");
Serial.println(totalForce, 4);
delay(30);
Serial.println("-------------------------------------------------------");
delay(3000);
}
// The voltage = Vcc * R / (R + FSR) where R = 10K and Vcc = 5V
// so FSR = ((Vcc - V) * R) / V
int conductanceFunction(long x, long y)
{long result;
x = 5000 - y; // fsrVoltage is in millivolts so 5V = 5000mV
x *= 10000; // 10K resistor
x /= y;
result = 1000000/x; //ulForceSensorConductance2 = 1000000 measured in micromhos
return result;
}
答案 0 :(得分:0)
首先:
当iForceSensorVoltage不为零时,您只更新ulForceSensorConductance。当然,我希望很难得到一个完美的零。
if (iForceSensorVoltage == 0){
Serial.println("No pressure at index finger");
}
else{
ulForceSensorConductance = conductanceFunction(ulForceSensorResistance, iForceSensorVoltage);
delay(30);
}
第二
我看到ulForceSensorResistance仅被初始化并且从未实际使用过分配任何值,但作为第一个参数传递给conductanceFunction。其中x inside conductanceFunction立即用
覆盖unsigned long ulForceSensorResistance1;
...
ulForceSensorConductance1 = conductanceFunction(ulForceSensorResistance1, iForceSensorVoltage1);
...
int conductanceFunction(long x, long y)
{long result;
x = 5000 - y; // fsrVoltage is in millivolts so 5V = 5000mV