So I've programmed it with two force-sensors. But in my code there's an error. Can someone show me, what I did wrong and how I can improve it? That would be nice.
Here's my code:
const int FSR_PIN = A0;
const int FSR_PIN1 = A1; // Pin connected to FSR/resistor divider
// Measure the voltage at 5V and resistance of your 3.3k resistor, and enter
// their value's below:
const float VCC = 4.98; // Measured voltage of Ardunio 5V line
const float R_DIV = 3230.0; // Measured resistance of 3.3k resistor
void setup()
{
Serial.begin(9600);
pinMode(FSR_PIN, INPUT);
pinMode(FSR_PIN1, INPUT);
}
void loop()
{
int fsrADC = analogRead(FSR_PIN);
int fsrADC1 = analogRead(FSR_PIN1);
// If the FSR has no pressure, the resistance will be
// near infinite. So the voltage should be near 0.
if((fsrADC != 0) && (fsrADC1 != 0)); // If the analog reading is non-zero
{
// Use ADC reading to calculate voltage:
float fsrV = fsrADC * VCC / 1023.0;
float fsrV1 = fsrADC1 * VCC / 1023.0;
// Use voltage and static resistor value to
// calculate FSR resistance:
float fsrR = R_DIV * (VCC / fsrV - 1.0);
float fsrR1 = R_DIV * (VCC / fsrV1 - 1.0);
Serial.println("Resistance: " + String(fsrR) + " ohms");
Serial.println("Resistance: " + String(fsrR1) + " ohms");
// Guesstimate force based on slopes in figure 3 of
// FSR datasheet:
float force;
float force1;
float fsrG = 1.0 / fsrR;
float fsrG1 = 1.0 / fsrR1;
if ((fsrR <= 600) && (fsrR1 <= 600));
force = (fsrG - 0.00075) / 0.00000032639;
force1 = (fsrG1 - 0.00075) / 0.00000032639;
else
force = fsrG / 0.000000642857;
force1 = fsrG1 / 0.000000642857;
Serial.println("Force: " + String(force) + " g");
Serial.println("Force: " + String(force1) + " g");
Serial.println();
Serial.println();
delay(500);
}
else
{
// No pressure detected
}
}
Thank you for the answer.