Run Continuous routine and update even while in loop of routine

Hi everyone and thanks for looking at my post. I’m trying to get the following to occur and I’m looking for the best method by which to accomplish the task.

I have a sensor input for weight and a LED output that blinks faster as more weight is applied. I have accomplished this using a simple if else loop. See the section of code below.

lpin is my variable for the light output pin.
WeightMeasure() is my function that returns the weight in lbs.

CODE

void setup()
{
pinMode(lpin, OUTPUT);
}
void loop()
{
WeightMeasure();// get the current weight in lbs

Serial.println("The wight of the object is: ");
Serial.print(WeightMeasure);
Serial.println(" lbs");

delay(100);

//runs the if loop to turn on the LED based on weight
if(WeightMeasure > 20){
digitalWrite(lpin, LOW);
delay(50);
Serial.println("I'm more than 20 LBS”);
}
else if(WeightMeasure > 10){
digitalWrite(lpin, HIGH);
delay(800);
digitalWrite(lpin, LOW);
delay(800);
Serial.println("I'm less than 20lbs but more than 10lbs ");
}
else if(WeightMeasure > 5 ){
digitalWrite(lpin, HIGH);
delay(500);
digitalWrite(lpin, LOW);
delay(500);
Serial.println("I'm less than 10lbs but more than 5lbs");
}
else if(WeightMeasure > 3){
digitalWrite(lpin, HIGH);
delay(200);
digitalWrite(lpin, LOW);
delay(200);
Serial.println("I'm less than 5lbs but more than 3lbs");
}
else {
digitalWrite(lpin, HIGH);
Serial.println("I'm less than 3lbs");

}

END CODE

This code is meant to blink more as less weight is applied to the sensor. While this works, it has a major flaw. I have set up the delay for blinks corresponding with each range of weights in each IF ELSE statement. This does not allow for updates in real time. For example, if an item of 20lbs is applied to the sensor, it will delay sampling weight again for 800ms. I would like to achieve the appropriate time length between blinks while the weight is over 20lbs but if the weight drops from 21lbs to 19lbs rapidly, I want it to immediately break out of the 800ms wait delay and begin blinking at the appropriately faster interval. Does anyone have a thought on how best to achieve this? I’m probably doing this completely wrong but I’m using my basic understanding of programming. An example or best method and explanation would be helpful as I’m really just a beginner. If anything is unclear, please ask.

Cheers,

War

See Blink without delay examples.

okay...

i found this example on the forum

but this does not fix the issue where I need the blinking to change "instantaneously" at the change in weight. I was thinking of using a blink delay value separately from the check weight loop.

This will allow any changes or actions to happen right away in the loop. Without the delay() holding up the loop. Please study some examples. it's time-sharing.

Hi warmonger0

To support what steinie44 said, the "BlinkWithoutDelay" example uses a variable to hold the time between LED blinks. As soon as your code detects a change in weight, it can change the value of that variable and the blinking will change to the new speed.

Regards

Ray

thank you.