Arduino scale code

I have a load cell hooked to the arduino and I'd like to modify the code to close a switch when the load reading on the scale hits a set level.

I've got the hardware and code working to the point of accurately reading out weights. But when I try to add a command to energize the relay, it throws of the code; the scale weights are off. Etc.

Below I'll post the code I got from "Steve Spence" website.

Can someone please post the code with the changes neccesary for it to do what I request? Or point me in the right direction?

Thank you very much in advance,
Jim

Sorry, here it is:

// 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 loadA = 10; // kg
int analogvalA = 200; // analog reading taken with load A on the load cell

float loadB = 30; // kg
int analogvalB = 600; // analog reading taken with load B on the load cell

// Upload the sketch again, and confirm, that the kilo-reading from the serial output now is correct, using your known loads

float analogValueAverage = 0;

// How often do we do readings?
long time = 0; //
int timeBetweenReadings = 200; // We want a reading every 200 ms;

void setup() {
Serial.begin(9600);
}

void loop() {
int analogValue = analogRead(0);

// running average - We smooth the readings a little bit
analogValueAverage = 0.99analogValueAverage + 0.01analogValue;

// Is it time to print?
if(millis() > time + timeBetweenReadings){
float load = analogToLoad(analogValueAverage);

Serial.print("analogValue: ");Serial.println(analogValueAverage);
Serial.print(" load: ");Serial.println(load,5);
time = millis();
}
}

float analogToLoad(float analogval){

// using a custom map-function, because the standard arduino map function only uses int
float load = mapfloat(analogval, analogvalA, analogvalB, loadA, loadB);
return load;
}

float mapfloat(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;
}

Here it is. My intention is to trigger a relay upon hitting a level (ideally the load value that is displayed in the serial monitor).
Thanks,
-jim

// 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 loadA = 48.3; // kg
int analogvalA = 548.23; // analog reading taken with load A on the load cell

float loadB = 1051.9; // kg
int analogvalB = 305.75; // analog reading taken with load B on the load cell

// Upload the sketch again, and confirm, that the kilo-reading from the serial output now is correct, using your known loads

float analogValueAverage = 0;

// How often do we do readings?
long time = 0; //
int timeBetweenReadings = 2000; // We want a reading every 2000 ms;
int relay1 = 10;
void setup() {
Serial.begin(9600);
pinMode(relay1, HIGH);
}

void loop() {
int analogValue = analogRead(3);
// running average - We smooth the readings a little bit
analogValueAverage = 0.99analogValueAverage + 0.01analogValue;

// Is it time to print?
if(millis() > time + timeBetweenReadings){
float load = analogToLoad(analogValueAverage);

Serial.print("analogValue: ");Serial.println(analogValueAverage);
Serial.print(" load: ");Serial.println(load,5);
time = millis();
if(analogValueAverage > 500);
{ digitalWrite(relay1, HIGH);
}
}
}

float analogToLoad(float analogval){

// using a custom map-function, because the standard arduino map function only uses int
float load = mapfloat(analogval, analogvalA, analogvalB, loadA, loadB);
return load;
}

float mapfloat(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;
}

well, the weight measurements are ok now, but my led (switch) is not turning on when the load value exceeds 500.

if(analogValueAverage > 500);

Remove the semicolon.

thank you, thank you,
I got everything working.
excellent,
-jim PS,
here is the final code for all to enjoy!

// 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 loadA = 48.3; // kg
int analogvalA = 548.23; // analog reading taken with load A on the load cell

float loadB = 1051.9; // kg
int analogvalB = 305.75; // analog reading taken with load B on the load cell

// Upload the sketch again, and confirm, that the kilo-reading from the serial output now is correct, using your known loads

float analogValueAverage = 0;

// How often do we do readings?
long time = 0; //
int timeBetweenReadings = 2000; // We want a reading every 2000 ms;
int relay1 = 10;
void setup() {
Serial.begin(9600);
pinMode(relay1, HIGH);
}

void loop() {
int analogValue = analogRead(3);
// running average - We smooth the readings a little bit
analogValueAverage = 0.99analogValueAverage + 0.01analogValue;

// Is it time to print?
if(millis() > time + timeBetweenReadings){
float load = analogToLoad(analogValueAverage);

Serial.print("analogValue: ");Serial.println(analogValueAverage);
Serial.print(" load: ");Serial.println(load,5);
time = millis();
if(load > 150)
{ digitalWrite(relay1, HIGH);

}
}
}

float analogToLoad(float analogval){

// using a custom map-function, because the standard arduino map function only uses int
float load = mapfloat(analogval, analogvalA, analogvalB, loadA, loadB);
return load;
}

float mapfloat(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;
}

 if(millis() > time + timeBetweenReadings){

This line is still wrong.
See reply 3 from Delta_G

Right you are! Here is a fixed version that is stable:

// 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 loadA = 48.3; // kg
int analogvalA = 548.23; // analog reading taken with load A on the load cell

float loadB = 1051.9; // kg
int analogvalB = 305.75; // analog reading taken with load B on the load cell

// Upload the sketch again, and confirm, that the kilo-reading from the serial output now is correct, using your known loads

float analogValueAverage = 0;

// How often do we do readings?
long time = 0; //
int timeBetweenReadings = 1000; // We want a reading every 2000 ms;
int relay1 = 10;
void setup() {
Serial.begin(9600);
pinMode(relay1, HIGH);
}

void loop() {
int analogValue = analogRead(3);
// running average - We smooth the readings a little bit
analogValueAverage = 0.99analogValueAverage + 0.01analogValue;

// Is it time to print?
if(millis()- time >= timeBetweenReadings){
float load = analogToLoad(analogValueAverage);

Serial.print("analogValue: ");Serial.println(analogValueAverage);
Serial.print(" load: ");Serial.println(load,5);
time = millis();
if(load > 1000)
{ digitalWrite(relay1, HIGH);

}
}
}

float analogToLoad(float analogval){

// using a custom map-function, because the standard arduino map function only uses int
float load = mapfloat(analogval, analogvalA, analogvalB, loadA, loadB);
return load;
}

float mapfloat(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;
}

MODERATOR: 4 chances missed to learn to use [code]...[/code] tags..