0

我正在做一个需要使用 arduino uno、4 个力传感器和 16x2 LCD 的项目。我正在尝试通过按钮的实现将力传感器的读数显示在 LCD 上。例如。如果我按下向上按钮,它应该显示第一个力传感器读数。问题是它只显示大量的 143164976.0000,即使没有对其施加任何力。请就我的编码是否有问题提出建议。

    int iForceSensorReading;     // the analog reading from the FSR resistor divider
    int iForceSensorReading1;
    int iForceSensorReading2;
    int iForceSensorReading3;
    int iForceSensorVoltage;     // the analog reading converted to voltage
    int iForceSensorVoltage1;
    int iForceSensorVoltage2;
    int iForceSensorVoltage3;
    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 ulForceSensorConductance;
    unsigned long ulForceSensorConductance1;
    unsigned long ulForceSensorConductance2;
    unsigned long ulForceSensorConductance3;
    float FsrForce = 0;       // Finally, the resistance converted to force
    float FsrForce1 = 0;
    float FsrForce2 = 0;
    float FsrForce3 = 0; 
    #include <Wire.h>
    #include <Adafruit_MCP23017.h>
    #include <Adafruit_RGBLCDShield.h>
    Adafruit_RGBLCDShield lcd = Adafruit_RGBLCDShield();
    #define RED 0x1
    #define YELLOW 0x3
    #define GREEN 0x2
    #define TEAL 0x6
    #define BLUE 0x4
    #define VIOLET 0x5
    #define WHITE 0x7
    void setup(void){
      Serial.begin(9600);   // send debugging information via the Serial monitor
      lcd.begin(16, 2);
      lcd.print("Hand Muscle");
      lcd.setCursor(0,1);
      lcd.print("Meter");
      lcd.setBacklight(WHITE);
    }
    uint8_t i=0;
    void loop(void){
    uint8_t buttons = lcd.readButtons();
    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);
    ulForceSensorConductance = conductanceFunction(ulForceSensorResistance,                 iForceSensorVoltage);
    if (ulForceSensorConductance <= 1000){
        FsrForce = ulForceSensorConductance / 80;    
        } 
    else{
        FsrForce = ulForceSensorConductance - 1000;
        FsrForce /= 30;
       }
    iForceSensorReading1 = analogRead(A1);//read middle finger pressure
    delay(30);
    iForceSensorVoltage1 = map(iForceSensorReading1, 0, 1023, 0, 5000);
    if (iForceSensorVoltage1 == 0){
      } 
    else{
    ulForceSensorConductance1 = conductanceFunction(ulForceSensorResistance1,         iForceSensorVoltage1);
    delay(30);
      }
    if (ulForceSensorConductance1 <= 1000){
        FsrForce1 = ulForceSensorConductance1 / 80;
        } 
    else{
    FsrForce1 = ulForceSensorConductance1 - 1000;
    FsrForce1 /= 30;  
        }
    iForceSensorReading2 = analogRead(A2);// read ring finger pressure
    delay(30);
    iForceSensorVoltage2 = map(iForceSensorReading2, 0, 1023, 0, 5000);
    ulForceSensorConductance2 = conductanceFunction(ulForceSensorResistance2,         iForceSensorVoltage2);
    if (ulForceSensorConductance2 <= 1000){
    FsrForce2 = ulForceSensorConductance2 / 80;
      }
    else{
    FsrForce2 = ulForceSensorConductance2 - 1000;
    FsrForce2 /= 30;   
      }
    iForceSensorReading3 = analogRead(A3);//read little finger pressure
    delay(30);
    iForceSensorVoltage3 = map(iForceSensorReading3, 0, 1023, 0, 5000);
    ulForceSensorConductance3 = conductanceFunction(ulForceSensorResistance3,         iForceSensorVoltage3);
    if (ulForceSensorConductance3 <= 1000) 
    {FsrForce3 = ulForceSensorConductance3 / 80;    
    }else 
    {FsrForce3 = ulForceSensorConductance3 - 1000;
     FsrForce3 /= 30;   
     }
    if (buttons) {
        lcd.clear();
        lcd.setCursor(0,0);
        if (buttons & BUTTON_UP) {
        lcd.print("Index Finger: ");
        lcd.setCursor(0,1);
        lcd.print(FsrForce,DEC);
        lcd.setBacklight(WHITE);
        }
        if (buttons & BUTTON_DOWN) {
        lcd.print("Little Finger: ");
        lcd.setCursor(0,1);
        lcd.print(FsrForce3,DEC); 
        lcd.setBacklight(WHITE);
        }
        if (buttons & BUTTON_LEFT) {
        lcd.print("Middle Finger: ");
        lcd.setCursor(0,1);
        lcd.print(FsrForce1,DEC); 
        lcd.setBacklight(WHITE);
        }
        if (buttons & BUTTON_RIGHT) {
        lcd.print("Ring Finger: ");
        lcd.setCursor(0,1);
        lcd.print(FsrForce2,DEC);  
        lcd.setBacklight(WHITE);
        }
        if (buttons & BUTTON_SELECT) {
        lcd.print("SELECT ");
        lcd.setBacklight(WHITE);
        }
      }
    }
    // 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;
    }
4

1 回答 1

0

first likely issue, is this symptom sounds like a signed vs un-signed conflict. I see in your code the following calls

ulForceSensorConductance = conductanceFunction(ulForceSensorResistance,                 iForceSensorVoltage);

where

int conductanceFunction(long x, long y)

but

int iForceSensorVoltage;

You are passing an int(signed) into the second argument. Using it on math and creating a resultant variable of

long result;

and returning that as a INT.

Where you may have other problems. You should follow some basic diagnostic procedures. Assuming this problem is on ALL channels. if not then it is likely electrical. Focusing on one channel.

PUT IN PRINTS of the various variables, to see what is moving as expected and validate the math is as expected

于 2014-01-17T15:00:40.023 回答