2016-12-06 178 views
0

我有一個柔性傳感器連接一個arduino百合墊和一個47k ohms電阻。柔性傳感器與arduino

我的問題是,我無法從柔性傳感器得到一致的值:有時,我得到很奇怪的讀數,即使彎曲傳感器不是彎曲狀態

我試圖更改straight_resistancebend_resistance的值,但似乎無法解決問題。

這是我的代碼,期待尋求幫助。

const int FLEX_PIN = A0; // Pin connected to voltage divider output 

// Measure the voltage at 5V and the actual resistance of your 
// 47k resistor, and enter them below: 
const float VCC = 4.98; // Measured voltage of Ardunio 5V line (r1/r1+r2)5 
const float R_DIV = 47500.0; // Measured resistance of 3.3k resistor 

// Upload the code, then try to adjust these values to more 
// accurately calculate bend degree. 
const float STRAIGHT_RESISTANCE = 24248750.00; // resistance when  straight 
const float BEND_RESISTANCE = 48544996.00; // resistance at 90 deg 

void setup() 
{ 
    Serial.begin(9600); 
    pinMode(FLEX_PIN, INPUT); 
} 

void loop() 
{ 
    // Read the ADC, and calculate voltage and resistance from it 
    int flexADC = analogRead(FLEX_PIN); 
    float flexV = flexADC * VCC/1023.0; 
    float flexR = R_DIV * (VCC/flexV - 1.0); 
    Serial.println("Resistance: " + String(flexR) + " ohms"); 

    // Use the calculated resistance to estimate the sensor's 
    // bend angle: 
    float angle = map(flexR, STRAIGHT_RESISTANCE, BEND_RESISTANCE, 
       0, 90.0); 
    Serial.println("Bend: " + String(angle) + " degrees"); 
    Serial.println(); 

    delay(750); 
} 
+0

您是否檢查您的柔性傳感器是否沒有損壞?你可以嘗試用歐姆表檢查出來。 – zmo

回答

2

您的代碼中的以下值似乎不正確。這些是MOhms中的值,而不是kOhms。

const float STRAIGHT_RESISTANCE = 24248750.00; // resistance when  straight 
const float BEND_RESISTANCE = 48544996.00; // resistance at 90 deg 

另一個問題是map()函數只能用於整數!在map()函數使用它們之前,您傳遞的浮點值將轉換爲整數。