Below is a working code that throws a switch when an arduino connected scale reaches a certain reading (weight). What I need help with is re-setting the relay. If I re-set the program, the tare reading changes and the weight of the samples will be different. What I propose is something like:
if relay switch has been thrown, check for state of a re-set switch and cease voltage to the relay.
Can someone kindly look through the code below and show me where to insert this type of statement? I have tried many times and cant seem to get the results I am after.
I should mention that I am a bit of a noob...
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 = 20; // 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.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();
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 edit: </mark> <mark>[code]</mark> <mark>
When do you want to reset the relay? After reading the weight, and activating the relay, it would make sense to me to read the switch, and reset the relay if the switch is pressed.
yes, that is my intention.
The desired weight is reached, the relay is activated, the switch is read, and the program either continues or stops awaiting the switch condition to change.
thanks,
-jim
Help with what? Reading the switch, if wired properly, is trivial. Turning a pin on or off is trivial. You need 2 to 7 lines of code. 2 or 3 statements and the rest are curly braces.
// 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 = 400; // We want a reading every 2000 ms;
int relay1 = 10;
int switch1 = 8;
void setup() {
Serial.begin(9600);
pinMode(relay1, OUTPUT);
pinMode(relay1, HIGH);
pinMode(switch1, INPUT);
}
void loop() {
int analogValue = analogRead(3);
int state = digitalRead(switch1);
// 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();
if(load > 100)
{ digitalWrite(relay1, HIGH);
}
if(state = HIGH){
analogWrite(relay1,LOW);
}
}
}
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 edit: </mark> <mark>[code]</mark> <mark>