delay before reading data

Hi guys. I am wondering how can I have a code that has a delay before reading a data. Here is the code..

void loop()
{

while (Serial.available())

{
count = count + 1;

val= scale.read ();

quantity=newquantity;
newquantity = ((((((val - 107400)/305890.0f) * 50))-36.545)/0.555);

delay(500);
{
delay(3);
String a = Serial.readString();

{
if (newquantity == quantity-1)
{
Serial.println("1 PILL WAS TAKEN");

}
else if (newquantity == quantity+1)
{
Serial.println("1 PILL WAS ADDED");

}
else if (newquantity == quantity-2)
{
Serial.println("2 PILLS WERE TAKEN");
}
else if (newquantity == quantity+2)
{
Serial.println("2 PILLS WERE ADDED");
}
else if (newquantity == quantity-3)
{
Serial.println("3 PILLS WERE TAKEN");
}
else if (newquantity == quantity+3)
{
Serial.println("3 PILLS WERE ADDED");
}

So the code above uses load cell to measure the quantity of pills in the container. The if statements show that everytime I get 1 PILL it will say 1 pill were taken and everytime I get 1PILL it will say 1 PILL was added.

It always works in the 1 PILL condition but seldom works on 2 and 3 PILLS condition because I am having a hard time getting 2 or 3 pills in the container. I am wondering is there a way of delaying before reading because the load cell is quite sensitive and i sometimes apply pressure while getting 2 or 3 pills it will read my pressure sometimes 2 pills added and sometime 3 pills added. So i would like an assistance on this one where there is a delay like 5 seconds before reading the data or an ample time for me to get 2 or 3 pills without the system reading it. Thank you in advance. I hope you guys understand what my problem is.

I would suggest reading the load cell and wait for it to settle down, e.g. to give a constant reading (allowing for noise) over 2 or 3 seconds

In general using delay when reading serial data is a mistake. You want to read as soon as possible
always, so you don't lose/corrupt incoming serial data. You can always ignore data you are not ready for.

horace:
I would suggest reading the load cell and wait for it to settle down, e.g. to give a constant reading (allowing for noise) over 2 or 3 seconds

how do i do that? what code should i add?

MarkT:
In general using delay when reading serial data is a mistake. You want to read as soon as possible
always, so you don't lose/corrupt incoming serial data. You can always ignore data you are not ready for.

My application on this is when someone take two or three pills gsm module will text to a caregiver that the patient took 2 or 3 pills. So I need something to delay because its hard to get 2 or 3 pills in an instant unlike 1 pill.

ymmot17:
how do i do that? what code should i add?

read analogue value and wait for it to stay stable over a few seconds, e.g. in pseudo code

  // initialise time and read analogue
  initialTime = currentTime() ;
  oldValue=analogRead();
  while((currentTime() - initialTime) < settleTime)
  {
    delay(500);                         // delay
    newValue = analogRead();    // read the input pin
    println(newValue);             // display value
    if(abs(newValue-oldValue) > error)
     {   // analogue has not settled yet 
        oldValue=newValue;        // reset analogue value 
        initialTime = readTime();   // reset initial time
    }
  }
  print("Analogue settled at ");
  println(newValue);             // final value

this loops until the analogue reading has been stable (change less than error) for settleTime

The demo Several Things at a Time illustrates the use of millis() to manage timing without blocking. It may help with understanding the technique.

The examples in Serial Input Basics illustrate simple reliable ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.

...R

horace:
read analogue value and wait for it to stay stable over a few seconds, e.g. in pseudo code

  // initialise time and read analogue

initialTime = currentTime() ;
  oldValue=analogRead();
  while((currentTime() - initialTime) < settleTime)
  {
    delay(500);                        // delay
    newValue = analogRead();    // read the input pin
    println(newValue);            // display value
    if(abs(newValue-oldValue) > error)
    {  // analogue has not settled yet
        oldValue=newValue;        // reset analogue value
        initialTime = readTime();  // reset initial time
    }
  }
  print("Analogue settled at ");
  println(newValue);            // final value



this loops until the analogue reading has been stable (change less than error) for settleTime

Where will i put this in my code? I am sorry I am just a beginner in codes. I just want to read the amount of pills until it is stable

ymmot17:
Where will i put this in my code? I am sorry I am just a beginner in codes. I just want to read the amount of pills until it is stable

where you have the call

val= scale.read ();

insert the code replacing the calls to analogueRead() with call to scale.read ()
remember the example is in pseudo code so you will have to translate it to C/C++

horace:
where you have the call

val= scale.read ();

insert the code replacing the calls to analogueRead() with call to scale.read ()
remember the example is in pseudo code so you will have to translate it to C/C++

What values should I assign to settle time, error, readtime and initial time

the settleTime is the time you require for the signal to settle, initially try 5 seconds and see if it works then try reducing it
error is to allow for the noise in your signal - say 5% of the maximum value scale.read() gives? you may have to experiment with this
replace readTime() and currentTime() with calls to millis()

I’m surprised it works at all. Looks like you’re trying to do equality tests on floats. Of course, since you didn’t post your full code, I can’t see the actual variable definitions. Also, surprised no one has complained yet about your lack of code tags and use of Magic Numbers.