Load Cells...! :)

Hello Again. - I have this code, but I need some help to "build" upon it.

I could imagined that my Arduino to send 5v out to a relay when it reaches a certain weight. Can it be done?

http://pastebin.com/u38MZsLc

Greetings Morten

Folks here tend to not like going to pasteBin. I would suggest you post the code here.

It certainly can be done. Perhaps a little more detail would help you get better answers. You say you want to build upon this code. What exactly do you have in mind?

Okay. Quite all right. It comes here: Picture of The Code // 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.99*analogValueAverage + 0.01*analogValue;

// 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;

I would like to do so that my Arduino sends 5v out for a relay if the ex. measuring a weight of 2,3Kg. I can not describe it better than that.

//Morten

There are a few holes in that code, but you should be able to get your output working before they bite you.

You have a varaible called "load" where you are calculating the weight of the object. You could use a simple if statement to test whether that value is above some limit.

if(load > 2.3){
     digitalWrite(relayPin, HIGH);
}
else {
     digitalWrite(relayPin, LOW);
}

where you have of course defined a variable relayPin with the pin number and set it up as OUTPUT using pinMode. If you don't know what I mean in this last sentence, then you need to go work through some of the examples and learn how digital pins work.

if(millis() > time + timeBetweenReadings){

This has a bug waiting for you. It will work for a while and then crash. It should be written like this:

if(millis() - time > timeBetweenReadings){

Perfect. I thank you for your help.

  • I completely forgot to say that if my object weighs less than the 2,3Kg then it must trigger the relay. However, if it weighs more than 2,3Kg, the relay do not need to turn on.
  • Is it much to change in the code? :)

//Morten

ArduinoGandalf: Perfect. I thank you for your help.

  • I completely forgot to say that if my object weighs less than the 2,3Kg then it must trigger the relay. However, if it weighs more than 2,3Kg, the relay do not need to turn on.
  • Is it much to change in the code? :)

//Morten

Are you not even going to try... Then I'm done helping you. If you can't look at what I just wrote and figure out what one change to make to change the condition from grater than to less than then you probably don't need to be playing with microcontrollers.

Of course, I will try all that you have suggested me to try. - I just ordered an Arduino, and waiting for it comes in the mail :)

Greetings Morten