Time Delay when Scale reaches certain weight

Hello friends.. I'm working on a project that involves an LED lighting up with a load cell reaches. certain number. Basically... I want the light to blink when a weight between 9g-11g is placed on the scale and stay solid when weight of 14g-16g is placed on the scale. The issue I'm having is that when I place the 15g weight on the code is picking up the 10g number as it passes by and blinking for a second, turning off then turning back on to solid when it reaches 15g. Then when I take the weight off and the number is dropping it is registering the blink again before it goes off and reaches zero. Any suggestions to stop this would be great.

#include <HX711_ADC.h> 

HX711_ADC scale (11,12); // 6 is green 7 is orange 

int ledPin = 10;

void setup()
{
Serial.begin(9600);
scale.begin(); // start connection to HX711
  scale.start(2000); // load cells gets 2000ms of time to stabilize
  scale.setCalFactor(1000.0); // calibration factor for load cell => dependent on your individual
pinMode (ledPin, OUTPUT);

}

void loop()
{
  scale.update(); // retrieves data from the load cell
  float i = scale.getData(); // get output value
Serial.print(scale.getData(), 1);
Serial.println("measurement");
delay(200);

if (i >= 9 && i <= 11) { //  Blinking 
digitalWrite (ledPin, HIGH);
delay (1000);
digitalWrite (ledPin, LOW);
}
else if (i >= 14 && i <= 16) { //  Solid 
digitalWrite (ledPin, HIGH);
}
else{
  digitalWrite (ledPin, LOW); //  No light everything is in the bag Penn has nothing

delay (100);

}
}

All your code should work with non-blocking timing to avoid the freezing of the microcontroller als long as a delay() is "executed"

Your code needs to check if the measured weight is settling on a fixed value or still changing.
If you do repeat to measure and the measured value deviates from the value measured before the settling is still "on the go"

If the measured values stay inside a small range for at least half a second the scale has settled.

repeated measuring and blinking at the same time is best done with non-blocking timing.

Attention !

Non-blocking timing works completely different from delay().
As long as you try to see a delay-similar thing it will be hard to understand how non-blocking timing works because it works so different.

Basically you have two ways to learn it:

way1: trying to be fast by not taking time to read carefully and then wasting time with trying this trying that and finally needing more time to work because you change to way 2 after having no success with trying to be fast
or
immitiately do it
way2: by carefully reading and taking time to understand before starting to code

here is a tutorial how it works

best regards Stefan

@jasonmagician
First of all you should avoid delays in the code. See the BlinlWithoutDelay example in the IDE.

As about the problem catching the intermediate values - when your measured weight is between 9 and 11 grams - take the short pause (0.1 - 0.3 s) and weighting again, than use the second value as trigger for led mode

try this code:

#include <HX711_ADC.h>
HX711_ADC scale (11, 12); // 6 is green 7 is orange
int ledPin = 10;
long previousMillis = 0;        // will store last time LED was updated
long interval = 1000;           // interval at which to blink (milliseconds)
//-------------------------------------------
void setup()
{
  Serial.begin(9600);
  scale.begin(); // start connection to HX711
  scale.start(2000); // load cells gets 2000ms of time to stabilize
  scale.setCalFactor(1000.0); // calibration factor for load cell => dependent on your individual
  pinMode (ledPin, OUTPUT);
}
//-------------------------------------------
void loop()
{
  scale.update(); // retrieves data from the load cell
  float i = scale.getData(); // get output value
  Serial.print(scale.getData(), 1);
  Serial.println(" measurement ");
  delay(200);
  //i = i * 10; // for debug
  //Serial.print(i, 1); // for debug
  switch (int(i)) {
    case 9 ...11:
      blinkWODelay();
      break;
    case 14 ...16:
      digitalWrite(ledPin, HIGH);
      break;
    default:
      digitalWrite(ledPin, LOW);
      break;
  }
}
//-------------------------------------------
void blinkWODelay() {
  if (millis() - previousMillis > interval) {
    previousMillis = millis();
    digitalWrite(ledPin, !digitalRead(ledPin));
  }
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.