Hi All,
I am using the following code on a Due board. It originally was written to read one load cell and I doubled everything up so it could read two load cells. I am getting dual readings as required on the serial monitor but it appears that the weight is been averaged using the Load0A & Load0B.
It seems to be ignoring Load1A & Load1B so I cannot calibrate the 2nd Load cell. Please can anyone help to see where I may be going wrong?
Thank you in advance Nevjc
// Arduino as load cell amplifier
// by Christian Liljedahl
// christian.liljedahl.dk
// Load cells are linear. So once you have established two data pairs, you can interpolate the rest.
// Step 1: Upload this sketch to your arduino board
// You need two loads of well know weight. In this example A = 10 kg. B = 30 kg
// Put on load A
// read the analog value showing (this is analogvalA)
// put on load B
// read the analog value B
// Enter you own analog values here
float load0A = 4.0; // kg
int analogval0A = 900; // analog reading taken with load A on the load cell
float load1A = 4.0;
int analogval1A = 100;
float load0B = 69.1; // kg
int analogval0B = 2976; // analog reading taken with load B on the load cell
float load1B = 69.1;
int analogval1B = 2200;
// Upload the sketch again, and confirm, that the kilo-reading from the serial output now is correct, using your known loads
float analogValueAverage0 = 0;
float analogValueAverage1 = 0;
// How often do we do readings?
int timeBetweenReadings = 200; // We want a reading every 200 ms;
void setup() {
Serial.begin(9600);
}
void loop() {
analogReadResolution(12);
int analogValue0 = analogRead(0);
int analogValue1 = analogRead(1);
// running average - We smooth the readings a little bit
analogValueAverage0 = 0.99analogValueAverage0 + 0.01analogValue0;
analogValueAverage1 = 0.99analogValueAverage1 + 0.01analogValue1;
// Is it time to print?
float load0 = analogToLoad(analogValueAverage0);
float load1 = analogToLoad(analogValueAverage1);
if (load0 < 0) { (load0=0);
}
if (load1 < 0) { (load1=0);
}
Serial.print(load0,1);
Serial.print(" ");
Serial.println(load1,1);
delay(10);
}
float analogToLoad(float analogval){
// using a custom map-function, because the standard arduino map function only uses int
float load0 = mapfloat0(analogval, analogval0A, analogval0B, load0A, load0B);
return load0;
float load1 = mapfloat1(analogval, analogval1A, analogval1B, load1A, load1B);
return load1;
}
float mapfloat0(float x, float in_min, float in_max, float out_min, float out_max)
{
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
float mapfloat1(float x, float in_min, float in_max, float out_min, float out_max)
{
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}