我有 flex sensor 连接 arduino lily pad 和47k ohms
电阻。
我的问题是我无法从 flex sensors 获得一致的值:有时候,即使Flex传感器没有处于弯曲状态,我也会得到非常奇怪的读数
我尝试更改straight_resistance
和bend_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 :(得分:2)
您的代码中的以下值似乎不对。这些是MOhms中的值,而不是kOhms。
const float STRAIGHT_RESISTANCE = 24248750.00; // resistance when straight
const float BEND_RESISTANCE = 48544996.00; // resistance at 90 deg
另一个问题是map()函数只适用于整数!在map()函数使用它们之前,传入的浮点值将转换为整数。