Hello I have connected 4 FSR and I got data but I would like to get values printed up to two decimal places. I got app that connect to arduino and display its reading but like I said it would be nice o get more decent data. I tried to change int to long for forceSensor1data but nothing changed
Since you are reading the analog to digital converter, you get integers between and including 0-1023. You can't do any better than that, unless you average many readings.
Delta_G:
Although the point in the previous post is very valid. You're taking 10bit readings from the adc. You can use math to get a number with decimals but it doesn't change the resolution of the measurement and doesn't make the data any better.
Yes, as you say, the measurement quantity is significantly less than 1024, then a decimal fraction is needed to represent the input using all the accuracy that is available. E.g. if 1024 = 5V then 500 = 2.44
The key thing is not to destroy the accuracy by truncating the value in intermediate calculations.
@OP, "magic numbers" like 94 are a no-no in programming. If you can't express it as a formula or named constant, you have to at least document it.
int fsrPin1 = 0; // the FSR and 10K pulldown are connected to a0
int fsrReading1; // the analog reading from the FSR resistor divider
int fsrVoltage1; // the analog reading converted to voltage
unsigned long fsrResistance1; // The voltage converted to resistance, can be very big so make "long"
unsigned long fsrConductance1;
long fsrForce1; // Finally, the resistance converted to force
int fsrPin2 = 1; // the FSR and 10K pulldown are connected to a0
int fsrReading2; // the analog reading from the FSR resistor divider
int fsrVoltage2; // the analog reading converted to voltage
unsigned long fsrResistance2; // The voltage converted to resistance, can be very big so make "long"
unsigned long fsrConductance2;
long fsrForce2; // Finally, the resistance converted to force
int fsrPin3 = 2; // the FSR and 10K pulldown are connected to a0
int fsrReading3; // the analog reading from the FSR resistor divider
int fsrVoltage3; // the analog reading converted to voltage
unsigned long fsrResistance3; // The voltage converted to resistance, can be very big so make "long"
unsigned long fsrConductance3;
long fsrForce3; // Finally, the resistance converted to force
int fsrPin4 = 3; // the FSR and 10K pulldown are connected to a0
int fsrReading4; // the analog reading from the FSR resistor divider
int fsrVoltage4; // the analog reading converted to voltage
unsigned long fsrResistance4; // The voltage converted to resistance, can be very big so make "long"
unsigned long fsrConductance4;
long fsrForce4; // Finally, the resistance converted to force
void setup(void) {
Serial.begin(9600); // We'll send debugging information via the Serial monitor
}
void loop(void) {
fsrReading1 = analogRead(fsrPin1);
fsrReading2 = analogRead(fsrPin2);
fsrReading3 = analogRead(fsrPin3);
fsrReading4 = analogRead(fsrPin4);
// analog voltage reading ranges from about 0 to 1023 which maps to 0V to 5V (= 5000mV)
fsrVoltage1 = map(fsrReading1, 0, 1023, 0, 5000);
fsrVoltage2 = map(fsrReading2, 0, 1023, 0, 5000);
fsrVoltage3 = map(fsrReading3, 0, 1023, 0, 5000);
fsrVoltage4 = map(fsrReading4, 0, 1023, 0, 5000);
if (fsrVoltage1 == 0) {
Serial.println("No pressure");
} else {
// The voltage = Vcc * R / (R + FSR) where R = 10K and Vcc = 5V
// so FSR = ((Vcc - V) * R) / V
fsrResistance1 = 5000 - fsrVoltage1; // fsrVoltage is in millivolts so 5V = 5000mV
fsrResistance1 *= 10000; // 10K resistor
fsrResistance1 /= fsrVoltage1;
fsrConductance1 = 1000000; // we measure in micromhos so
fsrConductance1 /= fsrResistance1;
// Use the two FSR guide graphs to approximate the force
if (fsrConductance1 <= 1000) {
fsrForce1 = fsrConductance1 / 80;
Serial.print("F1: ");
Serial.println(fsrForce1);
} else {
fsrForce1 = fsrConductance1 - 1000;
fsrForce1 /= 30;
Serial.print("F1: ");
Serial.println(fsrForce1);
}
}
if (fsrVoltage2 == 0) {
Serial.println("No pressure");
} else {
fsrResistance2 = 5000 - fsrVoltage2; // fsrVoltage is in millivolts so 5V = 5000mV
fsrResistance2 *= 10000; // 10K resistor
fsrResistance2 /= fsrVoltage2;
fsrConductance2 = 1000000; // we measure in micromhos so
fsrConductance2 /= fsrResistance2;
if (fsrConductance2 <= 1000) {
fsrForce2 = fsrConductance2 / 80;
Serial.print("F2: ");
Serial.println(fsrForce2);
} else {
fsrForce2 = fsrConductance2 - 1000;
fsrForce2 /= 30;
Serial.print("F2: ");
Serial.println(fsrForce2);
}
}
if (fsrVoltage3 == 0) {
Serial.println("No pressure");
} else {
fsrResistance3 = 5000 - fsrVoltage3; // fsrVoltage is in millivolts so 5V = 5000mV
fsrResistance3 *= 10000; // 10K resistor
fsrResistance3 /= fsrVoltage3;
fsrConductance3 = 1000000; // we measure in micromhos so
fsrConductance3 /= fsrResistance3;
if (fsrConductance3 <= 1000) {
fsrForce3 = fsrConductance3 / 80;
Serial.print("F3: ");
Serial.println(fsrForce3);
} else {
fsrForce3 = fsrConductance3 - 1000;
fsrForce3 /= 30;
Serial.print("F3: ");
Serial.println(fsrForce3);
}
}
if (fsrVoltage4 == 0) {
Serial.println("No pressure");
} else {
fsrResistance4 = 5000 - fsrVoltage4; // fsrVoltage is in millivolts so 5V = 5000mV
fsrResistance4 *= 10000; // 10K resistor
fsrResistance4 /= fsrVoltage4;
fsrConductance3 = 1000000; // we measure in micromhos so
fsrConductance3 /= fsrResistance3;
if (fsrConductance4 <= 1000) {
fsrForce4 = fsrConductance4 / 80;
Serial.print("F4: ");
Serial.println(fsrForce4);
} else {
fsrForce4 = fsrConductance4 - 1000;
fsrForce4 /= 30;
Serial.print("F4: ");
Serial.println(fsrForce4);
}
}
Serial.println("--------------------");
delay(1000);
}
is code good or it need some finishing touch? now force is not int but long so is it possible for two decimal places?