# Time Delay when Scale reaches certain weight

**URL:** <https://forum.arduino.cc/t/time-delay-when-scale-reaches-certain-weight/1151221>\
**Category:** Programming\
**Created:** [July 24, 2023, 12:37pm UTC](https://forum.arduino.cc/t/time-delay-when-scale-reaches-certain-weight/1151221 "2023-07-24T12:37:24Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jasonmagician](https://avatars.discourse-cdn.com/v4/letter/j/e99b99/32.png) [@jasonmagician](https://forum.arduino.cc/u/jasonmagician)\
**Post date:** [July 24, 2023, 12:37pm UTC](https://forum.arduino.cc/t/time-delay-when-scale-reaches-certain-weight/1151221/1 "2023-07-24T12:37:24Z")

</div>

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.

```arduino
#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);

}
}

```

---

<div class="post-metadata">

**Author:** ![StefanL38](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/stefanl38/32/713548_2.png) [@StefanL38](https://forum.arduino.cc/u/StefanL38)\
**Post date:** [July 24, 2023, 12:58pm UTC](https://forum.arduino.cc/t/time-delay-when-scale-reaches-certain-weight/1151221/2 "2023-07-24T12:58:29Z")

</div>

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

> [@Example-code for timing based on millis() easier to understand through the use of example-numbers / avoiding delay()](https://forum.arduino.cc/t/example-code-for-timing-based-on-millis-easier-to-understand-through-the-use-of-example-numbers-avoiding-delay/974017/1):
>
> non-blocking timing. Execute code only from time to time. There are a lot of different ways to learn programming. This thread wants to add another approach that is different to the yet existing ones. UPDATE 06.01.2023 if you are mainly interested in applying non-blocking timing you can do a quick read of this short tutorial / demonstration If you are intersted in understanding the details how it works go on reading here. I use an everyday analogon in this post to explain the basic principle …

best regards Stefan

---

<div class="post-metadata">

**Author:** ![b707](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/b707/32/854321_2.png) [@b707](https://forum.arduino.cc/u/b707)\
**Post date:** [July 24, 2023, 1:01pm UTC](https://forum.arduino.cc/t/time-delay-when-scale-reaches-certain-weight/1151221/3 "2023-07-24T13:01:16Z")

</div>

@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

---

<div class="post-metadata">

**Author:** ![ruilviana](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ruilviana/32/722429_2.png) [@ruilviana](https://forum.arduino.cc/u/ruilviana)\
**Post date:** [July 24, 2023, 1:28pm UTC](https://forum.arduino.cc/t/time-delay-when-scale-reaches-certain-weight/1151221/4 "2023-07-24T13:28:28Z")

</div>

try this code:

```arduino
#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));
  }
}

```

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [January 20, 2024, 1:29pm UTC](https://forum.arduino.cc/t/time-delay-when-scale-reaches-certain-weight/1151221/5 "2024-01-20T13:29:02Z")

</div>

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