c - Arduino fsr 保持值(value)

标签 c arduino sensors arduino-ide

嗨,我正在做一个使用力传感器和 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;
    }

最佳答案

第一:

仅当 iForceSensorVoltage 不为零时才更新 ulForceSensorConductance。当然,我预计很难获得完美的零分。

  if (iForceSensorVoltage == 0){
    Serial.println("No pressure at index finger");  
  } 
  else{
  ulForceSensorConductance = conductanceFunction(ulForceSensorResistance,         iForceSensorVoltage);
  delay(30);
  } 

第二:

我看到 ulForceSensorResistance 仅被初始化,并且从未实际使用分配任何值,而是作为第一个参数传递到conductanceFunction。其中conductanceFunction中的x立即被覆盖

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

关于c - Arduino fsr 保持值(value),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21188389/

相关文章:

Java Arduino 串行读取

c - 如何在 Arduino Due 中创建精确的计时器

Android - 强制传感器?

c - 这段代码中的值 12800 将一个字符输出到串口是什么?

c++ - 为什么我的串行通过串行监视器在 Arduino IDE 上打印两次?

c++ - 在哪里可以找到++ 运算符的实现?

android - logcat 中有很多关于接近传感器的错误消息

arduino - fatal error : avr/io. h:没有这样的文件或目录 arduino

Clang 静态分析器意外的 NULL 指针警告

python - 安装 mply 库时出错。 fatal error : gsl/gsl_sf. h:没有这样的文件或目录