What is the best way to interrupt a long delay

I’m working on a project of mine and I’m facing some programing difficulties since i don’t have much background. i have made most of the code to work tho, i just need to find a way to stop a delay when a condition is false.

So I’m working with a weight sensor, LCD, and a GSM shield. The weight sensor monitors a variable weight of an object and display that weight on the LCD. When the weight is low the LCD show “REFILL” and the GSM shield sends a text message to a phone number saying “REFILL”. When it is refilled the weight sensor go back to monitoring the weight. Thats all great, except when the weight is low the GSM shield keep sending continuous text messages until the weight is back up instead of sending just one text message. so i placed a long delay after the shield sends the first message however now when the weight is back up the weight sensor does not monitor the weight because the arduino would be still in the long delay.

i would like to know what is the best way to interrupt that delay whenever the weight is back up to it’s monitoring level.

here is the code i have so far.

#include <LiquidCrystal.h>
#include <hx711.h>
#include <GSM.h>

#define PINNUMBER “”

// initialize the library instance
GSM gsmAccess;
GSM_SMS sms;

LiquidCrystal lcd(12, 11, 5, 4, 9, 8 );
Hx711 scale(A1, A0);

void setup() {

lcd.begin(16, 2);

}

void loop() {

if(scale.getGram() >= -300 && scale.getGram() <= 300){
lcd.setCursor(0, 0);
lcd.print(scale.getGram(), 1);
lcd.print(" g");
lcd.print(" ");
}
else {
lcd.setCursor(0, 0);
lcd.print("REFILL ");

// initialize serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}

Serial.println(“SMS Messages Sender”);

// connection state
boolean notConnected = true;

// Start GSM shield
// If your SIM has PIN, pass it as a parameter of begin() in quotes
while (notConnected)
{
if (gsmAccess.begin(PINNUMBER) == GSM_READY)
notConnected = false;
else
{
Serial.println(“Not connected”);
delay(1000);
}
}

Serial.println(“GSM initialized”);

char remoteNum[20]=“9178214285”; // telephone number to send sms
Serial.println(remoteNum);

// sms text
char txtMsg[200]=“REFILL”;
Serial.println(“SENDING”);
Serial.println();
Serial.println(“Message:”);
Serial.println(txtMsg);

// send the message
sms.beginSMS(remoteNum);
sms.print(txtMsg);
sms.endSMS();
Serial.println("\nCOMPLETE!\n");

delay(25200000);

}
}

What is the best way to interrupt a long delay

Don't have one. There is no reason to use delay. The blink without delay example shows how to not use delay.

 // initialize serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only

This belongs in setup(), not loop().

Then don't use a delay(). The best delay is no delay.

You just have to look for the moment the weight becomes low.

bool isLow = false;

if(stateNow == low && isLow == false){
  //it became low!
  isLow = true;

  //send text
}
//if high again
if(stateNow != low){
  isLow = false; //store it's not low anymore
}

ps Did you add that smiley in there? Don't think so. So please read the rules and use code tags!

The demo Several Things at a Time illustrates the use of millis() to manage timing without blocking.

…R

That's indeed a good tutorial if he actually wanted to do time managment.

The delay() here is nothing more the a ugly fix bodge to prevent a action that only should have been triggered on a state change.

septillion: That's indeed a good tutorial if he actually wanted to do time managment.

Once a simple delay() won't work, that's what you need for non-blocking timing.

...R

 char remoteNum[20]="xxxxxxxxxxx";  // telephone number to send sms

If that is your real telephone number in your post I suggest you edit it and redact it.

Please edit your post, select the code, and put it between [code][/code] tags.

You can do that by hitting the “Code” icon above the posting area. It is the first icon, with the symbol: </>

Please use code tags.

Read this before posting a programming question

Robin2: Once a simple delay() won't work, that's what you need for non-blocking timing.

Yes, if you actually want delay... But he doesn't. He only inserted it as a bodge to stop the flood of text messages when the weight is low so he only gets a a text when the weight became low. And that's simply a state change problem.

septillion: And that's simply a state change problem.

Yes. I see that now. I had jumped to the conclusion from your opening words "Then don't use a delay(). The best delay is no delay." that the problem was timing.

...R