#include <HX711_ADC.h> // https://github.com/olkal/HX711_ADC
#include <Wire.h>
#include <LiquidCrystal_I2C.h> // LiquidCrystal_I2C library
HX711_ADC LoadCell(4, 5); // parameters: dt pin, sck pin<span data-mce-type="bookmark" style="display: inline-block; width: 0px; overflow: hidden; line-height: 0;" class="mce_SELRES_start"></span>
LiquidCrystal_I2C lcd(0x27, 16, 2); // 0x27 is the i2c address of the LCM1602 IIC v1 module (might differ)
int Relay = 9;
int val;
void setup()
{
LoadCell.begin(); // start connection to HX711
LoadCell.start(2000); // load cells gets 2000ms of time to stabilize
LoadCell.setCalFactor(968.303); // calibration factor for load cell => strongly dependent on your individual setup
lcd.begin(); // begins connection to the LCD module
lcd.backlight(); // turns on the backlight
pinMode(9, OUTPUT);
pinMode(Relay, OUTPUT); // it was missing in your sketch
}
void loop()
{
LoadCell.update(); // retrieves data from the load cell
float i = LoadCell.getData(); // get output value ;
int wt = (int)i;
lcd.setCursor(0, 0); // set cursor to first row
lcd.print("Weight[g]:"); // print out to LCD
lcd.setCursor(0, 1); // set cursor to secon row
lcd.print(i); // print out the retrieved value to the second row
//---------------------------------------
if (wt < 100)
{
digitalWrite(Relay, LOW);
}
if (wt > 100 && wt < 500)
{
digitalWrite(Relay, HIGH);
delay(15000);
digitalWrite(Relay, LOW);
}
if (wt > 500)
{
digitalWrite(Relay, HIGH);
delay(30000);
digitalWrite(Relay, LOW);
}
}
This coding work but when the relay triggered it did not reset after 15 seconds.
Can someone help me how to make the relay back to LOW state after 15 seconds ?
6v6gt:
If the weight on the load cell does not change, then after the 15 seconds has elapsed, the relay will go LOW, but then again immediately HIGH.
Add some Serial.print statements so you see exactly what is happening on the serial console.
so added the serial print statement and instead of printing on newline it print on a straight line and when reading hit above 100 the relay triggered but it did not get LOW after 15 second HIGH.
You can use Serial.println() instead to force a new line, but let me get this clear, the code never reaches this statement:
digitalWrite(Relay, LOW) ;
?
Walk through your code manually. Suppose weight is "250": You turn the relay on and, after 15-seconds, turn it off. But what immediately what happens if the weight is still present?
How do you want the logic to act?
If that "250" is present and you set your relay on-time for 15-seconds what do you actually want to do after turning the relay off? Do you, say, need to wait until the reading is "0" (or close to it) before looking at the weight again?
kira1996:
This coding work but when the relay triggered it did not reset after 15 seconds.
Can someone help me how to make the relay back to LOW state after 15 seconds ?
The relay is ON because this condition (wt>100 && <500) is satisfied. The relay remains ON for 15 sec and then it is OFF. It is ON again (to say instantly) because, the condition (wt>100 && <500) is still true. To make the relay OFF, you have to see that the weight has gone outside the boundary value of (wt>100 && <500). See this post (repeated below) which may be helpful for you; where, the relay goes OFF when the delay has expired or the weight has gone outside the boundary before the delay expires.