Go Down

Topic: Weight did not reset reading after triggering the relay (Read 117 times) previous topic - next topic

kira1996

Code: [Select]
#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.

kira1996

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.

what should i do ?

6v6gt

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) ;
?

Blackfin

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?


GolamMostafa

#5
Oct 23, 2019, 07:08 am Last Edit: Oct 23, 2019, 07:11 am by GolamMostafa
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.



Codes for the above Flow Chart/Control Program.
Code: [Select]
#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); //
LiquidCrystal_I2C lcd(0x27, 16, 2); //
int wt;
int Relay = 9;
int val;
unsigned long prMillis;

void setup()
{
  Serial.begin(9600);
  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.init();//begin(); // begins connection to the LCD module
  lcd.backlight(); // turns on the backlight
  pinMode(Relay, OUTPUT);
  // pinMode(Relay, OUTPUT);   // it was missing in your sketch
}

void loop()
{
  wt = wtDisplay();
  Serial.print(wt); //185
  //---------------------------------------
  if (wt > 100)
  {
    if (wt < 500)
    {
      processControl(15000, 15);
    }
    else
    {
      if (wt > 500)
      {
        processControl(30000, 30);
      }
    }
  }
  digitalWrite(Relay, LOW);
  delay(1000);
  //while (1);
}


int wtDisplay()
{
  LoadCell.update(); // retrieves data from the load cell
  float i = LoadCell.getData(); // get output value
  wt = (int)i;
  //wt = 615;//185;
  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(wt); // print out the retrieved value to the second row
  //  wt = 90;
 //wt = analogRead(A0);
  return wt;
}

void processControl(unsigned long x, int value)
{
  while (1)
  {
    digitalWrite(Relay, HIGH);
    prMillis = millis();
    while (millis() - prMillis < x)
    {
      wt = wtDisplay();
      if (value == 15)
      {
        if (!(wt > 100 && wt < 500))
        {
          break;
        }
      }
      else
      {
        if (value == 30)
        {
          if ((!wt > 500))
          {
            break;
          }
        }
      }
    }
    break;
  }
}



Go Up